Accessor & Mutator methods understanding..
Hello all, I've decided to become a member after seeing how great the support was.
Now, here's my question(s) and I hope someone can help me understand it better.
I have to create a class called Counter where the object in it count numbers, but not a negative number. I have to create a method that sets the counter to 0, increases it by 1, and decreases it by 1. I have to make sure no method allows the counter value to be negative. Then I have to include an accessor method that returns the current count value and a method to output it on the screen. There's no input method.
Here is my code:
Quote:
public class Counter
{
private int count; //set modifier to private restricting access to only inside class definition
public void setCount() //mutator method to set the count equal to 0
{
count = 0;
}
public void addOneToCount() //increments the value in count by 1
{
count++;
}
public void subtractOneToCount() //decrements the value in count by 1
{
if (count > 0) //value will never by a negative number
count--;
}
public int getCount() //accessor method to return the current count value
{
return count;
}
public void displayCount() //outputs the value in count on screen
{
System.out.println("The count is: " + count);
}
} //end of class Counter
Here is my Tester
Quote:
public class CounterTester
{
public static void main(String[] args) //Create main class to implement tasks
{
Counter theCounter = new Counter(); //Create calling object to invoke methods
theCounter.setCount(); //sets the value of count in object
theCounter.getCount(); //retrieves that value
theCounter.displayCount(); //shows that return value on screen
theCounter.addOneToCount(); //increment value in count by 3 for testing
theCounter.addOneToCount();
theCounter.addOneToCount();
theCounter.getCount(); //returns the value after incremented 3 times
theCounter.displayCount(); //shows the return value on screen
theCounter.subtractOneToCount(); //decrements value in count by 2 for testing
theCounter.subtractOneToCount();
theCounter.getCount(); //returns the value after decremented 2 times
theCounter.displayCount(); //shows the return value on screen
}
}
The following appears:
Quote:
The count is: 0
The count is: 3
The count is: 1
My question is from the TESTER.
Do I even need to use the accessor method getCount() since the value has already been preset in the class definition?
All I would need is the mutator method setCount() and then the output() method to show it on screen right??
Same thing goes with after I incremented the value 3 times, do I need to use the getCount() to get the current count value, which is 3?? Or can I omit that line of code and just use output() method, and it will give me the same current count value???
My understand of the accessor method from the TESTER is that its usually used when you want to set the value of the instance variables to something of your preferences, instead of having the value preset it for you. And that's why you use the accessor method getName() to retrieve that value, since the instance variable is marked private. Is that true?? Correct me if I'm wrong.
And I apologize for writing such a long post.
THANKS!