Results 1 to 4 of 4
- 08-14-2010, 11:41 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 8
- Rep Power
- 0
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:
Here is my Testerpublic 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
The following appears: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
}
}
My question is from the TESTER.The count is: 0
The count is: 3
The count is: 1
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!
-
In your current Tester class, your calls to getCount() do nothing useful and can be dropped (if you remove them yourself, you'll see if it effects the output), but the getCount method itself is important for other reasons. For example:
What if you want to use the count result in some other calculation? For instance, what if you have two Counter objects, both that increment on some event, and you want in your Tester to add the results of both before displaying this? -- or -- What if you want to display the result from Counter in a different way, say all capitalized? Having the getCount() method is required for these and other uses.
- 08-14-2010, 12:07 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Another nitpick: you correctly test the counter value being zero before you decrement it but you don't test the counter value being Integer.MAX_VALUe before you want to increment it (it can become negative when it already had the largest possible value).
kind regards,
Jos
- 08-14-2010, 07:27 PM #4
Another nitpick: both by convention, and in line with the Java beans standards, "set" methods -- mutators -- are passed a parameter which is the value to be set. I would prefer to see resetCount() as the method that sets the value to zero.
Also, it wouldn't do any harm to name the other two methods incrementCount() / decrementCount().
And next time round, use the code tags, not the quote tags, to post code.
db
Similar Threads
-
Accessor/Mutator Question
By noble in forum New To JavaReplies: 4Last Post: 02-02-2010, 04:21 AM -
passing vs accessor
By gcampton in forum New To JavaReplies: 15Last Post: 01-08-2010, 03:21 AM -
Accessor method
By DC200 in forum New To JavaReplies: 19Last Post: 06-17-2009, 09:11 AM -
Trying to understand accessor and mutator methods
By DC200 in forum New To JavaReplies: 6Last Post: 12-02-2008, 11:15 PM -
mutator method
By dirtycash in forum New To JavaReplies: 7Last Post: 11-22-2007, 10:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks