View Full Version : Java Help
javanoob1
12-07-2006, 06:58 PM
I know this is probably a very simple program. But I am a network engineer and am have a tuff time understanding the Method, class, etc. things. Anyway the program i supposed to
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 will 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.
here is my code....
/**
Demo program that exercises the methods of the Counter class
*/
public class CounterDemo {
public static void main(String[] args) {
// Make a new counter
Counter counter = new Counter();
System.out.println("Initial value is " + counter.getValue());
// Test the increment and toString() methods.
counter.increment();
counter.increment();
System.out.println(
"After two increments, value is " + counter.toString());
// Test the decrement method
counter.decrement();
System.out.println("After one decrement, value is " + counter);
// Test the output() method
System.out.println("Result of calling counter.output() :");
counter.output();
// Make a second counter to test equals.
Counter counter2 = new Counter();
System.out.println(counter + " equals " + counter2 + "? " +
counter.equals(counter2));
// Increment counter2 so that they are equal
counter2.increment();
System.out.println(counter + " equals " + counter2 + "? " +
counter.equals(counter2));
// Reset counter2 to zero
counter2.resetToZero();
System.out.println("After resetting to zero, value is " + counter2);
}
}
/**
Class that counts things.
*/
class Counter {
/**
Current value of the counter
*/
private int value = 0;
public int getValue()
{
return (value);
}
public int increment()
{
return (value)+1;
}
public int decrement()
{
return (value)-1;
}
public int resetToZero()
{
if (value>10)
return (0);
else
return (value);
}
/**
Prints the current value to System.out
*/
public void output() {
System.out.println("Counter value is " + value);
}
}
it compiles but it iwill not decrement, increment, etc.
any help would be greatly appreciated.
Thx,
NOOB:eek:
liorean
12-07-2006, 07:13 PM
Posted in the wrong forum. Moving to Computer Programming.
Also, please use an appropriate thread title that actually explains what type of problem you have, and do use [code][/code] blocks around code.
javanoob1
12-07-2006, 07:19 PM
thank you for the guidance.....:thumbsup:
rpgfan3233
12-07-2006, 07:33 PM
What you are looking for is something like this:
public int increment ()
{
value = value + 1;
return (value);
}
public int decrement ()
{
value = value - 1;
return (value);
}
public int resetToZero()
{
if (value>10)
value = 0;
return (value);
}
Notice how the 'value' variable is set in each one. Since you are returning 'value' in each one, you need to set it each time. That is why private int value = 0; is in the Counter class -- to be used in the program. For example, in your code, 'value' would always be equal to 0. When you increment it, it would do nothing other than return 1, which would be stored nowhere. When you decrement it, it would return -1, which would also be stored nowhere. You need to set 'value' equal to something in increment(), decrement(), resetToZero(), etc. getValue() is fine because it is returning the information stored in 'value'.
javanoob1
12-07-2006, 08:25 PM
Thank you so much......so far I am making progress and the lil lights are going off in my head....great explination....thanks again
Now I am getting the out put...
Initial value is 0
After two increments, value is Counter@2ba41
After one decrement, value is Counter@2ba41
Result of calling counter output() :
Counter value is 1
Counter@82ba41 equals Counter@923e30? false
Counter@82ba41 equals Counter@923e30? false
After resetting to zeror, value is Counter@923e30
I assume that is all Hexidecimal and the freaky out put and and coming up as equal has something to do with the counter.toString method.......
_Aerospace_Eng_
12-07-2006, 08:45 PM
No need to convert to a string because once you use System.out.println() it will convert it to a string and count will be read normally.
Roelf
12-07-2006, 08:46 PM
/**
Demo program that exercises the methods of the Counter class
*/
public class CounterDemo {
public static void main(String[] args) {
// Make a new counter
Counter counter = new Counter();
System.out.println("Initial value is " + counter.getValue());
// Test the increment and toString() methods.
counter.increment();
counter.increment();
System.out.println(
"After two increments, value is " + counter.toString());
// Test the decrement method
counter.decrement();
System.out.println("After one decrement, value is " + counter);
// Test the output() method
System.out.println("Result of calling counter.output() :");
counter.output();
// Make a second counter to test equals.
Counter counter2 = new Counter();
System.out.println(counter + " equals " + counter2 + "? " +
counter.equals(counter2));
// Increment counter2 so that they are equal
counter2.increment();
System.out.println(counter + " equals " + counter2 + "? " +
counter.equals(counter2));
// Reset counter2 to zero
counter2.resetToZero();
System.out.println("After resetting to zero, value is " + counter2);
}
}
/**
Class that counts things.
*/
class Counter {
/**
Current value of the counter
*/
private int value = 0;
public int getValue()
{
return (value);
}
public int increment()
{
return (value)+1;
}
public int decrement()
{
return (value)-1;
}
public int resetToZero()
{
if (value>10)
return (0);
else
return (value);
}
/**
Prints the current value to System.out
*/
public void output() {
System.out.println("Counter value is " + value);
}
}
In your testprogram, you are trying three different ways of getting the currentvalue for the Counter object.
First (and correct) Counter.getValue()
Second Counter.toString();
and third just plain Counter (which might call the toString() method also)
Correct that in your program and the strange output will go away
javanoob1
12-07-2006, 10:00 PM
:confused: I need to get the correct value to the toString . I have everything else functioning correctly....I think.....except for the toString method......I have searched and read my book about it but all it says is that it is a good idea to use it in all of your programs
rpgfan3233
12-07-2006, 10:50 PM
You could always overload the toString() method, if you have learned that. If not, you could create your own method to convert to a String. Look for a method in your book called valueOf() under the String class. It basically works like this:
String.valueOf(int argument);
The hint is the int (what in your code is a variable of type int?)
javanoob1
12-08-2006, 05:32 PM
ok so I got the toString() thing figured out.....thanks for all of your help....I am now begining to actually understand what I am writing as opposed to writing different things until it works. I now have the problem of getting the program to say that 1 is infact equal to 1. I must use the equals method. I assume that the reason for my output (below the code) saying that one does not infact equal one, is because the hashes are not equal. So I need to override both the equals and hash methods.....how in the world do I do that?
public class CounterDemo {
public static void main(String[] args) {
// Make a new counter
Counter counter = new Counter();
System.out.println("Initial value is " + counter.getValue());
// Test the increment and toString() methods.
counter.increment();
counter.increment();
System.out.println(
"After two increments, value is " + counter);
// Test the decrement method
counter.decrement();
System.out.println("After one decrement, value is " + counter);
// Test the output() method
System.out.println("Result of calling counter.output() :");
counter.output();
// Make a second counter to test equals.
Counter counter2 = new Counter();
System.out.println(counter + " equals " + counter2 + "? " +
counter.equals(counter2));
// Increment counter2 so that they are equal
counter2.increment();
System.out.println(counter + " equals " + counter2 + "? " +
counter.equals(counter2));
// Reset counter2 to zero
counter2.resetToZero();
System.out.println("After resetting to zero, value is " + counter2);
}
}
/**
Class that counts things.
*/
class Counter {
/**
Current value of the counter
*/
public int value = 0;
public Counter(){
this.value = 0;
}
public int getValue()
{
return value;
}
public int increment()
{
return value++;
}
public int decrement()
{
return value--;
}
public int resetToZero()
{
return value=0;
}
public String toString() {
return Integer.toString(value);
}
/**
Prints the current value to System.out
*/
public void output() {
System.out.println("Counter value is " + value);
}
}
output
Initial value is 0
After two increments, value is 2
After one decrement, value is 1
Result of calling counter.output() :
Counter value is 1
1 equals 0? false
1 equals 1? false
After resetting to zero, value is 0
rpgfan3233
12-08-2006, 07:20 PM
Overloading the equals() method is a bit different:
private boolean equals (Counter counter_two) {
if (value == counter_two.getValue())
return true;
return false;
}
That should work since all you are comparing are the two values. I also based that method on the code that you had first posted, where 'value' was a private variable rather than public as it is in your latest bit of code. If you keep it public, counter_two.value can replace counter_two.getValue().
javanoob1
12-08-2006, 07:53 PM
you rock!!
Thanks for helping me figure this one out!!!!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.