w3resource

Scala Programming: Get the absolute difference between n and 51

Scala Programming Basic Exercise-3 with Solution

Write a Scala program to get the absolute difference between n and 51. If n is greater than 51 return triple the absolute difference.

Sample Solution:

Scala Code:

// Define an object named scala_basic
object scala_basic {
  // Define a function named test with a parameter x, returning an Int
  def test(x: Int): Int =
    {
    // Calculate the absolute difference between x and 51 and assign it to abs_Diff
    val abs_Diff = Math.abs(x - 51)
    
    // Check if x is greater than 51
    if (x > 51) 
      // If true, calculate and return 3 times the absolute difference
      3 * abs_Diff 
    else 
      // If false, return the absolute difference
      abs_Diff
    }
     
   // 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 60
      println("Result: " + test(60))
      
      // Print the result of calling test with the argument 40
      println("Result: " + test(40))
   }
}

Sample Output:

Result: 27
Result: 11

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): Int =: This line defines a function named test that takes an integer parameter x and returns an integer. The function calculates the absolute difference between x and 51 and returns either 3 times this difference or the difference itself based on a condition.
  • val abs_Diff = Math.abs(x - 51): This line calculates the absolute difference between x and 51 and assigns it to the variable abs_Diff.
  • if (x > 51) 3 * abs_Diff else abs_Diff: This is a conditional expression inside the test function. If x is greater than 51, it returns 3 times the absolute difference; otherwise, it returns the absolute difference itself.
  • 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(60)): This line calls the "test()" function with the argument 60, concatenates the result with the string "Result: ", and prints the entire string to the console.
  • println("Result: " + test(40)): Similar to the previous line, this calls the "test()" function with the argument 40, concatenates the result with the string "Result: ", and prints the entire string to the console.

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

Previous: Compute the sum of the two given integer values. If the two values are the same, then return triples their sum.
Next: Check two given integers, and return true if one of them is 30 or if their sum is 30.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.