Problem overview
The method isNegative takes an integer n and prints one word. It prints negative when n is less than zero, and positive otherwise. It returns nothing; the result is what appears on the console.
For n = 5 the output is positive. For n = -2 it is negative. For n = 0 it is positive, because zero is not less than zero. The lesson's word positive really means "zero or more".
Approach
There are exactly two outcomes, and each has a clear condition, so a single if-else is enough. Test the negative case, n < 0, and let else take everything else. Testing "less than zero" rather than "greater than zero" keeps zero on the right side without any special case.
Step by step
- Write the
ifwith its condition. The check isn < 0. Leave the body empty for the moment. - Print inside the block. Call
println("negative"), so the word appears only for negative input. - Add the
else. It prints"positive"for every other input. Zero lands here, since0 < 0is false.
The solution in Scala
class Solution {
def isNegative(n: Int): Unit = {
if (n < 0) {
println("negative")
} else {
println("positive")
}
}
}
Some Scala details in this code:
Unitas the return type.Unitis what a method returns when it has no useful result to give back. It plays the role thatvoidplays in other languages. The method's job here is the side effect of printing.- The block's value. In Scala the last expression of a block is its value. Here that expression is the
if-else, and each branch ends in aprintln, which itself returnsUnit. The types line up with the declared return type without anyreturnkeyword. - Parameter types are written after the name.
n: Intdeclares a 32-bit integer parameter namedn. - Output text is exact. The words must be lowercase, with no extra spaces, because the printed text is what gets checked.
A shorter way to write it
Since an if-else in Scala gives a value, the choice of word can be made first and printed once:
def isNegative(n: Int): Unit = {
println(if (n < 0) "negative" else "positive")
}
It prints the same words. The lesson's version, with a println in each branch, is the better fit for the three steps above. The version with a println in each branch makes the two paths easier to see while you are learning, but both are valid Scala.
Edge cases and common mistakes
- Zero.
isNegative(0)printspositive. Writingn <= 0would send it to the wrong branch. - The smallest
Int.-2147483648is less than zero, so it printsnegative. The method never negatesn, so no overflow can occur. - Returning instead of printing. The method's type is
Unit. The result has to be printed withprintln. - Printing both words. Without the
else, the secondprintlnwould run for every input, negative ones included. - Wrong capitalization.
"Negative"is not"negative".
Complexity
The method does one comparison and one print, so it takes constant time and uses constant extra space, whatever the value of n.
Testing
isNegative(5)printspositive.isNegative(0)printspositive.isNegative(-2)printsnegative.isNegative(-1)printsnegative.
