Results 1 to 4 of 4
  1. #1
    6thDAY is offline Member
    Join Date
    Aug 2010
    Rep Power

    Default 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:

    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

    public void subtractOneToCount() //decrements the value in count by 1
    if (count > 0) //value will never by a negative number

    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
    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.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.getCount(); //returns the value after decremented 2 times
    theCounter.displayCount(); //shows the return value on screen
    The following appears:
    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.


  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    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.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    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,


  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power


    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.


Similar Threads

  1. Accessor/Mutator Question
    By noble in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 04:21 AM
  2. passing vs accessor
    By gcampton in forum New To Java
    Replies: 15
    Last Post: 01-08-2010, 03:21 AM
  3. Accessor method
    By DC200 in forum New To Java
    Replies: 19
    Last Post: 06-17-2009, 09:11 AM
  4. Replies: 6
    Last Post: 12-02-2008, 11:15 PM
  5. mutator method
    By dirtycash in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 10:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts