w3resource

Scala Programming: Check a given integer and return true if it is within 20 of 100 or 300

Scala Programming Basic Exercise-5 with Solution

Write a Scala program to check a given integer and return true if it is within 20 of 100 or 300.

Sample Solution:

Scala Code:

// Define an object named scala_basic
object scala_basic {
  // Define a function named test with parameter x, returning a Boolean
  def test(x: Int): Boolean =
    {
      // Check if the absolute difference between 100 and x is less than or equal to 20,
      // or if the absolute difference between 300 and x is less than or equal to 20
      Math.abs(100 - x) <= 20 || Math.abs(300 - x) <= 20
    }
     
   // 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 argument 115
      println("Result: " + test(115))
      
      // Print the result of calling test with the argument 200
      println("Result: " + test(200))
      
      // Print the result of calling test with the argument 250
      println("Result: " + test(250))
      
      // Print the result of calling test with the argument 70
      println("Result: " + test(70))
   }
}

Sample Output:

Result: true
Result: false
Result: false
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): Boolean =: This line defines a function named test that takes an integer parameter x and returns a Boolean. The function checks if the absolute difference between x and 100 is less than or equal to 20, or if the absolute difference between x and 300 is less than or equal to 20. It returns true if any of these conditions is true, and false otherwise.
  • Math.abs(100 - x) <= 20 || Math.abs(300 - x) <= 20: This is a logical expression inside the test function. It calculates the absolute differences between x and 100, and between x and 300, and checks if either of these differences is less than or equal to 20 using the logical OR (||) operator.
  • 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(115)): This line calls the "test()" function with the argument 115, concatenates the result with the string "Result: ", and prints the entire string to the console.
  • println("Result: " + test(200)): Similar to the previous line, this calls the "test()" function with the argument 200.
  • println("Result: " + test(250)): Another call to the "test()" function with the argument 250.
  • println("Result: " + test(70)): Another call to the test function with the argument 70.

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

Previous: Check two given integers, and return true if one of them is 30 or if their sum is 30.
Next: Create a new string where 'if' is added to the front of a given string. If the string already begins with 'if', return the string unchanged.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/scala-exercises/basic/scala-basic-exercise-5.php