Scala Programming: Check whether one of the given temperatures is less than 0 and the other is greater than 100
Scala Programming Basic Exercise-14 with Solution
Write a Scala program to check whether one of the given temperatures is less than 0 and the other is greater than 100.
Sample Solution:
Scala Code:
// Define an object named scala_basic
object scala_basic {
// Define a function named test with parameters temp1 and temp2 of type Int, returning a Boolean
def test(temp1: Int, temp2: Int): Boolean =
{
// Check if either (temp1 is below freezing and temp2 is above boiling) or (temp2 is below freezing and temp1 is above boiling)
(temp1 < 0 && temp2 > 100) || (temp2 < 0 && temp1 > 100)
}
// 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 120 and -1
println("Result: " + test(120, -1))
// Print the result of calling test with the arguments -1 and 120
println("Result: " + test(-1, 120))
// Print the result of calling test with the arguments 2 and 120
println("Result: " + test(2, 120))
}
}
Sample Output:
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(temp1: Int, temp2: Int): Boolean =: This line defines a function named test that takes two parameters, temp1 and temp2, both of type Int, and returns a Boolean. The function checks if either (temp1 is below freezing and temp2 is above boiling) or (temp2 is below freezing and temp1 is above boiling).
- (temp1 < 0 && temp2 > 100) || (temp2 < 0 && temp1 > 100): This is the implementation of the test function. It uses logical operators (&& and ||) to check the conditions specified above. The && represents logical AND, and || represents logical OR.
- (temp1 < 0 && temp2 > 100): Checks if temp1 is below freezing (less than 0) and temp2 is above boiling (greater than 100).
- ||: Logical OR, meaning either the condition on the left or the condition on the right should be true for the entire expression to be true.
- (temp2 < 0 && temp1 > 100): Checks if temp2 is below freezing and temp1 is above boiling.
- 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(120, -1)): This line calls the "test()" function with the arguments 120 and -1, concatenates the result with the string "Result: ", and prints the entire string to the console.
- println("Result: " + test(-1, 120)): Similar to the previous line, this calls the "test()" function with the arguments -1 and 120.
- println("Result: " + test(2, 120)): Another call to the "test()" function with the arguments 2 and 120.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Check whether a given string starts with 'Sc' or not.
Next: Check two given integers whether either of them is in the range 100..200 inclusive.
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-14.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics