w3resource

Scala Programming: Check whether two given integer values are in the range 20..50 inclusive

Scala Programming Basic Exercise-17 with Solution

Write a Scala program to 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.

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 either (x is less than or equal to 20 or y is greater than or equal to 50) or (y is less than or equal to 20 or x is greater than or equal to 50)
      (x <= 20 || y >= 50) || (y <= 20 || x >= 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 20 and 84
      println("Result: " + test(20, 84))
      
      // Print the result of calling test with the arguments 14 and 50
      println("Result: " + test(14, 50))
      
      // Print the result of calling test with the arguments 11 and 45
      println("Result: " + test(11, 45))
      
      // Print the result of calling test with the arguments 25 and 40
      println("Result: " + test(25, 40))
    }
}

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): Boolean =: This line defines a function named test that takes two parameters, x and y, both of type Int, and returns a Boolean. The function checks if either (x is less than or equal to 20 or y is greater than or equal to 50) or (y is less than or equal to 20 or x is greater than or equal to 50).
  • (x <= 20 || y >= 50) || (y <= 20 || x >= 50);: This is the implementation of the "test()" function. It uses logical operators (|| and ||) to check the conditions specified above. The || represents logical OR.
    • (x <= 20 || y >= 50): Checks if x is less than or equal to 20 or y is greater than or equal to 50.
    • ||: 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 || x >= 50): Checks if y is less than or equal to 20 or x is greater than or equal to 50.
  • 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(20, 84)): This line calls the "test()" function with the arguments 20 and 84, concatenates the result with the string "Result: ", and prints the entire string to the console.
  • println("Result: " + test(14, 50)): Similar to the previous line, this calls the "test()" function with the arguments 14 and 50.
  • println("Result: " + test(11, 45)): Another call to the "test()" function with the arguments 11 and 45.
  • println("Result: " + test(25, 40)): Another call to the "test()" function with the arguments 25 and 40.

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: 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.
Next: Check whether a string 'yt' appears at index 1 in a given string. If it appears return a string without 'yt' otherwise return the original string.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.