Computer Science 120
Introduction to Programming
Fall 2011, Siena College
FourVoting Demo
A working demo of FourVoting will appear below. Click inside the applet to interact with it.
FourVoting BlueJ Project
Click here to download a BlueJ project for FourVoting.
FourVoting Source Code
The Java source code for FourVoting is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; /* * Example FourVoting * * Example similar to one from Section 4.5 of the text demonstrating * a four-candidate voting system. * * Jim Teresco, Siena College, CSIS 120, Fall 2011 * * $Id: FourVoting.java 1516 2011-01-31 21:08:19Z terescoj $ */ public class FourVoting extends WindowController { private int countA = 0; // number of votes for A private int countB = 0; // number of votes for B private int countC = 0; // number of votes for C private int countD = 0; // number of votes for D private Text infoA; // display of votes for A private Text infoB; // display of votes for B private Text infoC; // display of votes for C private Text infoD; // display of votes for D private static final int TEXT_OFFSET = 25; // offset of text from corner private static final int TEXT_SIZE = 60; // we will compute once and reuse the midpoints of the canvas private double midX; private double midY; // Create displays with voting board and results public void begin() { // set these variables for convenience midX = canvas.getWidth()/2; midY = canvas.getHeight()/2; infoA = new Text("A: 0", TEXT_OFFSET, TEXT_OFFSET, canvas); infoB = new Text("B: 0", midX + TEXT_OFFSET, TEXT_OFFSET, canvas); infoC = new Text("C: 0", TEXT_OFFSET, midY + TEXT_OFFSET, canvas); infoD = new Text("D: 0", midX + TEXT_OFFSET, midY + TEXT_OFFSET, canvas); infoA.setFontSize(TEXT_SIZE); infoB.setFontSize(TEXT_SIZE); infoC.setFontSize(TEXT_SIZE); infoD.setFontSize(TEXT_SIZE); new Line(0, midY, canvas.getWidth(), midY, canvas ); new Line(midX, 0, midX, canvas.getHeight(), canvas ); } // Update votes and display vote counts public void onMouseClick( Location point ) { if ( point.getX() < midX && point.getY() < midY ) { // upper-left countA++; infoA.setText( "A: " + countA); } else if ( point.getX() >= midX && point.getY() < midY ) { // upper-right countB++; infoB.setText( "B: " + countB); } else if ( point.getX() < midX && point.getY() >= midY ) { // lower-left countC++; infoC.setText( "C: " + countC); } else { // lower-right countD++; infoD.setText( "D: " + countD); } } }