Scala Programming: Check whether three given integer values are in the range 20..50 inclusive
Scala Programming Basic Exercise-16 with Solution
Write a Scala program to check whether three given integer values are in the range 20..50 inclusive. Return true if 1 or more of them are in the said range otherwise false.
Sample Solution:
Scala Code:
// Define an object named scala_basic
object scala_basic {
// Define a function named test with parameters x, y, and z of type Int, returning a Boolean
def test(x: Int, y: Int, z: Int): Boolean =
{
// Check if either (x is between 20 and 50), (y is between 20 and 50), or (z is between 20 and 50)
(x >= 20 && x <= 50) || (y >= 20 && y <= 50) || (z >= 20 && z <= 50);
}
// 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 11, 20, and 12
println("Result: " + test(11, 20, 12))
// Print the result of calling test with the arguments 30, 30, and 17
println("Result: " + test(30, 30, 17))
// Print the result of calling test with the arguments 25, 35, and 50
println("Result: " + test(25, 35, 50))
// Print the result of calling test with the arguments 15, 12, and 8
println("Result: " + test(15, 12, 8))
}
}
Sample Output:
Result: true Result: true Result: true 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, y: Int, z: Int): Boolean =: This line defines a function named test that takes three parameters, x, y, and z, all of type Int, and returns a Boolean. The function checks if either (x is between 20 and 50), (y is between 20 and 50), or (z is between 20 and 50).
- (x >= 20 && x <= 50) || (y >= 20 && y <= 50) || (z >= 20 && z <= 50);: This is the implementation of the test function. It uses logical operators (&& and ||) to check the conditions specified above. The && represents logical AND, and || represents logical OR.
- (x >= 20 && x <= 50): Checks if x is between 20 and 50 (inclusive).
- ||: Logical OR, meaning either the condition on the left or the condition on the right should be true for the entire expression to be true.
- (y >= 20 && y <= 50): Checks if y is between 20 and 50 (inclusive).
- ||: Another logical OR.
- (z >= 20 && z <= 50): Checks if z is between 20 and 50 (inclusive).
- 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(11, 20, 12)): This line calls the "test()" function with the arguments 11, 20, and 12, concatenates the result with the string "Result: ", and prints the entire string to the console.
- println("Result: " + test(30, 30, 17)): Similar to the previous line, this calls the "test()" function with the arguments 30, 30, and 17.
- println("Result: " + test(25, 35, 50)): Another call to the "test()" function with the arguments 25, 35, and 50.
- println("Result: " + test(15, 12, 8)): Another call to the "test()" function with the arguments 15, 12, and 8.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Check two given integers whether either of them is in the range 100..200 inclusive.
Next: Check whether two given integer values are in the range 20..50 inclusive. Return true if 1 or other is in the said range otherwise false.
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-16.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics