w3resource

Scala Programming: Compute the sum of the two given integer values


Write a Scala program to compute the sum of the two given integer values. If the two values are the same, then return triples their sum.


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.
  • Conditional Statements: Using if-else statements to execute different blocks of code based on conditions.
  • Arithmetic Operations: Performing basic mathematical operations like addition 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 encapsulate the program logic.
  • Hint 2: Create a function that takes two integer parameters and returns their sum.
  • Hint 3: Use an if-else condition to check if both values are the same.
  • Hint 4: If the values are equal, return three times their sum; otherwise, return their simple sum.
  • Hint 5: Call the function in the main() method and print the result.

Sample Solution:

Scala Code:

// Define an object named scala_basic
object scala_basic {
  // Define a function named test with parameters x and y, returning an Int
  def test(x: Int, y: Int): Int =
    {
        // Check if x is equal to y
        if (x == y) 
          // If true, calculate (x + y) * 3 and return the result
          (x + y) * 3 
        else 
          // If false, calculate x + y and return the result
          x + y
    }
     
   // 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 arguments 1 and 2
      println("Result: " + test(1, 2))
      
      // Print the result of calling test with arguments 2 and 2
      println("Result: " + test(2, 2))
   }
}

Sample Output:

Result: 3
Result: 12

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): Int =: This line defines a function named 'test' that takes two parameters, x and y, both of type Int, and returns an Int. The function body is enclosed in curly braces.
  • if (x == y) (x + y) 3 else x + y: This is a conditional expression inside the "test()" function. It checks whether x is equal to y. If true, it calculates and returns the result of (x + y) 3; if false, it calculates and returns the result of x + y.
  • 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(1, 2)): This line calls the "test()" function with arguments 1 and 2, concatenates the result with the string "Result: ", and prints the entire string to the console.
  • println("Result: " + test(2, 2)): Similar to the previous line, this calls the "test ()" function with arguments 2 and 2, 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: Print "Hello, world" and version of the Scala language.
Next: Absolute difference between n and 51. If n is greater than 51 return triple the absolute difference.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.