w3resource

Scala Programming: Check the largest number among three given integers

Scala Programming Basic Exercise-19 with Solution

Write a Scala program to check the largest number among three given integers.

Sample Solution:

Scala Code:

// Define an object named scala_basic
object scala_basic {
  // Define a function named test with parameters x, y, and z of type Int, returning an Int
  def test(x: Int, y: Int, z: Int): Int = {
       // Create a List containing x, y, and z, and find the maximum value using the max method
       List(x, y, z).max
    }
     
   // 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 1, 2, and 3
      println("Result: " + test(1,2,3))
      
      // Print the result of calling test with the arguments 1, 3, and 2
      println("Result: " + test(1,3,2))
      
      // Print the result of calling test with the arguments 1, 1, and 1
      println("Result: " + test(1,1,1))
      
      // Print the result of calling test with the arguments 1, 2, and 2
      println("Result: " + test(1,2,2))
    }
}

Sample Output:

Result: 3
Result: 3
Result: 1
Result: 2

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, z: Int): Int = {: This line defines a function named test that takes three parameters (x, y, and z), all of type Int, and returns an Int. The function uses the List structure to create a list containing x, y, and z, and then finds the maximum value in that list using the max method.
  • List(x, y, z).max: This is the implementation of the "test()" function. It creates a List containing the values of x, y, and z and then calls the max method on that list to find the maximum value.
  • }: Closes the test function.
  • 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,3)): This line calls the "test()" function with the arguments 1, 2, and 3, and prints the result to the console.
  • println("Result: " + test(1,3,2)): Another call to the "test()" function with the arguments 1, 3, and 2.
  • println("Result: " + test(1,1,1)): Another call to the "test()" function with the arguments 1, 1, and 1.
  • println("Result: " + test(1,2,2)): Another call to the "test()" function with the arguments 1, 2, and 2.

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

Previous: Check whether a string 'yt' appears at index 1 in a given string. If it appears return a string without 'yt' otherwise return the original string.
Next: Check which number is nearest to the value 100 among two given integers. Return 0 if the two numbers are equal.

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-19.php