w3resource

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


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.


Pre-Knowledge (Before You Start!)

Before attempting this exercise, you should be familiar with the following concepts:

  • Scala Objects: Understanding how to define and use objects in Scala with the object keyword.
  • Functions in Scala: How to define functions that accept multiple parameters and return a value.
  • Comparison Operators: Using operators like <= (less than or equal to) and >= (greater than or equal to) to compare integers.
  • Logical Operators: Using logical operators like || (OR) to combine multiple conditions.
  • Conditional Statements: Writing conditions that check whether values lie within a specific range.
  • Testing Functions: How to call a function with multiple arguments and print the results using println().

Hints (Try Before Looking at the Solution!)

Try to solve the problem using these hints:

  • Hint 1: Create a function that takes two integer parameters.
  • Hint 2: Use the <= and >= operators to check if each integer is within the range 20 to 50 (inclusive).
  • Hint 3: Use logical OR || to combine conditions that check whether either of the integers is within the range.
  • Hint 4: Return true if at least one of the integers is in the range 20 to 50, otherwise return false.
  • Hint 5: Test the function with different pairs of integers, such as (20, 84), (14, 50), or (25, 40).

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.