2009年4月13日 星期一

Homework 3-30-2009: counter

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. Include an accessor method that returns the current count value and a method that outputs the count to the screen. Write a program to test


counter.reset();
counter.inc();
counter.inc();
counter.dec();
counter.output();



class程式碼
------------------------------------------------<程式碼開始>--------------------------------------
public class Counter
{
public int count = 0;
public void reset()
{
count = 0;
}
public void inc()
{
count++;
}
public void dec()
{
count--;
}
public void output()
{
System.out.println(count);
}
}
------------------------------------------------<程式碼結束>--------------------------------------
主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;

public class Homework03302009Counter {
    public static void main (String args[]) {
        Counter counter = new Counter();
counter.reset();
counter.inc();
counter.inc();
counter.dec();
counter.output();
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

沒有留言:

張貼留言