Tuesday, November 09, 2004

Rigging an Election in C++

Here's some code for a simple, two cantidate election program in Java. I'm too lazy to include file writes.

//Cantidate.java
//---------------
public class Cantidate{
private int numberOfVotes;
private String name;

//constructors
//generic, blank constructor
Cantidate(){
numberOfVotes = 0;
name = "Unfilled";
}

//Fills in the cantidate's name with that set as a parameter.
Cantidate(string newName){
numberOfVotes = 0;
name = "Unfilled";
}

//this adds a vote to the numberOfVotes
public VoteFor(){
numberOfVotes++;
}

}

//Vote.java
//---------------

import cs1.Keyboard; //this is a input class we use in class
public class Vote{

public static void main(String[] args){

Cantidate Kerry = new Cantidate("John Kerry"); //creates Kerry, a variable of type Cantidate
Cantidate Bush = new Cantidate("George W. Bush"); //creates Bush, a variable of type Cantidate
int numberOfVoters = 10000;
int vote;

for (int timesVoted = 0; timesVoted <= numberOfVoters; timesVoted++){
System.out.println("Press 1 for George W. Bush : Press 2 for John Kerry");
vote = Keyboard.readInt();

if (vote == 1) Bush.voteFor();
else if (vote == 2) Bush.voteFor();
else System.out.println("Error!");
}

}

}


Now, that program is rather simple. I'm too lazy to right now, but some minor changes would throw off the whole election.

0 Comments:

Post a Comment

<< Home

-->