w3resource

Scala Programming: Get the absolute difference between n and 51


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


Pre-Knowledge (Before You Start!)

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

  • Scala Objects: Understanding how to define objects in Scala using the object keyword.
  • Functions in Scala: How to define functions with parameters and return values.
  • Mathematical Functions: Using Math.abs() to compute absolute values.
  • Conditional Statements: Implementing logic using if-else conditions.
  • Arithmetic Operations: Performing basic mathematical operations like subtraction and multiplication.
  • Main Method: Understanding the structure of the main() function in Scala.
  • Printing to Console: Using println() to output results.

Hints (Try Before Looking at the Solution!)

Try to solve the problem using these hints:

  • Hint 1: Define an object in Scala to contain the function.
  • Hint 2: Create a function that accepts an integer as a parameter.
  • Hint 3: Compute the absolute difference between the given number and 51 using Math.abs().
  • Hint 4: Use an if-else statement to check whether the number is greater than 51.
  • Hint 5: If the number is greater than 51, return three times the absolute difference; otherwise, return the absolute difference.
  • Hint 6: Call the function in the main() method and print the results.

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.