Hello everyone.
I am working on an assignment involving the return statement. What I have to do is create a private variable called newSeconds and make methods to change it's value only if it is greater than sixty and less than one hundred.
As you can see in the HR11Virus(Below) code I used the return and the System.out.println statements to show the value of the newSeconds variable. I originally used the return statement; that didn't print the value of the newSeconds variable so I used the System.out.println statement. It worked, so all is well.
But it made me wonder why the return statement didn't work. And secondly what are the differences of the return and System.out.println statements. From what I know System.out.print does everything the return statement does and more, if so why is there the return statement? And if the return statement's job is the return the value of a variable, why didn't it return the value of newSeconds?
public class HR11Virus { //Hour eleven, workshop one. private int newSeconds = 60; int getSeconds(int newValue){ if (newValue > 60){ newSeconds = newValue; System.out.println(newSeconds + " "); } return newSeconds; } }
Here is the program I used to test it.
class test { public static void main(String[] args) { HR11Virus virus = new HR11Virus(); virus.getSeconds(78); } }
Hope I was clear enough.
Thanks.
-Mel