0

I'm having trouble visualizing the end product of this problem:

Define a class called Counter whose objects count things. An object of this class records a count that is a nonnegative integer. Include methods to set the counter to 0, to increase the count by 1, and to decrease the count by 1. Be sure that no method allows the value of the counter to become negative. Include an accessor method that returns the current count value and a method that outputs the count to the screen. There should be no input method or other mutator methods. The only method that can set the counter is the one that sets it to zero. Also, include a toString method and an equals method. Write a program (or programs) to test all the methods in your class definition.

Can someone better explain this problem for me?

  • 2
    If this is homework, please replace one of your tags with the homework tag. – ObscureRobot Oct 22 '11 at 05:24
  • 4
    It is very well explained... Which part do you not understand specifically? – Petar Ivanov Oct 22 '11 at 05:26
  • I'm voting to close this question as off-topic because: - Not a real programming problem. Lot of such solutions exist. - Obvious homework without refer to work done. - Actually this is not about programming at all. Just person having trouble with reading technical requirements to simple task. Not for this place. – Roman Nikitchenko Sep 02 '16 at 09:27

2 Answers2

4

Write code that defines a class. The class' name shall be Counter. (Think: what does this imply about the file name? Keep in mind that we will want this to be a public class so that we can use it for testing.) A Counter is used to count things (not surprising).

To do this, each instance of the class will contain a "count" value. (Think: what would be a good name for this value? How do we make a separate value for each instance of a class, that belongs to the instances?) The value shall not be negative. (Obvious; how could you have a negative number of things?) You are expected to make sure that the value cannot become negative (this should happen automatically, if you are doing things in remotely normal ways).

The class should provide the following functionality:

  • A method that sets the count value to 0. (Think: what is a good name for this method? It should be public of course, since it is part of how other code will use the class. Think: do you need any parameters for this method? If so, what?)

  • A method that increases the count by 1. (Think: as above.)

  • A method that decreases the count by 1. (Think: as above. Also, what happens if the count is already 0, given that we must not let it become negative? Decide what to do in this case.)

  • A method that returns the current count value. (This is called an "accessor").

  • A method that displays the current count value. (Note: this is a very bad idea design-wise, but your assignment requires it. Hint: use one of the methods of System.out to output to the screen.)

  • A method called toString that returns a String representation of the counter. This should not take any parameters, and must return a String. (This is a standard piece of functionality that most objects in Java provide. Think: other than the count value, is there anything else that ought to be included in the string?)

  • A method called equals that compares the Counter to another Counter for equality. This method must take exactly one parameter, a Counter instance, and return a boolean indicating whether the two Counters are equal. (Think: what determines whether two counters are "equal"?)

The class should NOT provide any of the following functionality:

  • Any other method that changes the value of the counter (especially not one that reads a value from standard input).

Write a program that creates some Counter instances and uses their methods. The program should prove that your Counter class behaves as expected, by producing output that agrees with what you'd expect from reading the main program. (Hint: since this is Java, we will need another class for this program, with a public static void main(String[] args) method. Put it in another file. You may need to play around with package and import statements so that the main program can find your Counter class in the other file. Make sure you understand how to compile and run Java classes.)

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Then I guess I got it right, because I got everything to increment/decrement once also stating if it's equal. –  Oct 24 '11 at 00:46
0

The end product is a class with the methods described in the problem. You should probably also supply a test program to verify that the program works as expected with some example data.

ObscureRobot
  • 7,306
  • 2
  • 27
  • 36