Homework 05-01-2006 Lab Counter
package cccc;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Counter
{
private int 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. Return 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);
}
}
//=================================
package cccc;
import java.io.IOException;
public class CounterDemo
{
public static void main(String[] args) throws IOException
{
Counter counter= new Counter();
counter.readInput();
counter.inc();
counter.output();
counter.inc();
counter.output();
counter.dec();
counter.output();
counter.reset();
counter.output();
}
}
0 Comments:
Post a Comment
<< Home