import java.util.Iterator; /* An instance of this class represents (the state of) a game of Taxman. ** ** Taxman is a solitaire game in which the player tries to "earn" more ** income than (s)he "pays" in taxes. For some positive integer n chosen ** at the beginning of the game, each positive integer in the range [1..n] ** eventually is earned as income or is paid in taxes. ** ** The term "active" describes any number that has not yet been earned as ** income or paid in taxes. The game begins with all the numbers (i.e., ** 1 through n) being active. On each turn, the player must choose an ** active number k that has at least one active proper divisor. As a ** result of that choice, k is credited to the player as income and all ** its active proper divisors are paid as taxes. All these proper divisors, ** as well as k itself, become inactive. ** ** At such time that there remain no active numbers that have an active ** proper divisor, all remaining active numbers are paid as residual taxes ** and the game comes to an end. ** ** The player's goal is to accumulate more in income than is paid in taxes. ** ** Authors: R. McCloskey and < STUDENT's NAME > ** Collaborated with: ... ** Known Defects: ... */ public class Taxman { // instance variables // ------------------ private SetIntRange activeSet; // set of active #'s private SetIntRange activeDivSet; // set of active divisors of active #'s private SetIntRange incomeSet; // set of #'s claimed as income private SetIntRange taxesSet; // set of #'s paid as taxes private SetIntRange newTaxes; // set of taxes paid most recently private int incomeTotal; // sum of values in incomeSet private int taxesTotal; // sum of values in taxesSet // constructor // ----------- /* Initializes this game to one in which the set of active numbers ** is [1..n] and no income or taxes have been earned/paid yet. ** pre: n > 0. */ public Taxman(int n) { activeSet = new SetIntRange(1,n,false); // < REST OF INITIALIZATIONS ARE MISSING > } // observers // --------- /* Reports the total income earned (so far). */ public int incomeEarned() { return incomeTotal; } /* Reports the total taxes paid (so far). */ public int taxesPaid() { return taxesTotal; } /* Reports whether or not k may be chosen by the player as an income, ** which requires that it be active and have at least one proper divisor ** that is also active. */ public boolean isLegalChoice(int k) { return false; // STUB } /* Reports whether the player has any legal choices of income amounts. */ public boolean legalChoicesExist() { return false; // STUB } /* Returns a string showing the numbers that are active. */ public String imageOfActives() { return activeSet.toString(); } /* Returns a string showing the numbers that have been earned as income. */ public String imageOfIncome() { return incomeSet.toString(); } /* Returns a string showing the numbers that have been paid as taxes. */ public String imageOfTaxes() { return taxesSet.toString(); } /* Returns a string showing the numbers that were paid in taxes as ** a result of the most recent choice of income. */ public String imageOfNewTaxes() { return newTaxes.toString(); } /* Returns a string showing the members of each of three sets: ** the set of active numbers, the set of numbers earned as income, ** and the set of numbers paid as taxes. */ public String toString() { return "Income: " + imageOfIncome() + '\n' + "Taxes : " + imageOfTaxes() + '\n' + "Active: " + imageOfActives() + '\n' + "OpCntr: " + SetIntRange.opCntr; } // mutators // -------- /* Pays the remaining active numbers to the taxman (intended to be done ** when no more legal choices exist). */ public void payResidualTaxes() { taxesSet.absorb(activeSet); taxesTotal = taxesTotal + sumOf(activeSet); activeSet.setToEmpty(); activeDivSet.setToEmpty(); } /* Updates the state of the game to reflect that the player has chosen ** k as income. ** pre: isLegalChoice(k) ** post: k has been added as income && ** newTaxes contains all heretofore active proper divisors of k && ** the members of newTaxes have been paid as taxes && ** k and all its proper divisors are no longer active */ public void makeChoice(int k) { // STUB } // private methods // --------------- /* Reports the sum of the members of the given set. */ private int sumOf(SetIntRange s) { Iterator iter = s.getIterator(); int result = 0; while (iter.hasNext()) { result = result + iter.next(); } return result; } /* Returns the set containing all active proper divisors of k. ** pre: k > 1 ** post: the set returned contains the active proper divisors of m, i.e., ** { i | 0 < i < k : activeSet.isMemberOf(i) && k%i = 0 } */ private SetIntRange activeProperDivisorsOf(int k) { return null; // STUB } }