// package chapter6; /** * Title: Chapter 6, "Programming with Objects and Classes" * Description: Examples for Chapter 6 * Copyright: Copyright (c) 2000 * Company: Armstrong Atlantic State University * @author Y. Daniel Liang * @version 1.0 */ // TestCount.java: Count votes public class TestCountClass { public static void main(String[] args) { // Create Count object for each candidate Count count1 = new Count(); Count count2 = new Count(); // Count votes while (true) { System.out.print("Enter a vote: "); int vote = MyInput.readInt(); if (vote == 0) break; // End of the votes if (vote == 1) count1.increment(); if (vote == 2) count2.increment(); if (vote == -1) count1.decrement(); if (vote == -2) count2.decrement(); } System.out.println("The total number of candidates is " + Count.getNumOfCounts()); System.out.println("The votes for Candidate 1 is " + count1.getCount()); System.out.println("The votes for Candidate 1 is " + count2.getCount()); } } // Define the Count class class Count { private int count = 0; private static int numOfCounts = 0; /**Construct a count*/ public Count() { numOfCounts++; } /**Return count*/ public int getCount() { return count; } /**Set a new count*/ public void setCount(int count) { this.count = count; } /**Return number of count objects*/ public static int getNumOfCounts() { return numOfCounts; } /**Clear this count*/ public void clear() { count = 0; } /**Increment this count*/ public void increment() { count++; } /**Decrement this count*/ public void decrement() { count--; } }