Scala Programming: Check two given integers, and return true if one of them is 30 or if their sum is 30
Write a Scala program to check two given integers, and return true if one of them is 30 or if their sum is 30.
Pre-Knowledge (Before You Start!)
Before attempting this exercise, you should be familiar with the following concepts:
- Scala Objects: Understanding how to define objects using the object keyword.
- Functions in Scala: How to create functions with parameters and return values.
- Boolean Expressions: Understanding Boolean values (true or false) and logical conditions.
- Logical Operators: Using || (OR) to check multiple conditions.
- Equality Operators: Using == to compare values.
- Arithmetic Operations: Performing addition and checking conditions based on sums.
- Main Method: Understanding the structure of the main() function in Scala.
- Printing to Console: Using println() to display results.
Hints (Try Before Looking at the Solution!)
Try to solve the problem using these hints:
- Hint 1: Define an object in Scala to contain the function.
- Hint 2: Create a function that accepts two integer parameters.
- Hint 3: Use logical OR (||) to check multiple conditions.
- Hint 4: Check if either of the integers is equal to 30.
- Hint 5: Check if the sum of the two integers is equal to 30.
- Hint 6: Return true if any of the conditions are met; otherwise, return false.
- Hint 7: Call the function in the main() method with different test cases and print the results.
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?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics