Lab10
import java.io.*;
public class Counter {
private int count ;
public boolean equals(Counter aCounter)
{ return this.count == aCounter.count; }
public void readInput() throws IOException {
boolean tryAgain = true;
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System. in));
System.out.println("Enter a number.");
while (tryAgain)
{
String count1 = keyboard.readLine();
count = Integer.parseInt(count1);
if (count >= 0)
tryAgain = false;
else
System.out.println("Illegal date. Reenter input.");
}}
public void reset()
{count=0;}
public void inc()
{count=count+1;}
public void dec()
{count=count-1;}
public void output()
{System.out.println(count);}}
=============================
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
Counter counter1= new Counter(),
counter2= new Counter();
counter1.readInput();
counter2.readInput();
counter2.inc();
counter1.inc();
if ( counter1.equals(counter2))
System.out.println("counter1 = counter2");
else
System.out.println("They are not equal.");
counter2.dec();
counter1.inc();
if ( counter1.equals(counter2))
System.out.println("They are equal.");
else
System.out.println("They are not equal.");}}
結果:
Enter a number.
12
Enter a number.
12
counter1 = counter2
They are not equal.
public class Counter {
private int count ;
public boolean equals(Counter aCounter)
{ return this.count == aCounter.count; }
public void readInput() throws IOException {
boolean tryAgain = true;
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System. in));
System.out.println("Enter a number.");
while (tryAgain)
{
String count1 = keyboard.readLine();
count = Integer.parseInt(count1);
if (count >= 0)
tryAgain = false;
else
System.out.println("Illegal date. Reenter input.");
}}
public void reset()
{count=0;}
public void inc()
{count=count+1;}
public void dec()
{count=count-1;}
public void output()
{System.out.println(count);}}
=============================
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
Counter counter1= new Counter(),
counter2= new Counter();
counter1.readInput();
counter2.readInput();
counter2.inc();
counter1.inc();
if ( counter1.equals(counter2))
System.out.println("counter1 = counter2");
else
System.out.println("They are not equal.");
counter2.dec();
counter1.inc();
if ( counter1.equals(counter2))
System.out.println("They are equal.");
else
System.out.println("They are not equal.");}}
結果:
Enter a number.
12
Enter a number.
12
counter1 = counter2
They are not equal.
0 Comments:
張貼留言
<< Home