boolean for increment method
Hello my original attempted was to make increment method in the class. But my teacher want it to be in boolean instead of void
Code :
public void increment() {
if (count < maxValue)
count++;
else
System.out.println("Counter overflow. Increment ignored.");
};
so it going to being something like this, but i am not very familiar with boolean. Anyone please point out a way to improve this. Thank you.
Code :
public boolean increment() {
return true;
};
Re: boolean for increment method
Booleans aren't that hard to understand: they're either true or false, on or off, yes or no, etc.
What it sounds like your teacher wants you to do is modify the first method to return true or false to indicate if the increment succeeded.
A good way to think about booleans is to phrase a true/false question:
The increment method succeeded in increasing count's value. True or false?
The increment method failed to increase count's value. True or false?
Depending on which of these two questions you want to answer your method's return value will mean something different. From what you've posted it isn't clear which your teacher wants, though I have a tendency to lean towards the first because that's the approach usually taken.
You just need to modify your first method to properly answer this question without using System.out. This is console output, different from returning a value from a method.
Re: boolean for increment method
This is what i came up with. It run but it doesn't say anything if it false.
Code :
public boolean increment() {
if (count < maxValue)
{
count++;
return true;
}
else
//System.out.println("Increment ignored.");
return false;
};
Re: boolean for increment method
Normally the intent is for a method to perform work and return a result. The calling method will then decide what it wants to do with that result. This could be perform more computations, write it to a file, print out a message to the console, etc.
I don't have your assignment so I'm not positive if that's what your teacher wants, but if I had to guess that is what I would do. It's best to read the assignment or check with a member of the teaching staff to ensure you have an acceptable solution.
Re: boolean for increment method
Quote:
Originally Posted by
helloworld922
Normally the intent is for a method to perform work and return a result. The calling method will then decide what it wants to do with that result. This could be perform more computations, write it to a file, print out a message to the console, etc.
I don't have your assignment so I'm not positive if that's what your teacher wants, but if I had to guess that is what I would do. It's best to read the assignment or check with a member of the teaching staff to ensure you have an acceptable solution.
The problem here is there not actually an assignment. He just gave everyone the skeleton of the program, and expect the result. So I don't know what really to do :(. Well if there a better way to put that code please help me out, or the edit one be ok?
Re: boolean for increment method
You're guess is as good as mine. My suggestion is to ask other students for what they did, and/or ask your teacher (the teacher is the best option because they most likely know what they want).
That aside, if I were the teacher I would say your modified code is close to matching what I would consider to be good programming practice, the only things I can tell that are missing are comments.
Re: boolean for increment method