Homework 12-12
package javacode2;
public class Counter
{
public Counter()
{
count = 0;
}
public void increase()
{
count++;
}
public void decrease()
{
if(count >=1)
count--;
else
count = 0;
}
public int getVasue()
{
return count;
}
public void printcount()
{
System.out.println("counter count is " + count);
}
public boolean equals(Counter otherCounter)
{
return this.count == otherCounter.count;
}
public void resetToZero()
{
count = 0;
}
public String toString()
{
return Integer.toString(count) ;
}
private int count;
}
-----------------------------------------------------------------
package javacode2;
class CounterTest
{
public static void main(String[] args)
{
Counter counter1 = new Counter();
Counter counter2 = new Counter();
counter1.increase();
counter2.increase();
if (counter1.equals(counter2))
{
System.out.println( counter1 + " is equal to " + counter2);
}
else
{
System.out.println( counter1 + " is not equal to " + counter2);
}
counter2.increase();
counter1.decrease();
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