Scala Programming: Check a given integer and return true if it is within 20 of 100 or 300
Write a Scala program to check a given integer and return true if it is within 20 of 100 or 300.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, you should be familiar with the following concepts:
- Scala Objects: Understanding how to define objects using the object keyword.
- Functions in Scala: How to create functions with parameters and return values.
- Boolean Expressions: Understanding Boolean values (true or false) and logical conditions.
- Logical Operators: Using || (OR) to check multiple conditions.
- Mathematical Functions: Using Math.abs() to get the absolute difference.
- Comparison Operators: Understanding <= (less than or equal to) for range checks.
- Main Method: Understanding the structure of the main() function in Scala.
- Printing to Console: Using println() to display results.
Hints (Try Before Looking at the Solution!)
Try to solve the problem using these hints:
- Hint 1: Define an object in Scala to contain the function.
- Hint 2: Create a function that accepts an integer parameter.
- Hint 3: Use Math.abs() to calculate the absolute difference.
- Hint 4: Check if the absolute difference between the given number and 100 is less than or equal to 20.
- Hint 5: Check if the absolute difference between the given number and 300 is less than or equal to 20.
- Hint 6: Use logical OR (||) to combine both conditions.
- Hint 7: Return true if either condition is met; otherwise, return false.
- Hint 8: Call the function in the main() method with different test cases and print the results.
Sample Solution:
Scala Code:
// Define an object named scala_basic
object scala_basic {
// Define a function named test with parameter x, returning a Boolean
def test(x: Int): Boolean =
{
// Check if the absolute difference between 100 and x is less than or equal to 20,
// or if the absolute difference between 300 and x is less than or equal to 20
Math.abs(100 - x) <= 20 || Math.abs(300 - x) <= 20
}
// Define the main method, which is the entry point of the program
def main(args: Array[String]): Unit = {
// Print the result of calling test with the argument 115
println("Result: " + test(115))
// Print the result of calling test with the argument 200
println("Result: " + test(200))
// Print the result of calling test with the argument 250
println("Result: " + test(250))
// Print the result of calling test with the argument 70
println("Result: " + test(70))
}
}
Sample Output:
Result: true Result: false Result: false Result: false
Explanation:
Here is the break down of the said Scala code:
- object scala_basic {: This declares an object named scala_basic.
- def test(x: Int): Boolean =: This line defines a function named test that takes an integer parameter x and returns a Boolean. The function checks if the absolute difference between x and 100 is less than or equal to 20, or if the absolute difference between x and 300 is less than or equal to 20. It returns true if any of these conditions is true, and false otherwise.
- Math.abs(100 - x) <= 20 || Math.abs(300 - x) <= 20: This is a logical expression inside the test function. It calculates the absolute differences between x and 100, and between x and 300, and checks if either of these differences is less than or equal to 20 using the logical OR (||) operator.
- def main(args: Array[String]): Unit = {: This line defines the main method, which is the entry point of the program. It takes an array of strings (args) as its parameter and returns Unit (similar to void in other languages).
- println("Result: " + test(115)): This line calls the "test()" function with the argument 115, concatenates the result with the string "Result: ", and prints the entire string to the console.
- println("Result: " + test(200)): Similar to the previous line, this calls the "test()" function with the argument 200.
- println("Result: " + test(250)): Another call to the "test()" function with the argument 250.
- println("Result: " + test(70)): Another call to the test function with the argument 70.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Check two given integers, and return true if one of them is 30 or if their sum is 30.
Next: Create a new string where 'if' is added to the front of a given string. If the string already begins with 'if', return the string unchanged.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics