w3resource

Scala Programming: Find the larger value from two positive integer values that is in the range 20..30 inclusive

Scala Programming Basic Exercise-22 with Solution

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.

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?



Follow us on Facebook and Twitter for latest update.