Scala Programming: Check whether two given integers are in the range 40..50 inclusive, or they are both in the range 50..60 inclusive
Write a Scala program to check whether two given integers are in the range 40..50 inclusive, or they are both in the range 50..60 inclusive.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, you should be familiar with the following concepts:
- Range Checking: How to check if a number falls within a specific range of values (inclusive or exclusive).
- List in Scala: How to work with lists in Scala and how to iterate over them.
- Using the forall Method: Understanding how to use the forall method to apply a condition to every element in a list.
- Boolean Logic: How to use logical operators (like || for "or") to combine multiple conditions.
- Conditional Statements: Basic knowledge of how to implement if-else conditions in Scala functions.
Hints (Try Before Looking at the Solution!)
Try to solve the problem using these hints:
- Hint 1: Create a function that takes two integers as parameters.
- Hint 2: Use the forall method to check if both integers are within the range 40..50 or the range 50..60.
- Hint 3: Use logical OR (||) to combine the two conditions: one for the first range (40..50) and the other for the second range (50..60).
- Hint 4: The function should return true if either of the conditions holds, and false otherwise.
- Hint 5: Test the function with different sets of input to verify it works correctly.
Sample Solution:
Scala Code:
// Define an object named scala_basic
object scala_basic {
// Define a function named test with parameters x and y of type Int, returning a Boolean
def test(x: Int, y: Int): Boolean = {
// Check if all elements in the list [x, y] satisfy the condition: x and y are both greater than or equal to 40 and less than or equal to 50,
// or x and y are both greater than or equal to 50 and less than or equal to 60
List(x, y).forall { m => m >= 40 && m <= 50 } || List(x, y).forall { n => n >= 50 && n <= 60 }
}
// 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 arguments 78 and 95
println("Result: " + test(78,95))
// Print the result of calling test with the arguments 25 and 35
println("Result: " + test(25,35))
// Print the result of calling test with the arguments 40 and 50
println("Result: " + test(40,50))
// Print the result of calling test with the arguments 55 and 60
println("Result: " + test(55,60))
}
}
Sample Output:
Result: false Result: false Result: true Result: true
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, y: Int): Boolean = {: This line defines a function named test that takes two parameters (x and y), both of type Int, and returns a Boolean.
- List(x, y).forall { m => m >= 40 && m <= 50 } || List(x, y).forall { n => n >= 50 && n <= 60 }: This line checks if all elements in the list [x, y] satisfy the following conditions:
- For the first forall block: Both elements (x and y) must be greater than or equal to 40 and less than or equal to 50.
- For the second forall block: Both elements must be greater than or equal to 50 and less than or equal to 60. If either of these conditions is true, the entire expression evaluates to true, indicating that the test is successful.
- }: Closes the test function.
- 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(78,95)): This line calls the "test()" function with the arguments 78 and 95, and prints the result to the console.
- println("Result: " + test(25,35)): Another call to the "test()" function with the arguments 25 and 35.
- println("Result: " + test(40,50)): Another call to the "test()" function with the arguments 40 and 50.
- println("Result: " + test(55,60)): Another call to the "test()" function with the arguments 55 and 60.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Check which number is nearest to the value 100 among two given integers. Return 0 if the two numbers are equal.
Next: Find the larger value from two positive integer values in the range 20..30 inclusive, or return 0 if neither is in that range.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics