• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Counter Class  RSS feed

 
Danny Treart
Ranch Hand
Posts: 37
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is my second week of Java (local community college), and this assignment seems a bit difficult for being so new. I've read the chapters over and over for hours and honestly, it doesn't really go in-depth into these particular subjects like the Professor states. Any resources or other forum posts I've found on the topic seem too difficult to dissect at this point.

I'm not asking anyone to do my assignment at all. I was hoping someone could perhaps explain this to me in a simpler fashion and/or have an idea for a place where things like this where examples aren't so difficult to break down?


Write a counter class in its own file. This will allow your counter to be used by any program.
The counter must be able to increment and decrement by one. It must never go below zero. toString and equals must be implemented and
tested correctly. (toString and equals are covered in depth in your text) System.out.println("”+c1) must work and if(c1.equals(c2)) must work
where c1 and c2 are instances of your counter. Include an override of the default constructor that sets the counter to zero and a constructor
that allows you to set the count.
Write a test program that tests all the features of the counter. See my farkleberry example in course documents.
This assignment tests your understanding of the material in the text. It also provides a template for future assignments. It is designed to
provide you with the basic tools to write commercial classes which are written in separate files and always include toString and equals. It is
also an example of how programmers test their classes prior to releasing them.
Possible Example Output:
--------------------Configuration: <Default>--------------------
This program creates and uses Counters
Initial state
counter1 is at 0
counter2 is at 0.
counter1 equals counter2.
Error - Attempted to subtract 1 widget from 0 widgets.
Number of widgets reset to 0.
State after first test
counter1 is at 1.
counter2 is at 0.
counter1 does not equal counter2.
State after second test
counter1 is at 2.
counter2 is at 2.
counter1 equals counter2.
Process completed.
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I am assuming you have Java installed and you know how to compile and run Java programs. If I am wrong, please correct me.

So, first you need to create a Counter class in a separate file.
As you probably know a file should be named as a class. So the name of the file should be Counter.java

Initial contents of the file should be:Save and compile. BGenerally you should save and compile very often.

Your Counter should have a variable to keep its state. So add one. It should be a member variable and should be private.

So your class is nowThen you should add methods to increment and decrement the variable, add constructors and equals, toString and hashCode methods.
The hashCode is very important because you should always write one when you write equals method. Your instruction did not say that you need to do this but I believe it is implied.

Tell us if you have any problem with this assignment and we will help
 
Danny Treart
Ranch Hand
Posts: 37
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

If I'm only creating two instances of the Class Counter (c1, c2), I'm not quite sure I understand how/or why to create/use two separate constructors that do two different things.
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not confuse writing two constructors with creating two instances.
Having two constructors means having two ways of creating an instance. It does not mean that you can create only two instances.

Do you know how to write a constructor? Read this. If you have any questions do not hesitate to ask.
 
Danny Treart
Ranch Hand
Posts: 37
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Just trying this out, and it keeps saying change method return type to integer in the toString method... Yet, when I look at any other examples of this online it seems like it should be right. Like in this instance that I'm looking up:

 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Treart wrote:Just trying this out, and it keeps saying change method return type to integer.

And the compiler is right. toString() method wants to return String. You are returning int. You need to convert this int to String.

And as for your second constructor. I believe it should not take an Counter instance as a parameter. I believe it should take an int.

And you are making prograss . That's nice .
 
Danny Treart
Ranch Hand
Posts: 37
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgive me, but I thought the toString method was for the purpose of converting it? In the Student Class example that I posted, I do not see where it is explicitly converting the rollno int into a string..
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Treart wrote:In the Student Class example that I posted, I do not see where it is explicitly converting the rollno int into a string..

If you concatenate an int and a String using +, the result is always a String.
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is the purpose of toString method. But you need to somehow generate a String out of your int and then return it. You can't return your c directly.
In your Student example the conversion from int to String happens automatically as a result of using + operator.
If you try to concatenate a String and int, the int will be converted to String automatically for you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!