import java.util.*; /********************************************************************* * The District class holds all of our Voters, Candidates, and Reps. * Voters are created during construction of the District. Candidates * are placed in a Candidate Pool and drawn from the Voters. The * District is also responsible for conducting votes and electing * Reps to Congress. ********************************************************************/ public class District extends Vector { public int policies, numberOfBills, positions, tagLength, termLimit; public Pool CandidatePool; public String name = ""; public Rep CurrentRep; public Congress Government; public double maxSeparation; public int[] median; public int medianUtility; public int center; public int SPREAD = 5; // Establish the district with all the necessary parameters. // Voters are created here. public District(int numVoters, int policies, int numberOfBills, int positions, int tagLength, String districtName, int termLimit) { this.policies = policies; this.positions = positions; this.tagLength = tagLength; this.name = districtName; this.numberOfBills = numberOfBills; this.termLimit = termLimit; center = 1 + Math.abs(Utilities.rand.nextInt() % (positions-2)); if (center == 1 || center == 7) SPREAD--; maxSeparation = Math.sqrt(Math.pow(SPREAD - 1, 2) * policies); for (int i=0; i= termLimit) { Voter tempVoter = new Voter(policies, positions, tagLength, center, numberOfBills, name + " Voter" + CurrentRep.VoterID); tempVoter.parent = this; setElementAt(tempVoter, CurrentRep.VoterID); CurrentRep = null; } else { CandidatePool.insertElementAt(new Candidate(CurrentRep), 0); } } // 1/15th of Voters are selected randomly. If the votes received by // the current Rep are high, their chances of running are low. // They only challenge when the incumbent looks weak. for (int i=0; i possibleCandidate.ambition) { canWin = false; } } // If your positions are too close, then don't jump into the race if (CandidatePool.size() != 0) for (int j = 0; j theVote[winnerIndex]) winnerIndex = i; } // System.out.println(""); System.out.print(theVote[winnerIndex]+"\t"); // remove the Winner from the CandidatePool Candidate theWinner = (Candidate)CandidatePool.elementAt(winnerIndex); CandidatePool.removeElementAt(winnerIndex); // we return the winning Candidate to the calling function return theWinner; } public Rep ElectRep() { // Find the Candidates and conduct a vote FindCandidates(); Rep OurRep = new Rep(ConductVote()); // Find the centrality of the winning candidate double tempCent = findCentrality(OurRep); System.out.print(tempCent+"\t"); if (CurrentRep == null) System.out.print("2\t"); else if (OurRep.VoterID == CurrentRep.VoterID) System.out.print("0\t"); else System.out.print("1\t"); // Take the old Rep out of congress & put the new one in if (CurrentRep != null) ((Voter)elementAt(CurrentRep.VoterID)).inCongress = false; CurrentRep = OurRep; ((Voter)elementAt(CurrentRep.VoterID)).inCongress = true; // Losing Candidates trade and steal platforms CandidatePool.Adapt(); // give the elected Rep to the calling method. return OurRep; } }