/** * Returns true if and only if all voters in set vote true. Each voter must be called exactly * once. * * @param voters * @return Conjunction of all the votes */ public boolean census(Set<Voter> voters) { boolean census = true; for (Voter voter : voters) { census = census & voter.vote(); } return census; }
public Histogram[][] computeHistograms(PixelGradientVector[][] pgv) { int ySize = pgv.length; // number of rows int xSize = pgv[0].length; // number of coloumns int histogram_width = xSize / cell_width; int histogram_height = ySize / cell_height; Histogram histogram[][] = new Histogram[histogram_height][histogram_width]; // Getting the cells: // - the step is "this.cellSize" // - right and bottom border pixels may be excluded for (int y = cell_height; y <= ySize; y += cell_height) { for (int x = cell_width; x <= xSize; x += cell_width) { // Foreach cell, create Histogram exploring all the pixels int xPos = x / cell_width - 1; int yPos = y / cell_height - 1; histogram[yPos][xPos] = new Histogram(bins, cell_width, cell_height, signed); for (int i = x - cell_width; i < x; i++) { for (int j = y - cell_height; j < y; j++) { float magnitude = pgv[j][i].getMagnitude(); float vote = voter.vote(magnitude); float angle = pgv[j][i].getAngle(); int bin = getBin(angle, bins, signed); histogram[yPos][xPos].addVote(vote, bin); } } } } return histogram; }
public Candidate elect(Populace populace, Vector<Candidate> ballot) { int[][] votes = new int[ballot.size()][ballot.size()]; for (int i = 0; i < votes.length; i++) { Arrays.fill(votes[i], 0); } int n = populace.getSize(); for (int i = 0; i < n; i++) { Voter voter = populace.getRandomVoter(); double[] vote = voter.vote(ballot); count(votes, vote); } int winner = 0; for (int round = 0; round < votes.length; round++) { winner = Utilities.max(votes[round]); if (votes[round][winner] * 2 >= n || round == votes.length - 1) break; for (int i = 0; i < votes[round].length; i++) { votes[round + 1][i] += votes[round][i]; } } return ballot.get(winner); }
public boolean voting(ArrayList<Voter> voters) throws Exception { int notNullVotes = 0; int nullVotes = 0; boolean result = true; if (Evaluator.evaluate("VotersNull", voters, Operator.NOTEQUAL, null)) { // if(voters != null) { for (Voter v : voters) { if (Evaluator.evaluate("VoteNull", v, Operator.NOTEQUAL, null)) { // if (v != null) { if (Evaluator.evaluate("VoteValue", v.vote(), Operator.EQUAL, false)) { // if (v.vote() == false) { result = false; } notNullVotes += 1; } else { nullVotes += 1; } } } else { return true; } if (Evaluator.evaluate( "DubbleVote", notNullVotes + nullVotes, Operator.BIGGER, voters.size())) { // if (notNullVotes + nullVotes > voters.size()) { throw new Exception("Valid voters has voted more than once."); } else { if (Evaluator.evaluate( "NotVoted", notNullVotes + nullVotes, Operator.SMALLER, voters.size())) { // if (notNullVotes + nullVotes < voters.size()) { throw new Exception("Valid voters has not voted."); } else { return result; } } }
// This is where all the action happens. If an action is performed (button press for example), it // goes through this method // that takes an ActionEvent, which is created at the time of the action. public void actionPerformed(ActionEvent e) { // Try to cast the ActionEvent source as JToggleButton (if successful, the button is a ballot // choice) try { JToggleButton theButton = (JToggleButton) e.getSource(); String buttonText = theButton.getText(); if (theButton.isSelected()) { theButton.setForeground(Color.RED); switchButton = true; switchButtonText = buttonText; updateButtons( buttonText); // switches this button's respective vote position to 1 (true), meaning it // is selected } else if (!theButton.isSelected()) { theButton.setForeground(Color.BLACK); unvoteButton = true; unvoteButtonText = buttonText; } // If the program can't cast the source to a JToggleButton, cast it as a JButton, which is // 100% what the source will be at this point. // This button will be one of two things: the login button or the cast vote button. } catch (Exception exception) { JButton theButton = (JButton) e.getSource(); String buttonText = theButton.getText(); // If the user hits the Login button, check if the ID is valid. // If the ID is not valid, don't let them vote. However, if it is valid, disable login and // enable everything else. if (buttonText.equals("Login")) { String voterID = JOptionPane.showInputDialog(theButton, "Enter your voter ID: ", "Login", 3); FileIO votersFile = new FileIO("voters.txt"); ArrayList<Voter> voters = votersFile.createVoters(); if (votersFile.checkValidID(voterID)) { Voter theVoter = voters.get(votersFile.getVoterIndex(voterID)); if (theVoter.getVotedStatus().equals("false")) { setVoterID(voterID); } else { JOptionPane.showMessageDialog( theButton, "You already voted " + theVoter.getVoterName() + "!"); } } else { JOptionPane.showMessageDialog(theButton, "Invalid ID!", "Error", 0); } // If the user hits the cast vote button, update the respective ballot files and disable // every button except // for the login button. } else if (buttonText.equals("Cast vote")) { int confirm = JOptionPane.showConfirmDialog(theButton, "Are you sure?"); FileIO votersFile = new FileIO("voters.txt"); ArrayList<Voter> voters = votersFile.createVoters(); Voter theVoter = voters.get(votersFile.getVoterIndex(voterID)); if (confirm == 0) { JOptionPane.showMessageDialog(theButton, "Thanks for voting!"); updateBallots(); votersFile.updateVoterFile(theVoter.getVoterID()); setVoterID("-1"); votersFile.initializeVoters(); } } } }