Scala Programming: Find the larger value from two positive integer values that is in the range 20..30 inclusive
Write a Scala program to find the larger value from two positive integer values in the range 20..30 inclusive, or return 0 if neither is in that range.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, you should be familiar with the following concepts:
- Working with Lists in Scala: Understanding how to create and manipulate lists in Scala, and how to apply methods such as max on a list.
- Conditional Statements: Using if-else conditions to check if a number lies within a given range.
- Boolean Logic: Understanding how to combine conditions with logical operators like && (AND) to check if a value falls within a specific range.
- Return Statements: Returning values based on conditions inside a function.
Hints (Try Before Looking at the Solution!)
Try to solve the problem using these hints:
- Hint 1: Define a function that takes two integers as parameters and returns an integer.
- Hint 2: Use the max method on a List to find the larger of the two numbers.
- Hint 3: Check if the maximum number lies between 20 and 30 (inclusive) using an if statement.
- Hint 4: If the maximum number is in the range, return it; otherwise, return 0.
- Hint 5: Test the function with different values to ensure it behaves as expected.
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 an Int
def test(x: Int, y: Int): Int = {
// Calculate the maximum of the two numbers x and y using the max method on a List
val max_of_two = List(x, y).max
// Check if the maximum value is between 20 and 30 (inclusive)
// If true, return the maximum value; otherwise, return 0
if (max_of_two >= 20 && max_of_two <= 30) max_of_two else 0
}
// 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 20 and 30
println("Result: " + test(20,30))
// Print the result of calling test with the arguments 21 and 25
println("Result: " + test(21,25))
// Print the result of calling test with the arguments 28 and 28
println("Result: " + test(28,28))
}
}
Sample Output:
Result: 0 Result: 30 Result: 25 Result: 28
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): Int = {: This line defines a function named test that takes two parameters (x and y), both of type Int, and returns an Int.
- val max_of_two = List(x, y).max: This line calculates the maximum of the two numbers x and y using the max method on a List.
- if (max_of_two >= 20 && max_of_two <= 30) max_of_two else 0: This line checks if the calculated maximum value (max_of_two) is between 20 and 30 (inclusive). If true, it returns the maximum value; otherwise, it returns 0.
- }: 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(20,30)): Another call to the "test()" function with the arguments 20 and 30.
- println("Result: " + test(21,25)): Another call to the "test()" function with the arguments 21 and 25.
- println("Result: " + test(28,28)): Another call to the "test()" function with the arguments 28 and 28.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Check whether two given integers are in the range 40..50 inclusive, or they are both in the range 50..60 inclusive.
Next: Check whether a given character presents in a string between 2 to 4 times.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics