Scala Programming: Check whether two given integers are in the range 40..50 inclusive, or they are both in the range 50..60 inclusive
Scala Programming Basic Exercise-21 with Solution
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.
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?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/scala-exercises/basic/scala-basic-exercise-21.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics