Scala Programming: Check which number is nearest to the value 100 among two given integers
Write a Scala program to check which number is nearest to the value 100 among two given integers. Return 0 if the two numbers are equal.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, you should be familiar with the following concepts:
- Mathematical Operations in Scala: How to perform basic mathematical operations such as subtraction and finding absolute values.
- Using Math.abs: How to use the Math.abs() function to get the absolute value of a number in Scala.
- Conditional Statements: How to use if-else statements to compare values and return results based on certain conditions.
- Functions in Scala: Understanding how to define a function, pass parameters, and return values in Scala.
- 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 two integers as parameters.
- Hint 2: Use Math.abs() to calculate the absolute difference between each number and 100.
- Hint 3: Compare the absolute differences between the two numbers. If they are equal, return 0.
- Hint 4: Return the number that has the smaller absolute difference to 100.
- Hint 5: Test your function with different sets of integers to ensure it works as expected.
Sample Solution:
Scala Code:
// Define an object named scala_basic
object scala_basic {
// Define a function named test with parameters x and y of type Int, returning an Int
def test(x: Int, y: Int): Int = {
// Calculate the absolute difference between x and 100
val x_abs = Math.abs(x - 100)
// Calculate the absolute difference between y and 100
val y_abs = Math.abs(y - 100)
// Check conditions based on absolute differences
if (x_abs == y_abs) 0
else if (x_abs < y_abs) x
else 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 the arguments 78 and 95
println("Result: " + test(78,95))
// Print the result of calling test with the arguments 95 and 95
println("Result: " + test(95,95))
// Print the result of calling test with the arguments 99 and 70
println("Result: " + test(99,70))
}
}
Sample Output:
Result: 95 Result: 0 Result: 99
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.
- val x_abs = Math.abs(x - 100): This line calculates the absolute difference between x and 100 and assigns it to the variable x_abs.
- val y_abs = Math.abs(y - 100): This line calculates the absolute difference between y and 100 and assigns it to the variable y_abs.
- if (x_abs == y_abs) 0: This line checks if the absolute differences for x and y are equal. If true, it returns 0.
- else if (x_abs < y_abs) x: If the condition in the previous line is not true, this line checks if the absolute difference for x is less than the absolute difference for y. If true, it returns the value of x.
- else y: If none of the previous conditions are true, it returns the value of y.
- }: 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(78,95)): This line calls the "test()" function with the arguments 78 and 95, and prints the result to the console.
- println("Result: " + test(95,95)): Another call to the "test()" function with the arguments 95 and 95.
- println("Result: " + test(99,70)): Another call to the "test()" function with the arguments 99 and 70.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Check the largest number among three given integers.
Next: Check whether two given integers are in the range 40..50 inclusive, or they are both in the range 50..60 inclusive.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics