Scala Programming: Check the largest number among three given integers
Write a Scala program to check the largest number among three given integers.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, you should be familiar with the following concepts:
- Functions in Scala: How to define a function with parameters and a return type in Scala.
- Lists in Scala: How to create and work with lists in Scala, including accessing list elements.
- Using the max() method: How to use the max() method to find the largest element in a list.
- Conditional Statements (optional): Understanding how to perform comparisons and use conditional logic (though this particular solution uses a method directly).
- Testing Functions: Calling a function with different arguments to check if it behaves as expected.
Hints (Try Before Looking at the Solution!)
Try to solve the problem using these hints:
- Hint 1: Define a function that accepts three integers as parameters.
- Hint 2: Use a List to store the three integers.
- Hint 3: Use the max() method on the list to return the largest integer.
- Hint 4: Test your function with different sets of integers to check that it returns the correct result.
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?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics