/* * Author: G Towell * File: Counter.java * Purpose: In-class demo * This class shows printing and class instances **/ public class Counter { /* * Just holds a number. The number has not actual use **/ private int count; /* * The default constructor. Realistically, this could be omitted */ public Counter() { count = 0; } /** * A constructor initializing the internal variable * @param val the value to set the internal variable **/ public Counter (int val) { count = val; } /** * Accessor for the count variable * @return the value of an internal variable **/ public int getCount() { return count; } /** * Increment an internal variable by 1 **/ public void increment() { count++; } /** * Decrement an internal variable by some amount * @param v the amount to decremet by **/ public void decrement(int v) { count -= v; } /** * Reset the value of the internal variable to zero **/ public void reset() { count = 0; } /********************** * main * @param args not used * Driver code for the demo **********************/ public static void main(String[] args) { Counter c; c = new Counter(); c.increment(); c.increment(); System.out.println(c.getCount()); c.reset(); Counter d = new Counter(5); d.increment(); Counter e = d; e.increment(); System.out.println(c.getCount() + " " + d.getCount() + " " + e.getCount()); } }