w3resource

Scala Programming: Check two given integers whether either of them is in the range 100..200 inclusive


Write a Scala program to check two given integers whether either of them is in the range 100..200 inclusive.


Pre-Knowledge (Before You Start!)

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

  • Scala Objects: Understanding how to define objects using the object keyword.
  • Functions in Scala: How to create functions that take parameters and return values.
  • Comparison Operators: Using operators like > (greater than) and < (less than) to compare integers.
  • Logical Operators: Using logical operators like && (AND) and || (OR) to combine conditions.
  • Conditional Statements: Using if statements to check conditions.
  • Testing Functions: How to call functions with different arguments and print the results using println().

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.
  • Hint 2: Use the >= and <= operators to check if each integer is within the range 100 to 200 (inclusive).
  • Hint 3: Combine the checks for both integers using the || (logical OR) operator.
  • Hint 4: Return true if either integer is within the range, otherwise return false.
  • Hint 5: Test the function with different pairs of integers, like 100 and 199, or 250 and 300.

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 between 100 and 200) or (y is between 100 and 200)
      (x >= 100 && x <= 200) || (y >= 100 && y <= 200);
    }
     
   // 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 100 and 199
      println("Result: " + test(100, 199))
      
      // Print the result of calling test with the arguments 250 and 300
      println("Result: " + test(250, 300))
      
      // Print the result of calling test with the arguments 105 and 190
      println("Result: " + test(105, 190))
    }
}

Sample Output:

Result: true
Result: false
Result: true

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 between 100 and 200) or (y is between 100 and 200).
  • (x >= 100 && x <= 200) || (y >= 100 && y <= 200);: 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 >= 100 && x <= 200): Checks if x is between 100 and 200 (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 >= 100 && y <= 200): Checks if y is between 100 and 200 (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(100, 199)): This line calls the "test()" function with the arguments 100 and 199, concatenates the result with the string "Result: ", and prints the entire string to the console.
  • println("Result: " + test(250, 300)): Similar to the previous line, this calls the "test()" function with the arguments 250 and 300.
  • println("Result: " + test(105, 190)): Another call to the test function with the arguments 105 and 190.

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

Previous: Check whether one of the given temperatures is less than 0 and the other is greater than 100.
Next: 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.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.