// Stuart Reges handout #24 // 8/27/99 // // Class Accumulator keeps track of the state of an adding machine. It // accumulates digits to form entire numbers and either adds or subtracts // those numbers to a cumulative sum. It also has a reset option. public class Accumulator { private int myCurrent; // current number private int mySum; // current sum private int myDisplay; // current display // post: current number, current sum, display all cleared public void clear() { myCurrent = 0; mySum = 0; myDisplay = 0; } // post: digit is added to the end of the current number public void addDigit(int digit) { myCurrent = myCurrent * 10 + digit; myDisplay = myCurrent; } // post: current number is added to sum and cleared, current // sum is displayed public void plus() { mySum = mySum + myCurrent; myCurrent = 0; myDisplay = mySum; } // post: current number is subtracted from sum and cleared, // current sum is displayed public void minus() { mySum = mySum - myCurrent; myCurrent = 0; myDisplay = mySum; } // post: returns current display public int getDisplay() { return myDisplay; } }
Stuart Reges
Last modified: Mon Nov 22 13:07:39 PST 2004