Scala Programming: Check two given integers, and return true if one of them is 30 or if their sum is 30
Scala Programming Basic Exercise-4 with Solution
Write a Scala program to check two given integers, and return true if one of them is 30 or if their sum is 30.
Sample Solution:
Scala Code:
// Define an object named scala_basic
object scala_basic {
// Define a function named test with parameters x and y, returning a Boolean
def test(x: Int, y: Int): Boolean =
{
// Check if x is equal to 30, y is equal to 30, or the sum of x and y is equal to 30
x == 30 || y == 30 || x + y == 30
}
// 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 30 and 0
println("Result: " + test(30, 0))
// Print the result of calling test with arguments 25 and 5
println("Result: " + test(25, 5))
// Print the result of calling test with arguments 30 and 20
println("Result: " + test(30, 20))
// Print the result of calling test with arguments 25 and 20
println("Result: " + test(25, 20))
}
}
Sample Output:
Result: true Result: true Result: true Result: false
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): Boolean =: This line defines a function named test that takes two integer parameters, x and y, and returns a Boolean. The function checks if x is equal to 30, y is equal to 30, or the sum of x and y is equal to 30. It returns true if any of these conditions is true, and false otherwise.
- x 30 || y 30 || x + y == 30: This is a logical expression inside the test function. It checks if any of the conditions (equality to 30) is true using the logical OR (||) operator.
- 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(30, 0)): This line calls the "test()" function with the arguments 30 and 0, concatenates the result with the string "Result: ", and prints the entire string to the console.
- println("Result: " + test(25, 5)): Similar to the previous line, this calls the : "test()" function with the arguments 25 and 5, concatenates the result with the string "Result: ", and prints the entire string to the console.
- println("Result: " + test(30, 20)): Another call to the "test()" function with the arguments 30 and 20.
- println("Result: " + test(25, 20)): Another call to the test function with the arguments 25 and 20.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Absolute difference between n and 51. If n is greater than 51 return triple the absolute difference.
Next: Check a given integer and return true if it is within 20 of 100 or 300.
What is the difficulty level of this exercise?
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://w3resource.com/scala-exercises/basic/scala-basic-exercise-4.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics