JAVA

2005/12/19

Homework 12-12-2005 Counter II

package hw;

public class hw {
private int value = 0;
public boolean equals(hw otherCounter) {
return this.value == otherCounter.value;
}

public void resetToZero() {
value = 0;
}

public void increment() {
value++;
}

public void decrement() {
if (value <= 0)
value = 0;
else
value--;
}

public int getValue() {
return value;
}

public void output() {
System.out.println(" Counter value is " + value);
}

public String toString() {
return Integer.toString(value);
}
}

-------------------------------------------------------------------------
package hw;

public class hw1 {
public static void main(String[] args) {
hw counter1 = new hw();
hw counter2 = new hw();
counter1.resetToZero();
counter1.increment();
counter2.resetToZero();
counter2.increment();
if (counter1.equals(counter2)) {
System.out.println( counter1 + " is equal to " + counter2);
} else {
System.out.println( counter1 + " is not equal to " + counter2);
}
counter2.increment();

if (counter1.equals(counter2)) {
System.out.println( counter1 + " is equal to " + counter2);
} else {
System.out.println( counter1 + " is not equal to " + counter2);
}
}
}

0 Comments:

張貼留言

<< Home