Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+18 votes

In Lab 5, in isLeapYear method, when we return true/false, how do we use that result in main method.

asked in CSC211_Winter2018 by (1 point)

2 Answers

+11 votes
 
Best answer

This is how we call a boolean method:

If ((parameter)) { //this is a true statement
.....
}
If (!(parameter)) { or else //this is a false statement
.....
}

answered by (1 point)
selected by
+8 votes

You have two options.

You can store the return value of the method in a boolean variable, and then use that variable as the test in the if statement....

boolean isALeapYear = isLeapYear(year); // or whatever variable stores the year
if (isALeapYear){
......

OR

since the isLeapYear method return true or false, you can use the call to isLeapYear as the test in the if statement directly (which is preferable and in the slides how the isPrime method was called)

if (isLeapYear(year)){
......

answered by (1 point)
...