Update again:
Vote (FINISHED):
/**
* Represents a single vote and it's preferences
*
* Thomas Bruining
* v1.0
*/
import java.lang.String;
public class Vote
{
String vote;
//Constructor
public Vote(String marks){
this.vote = marks;
}
//Methods
public String getPrefs(){
return vote;
}
//Retuns true if the vote is formal< based on the fact that it is a candidate(non duplicate)
public boolean isFormal(String candidates){
if(vote.length() == candidates.length()){
if(vote == candidates){
return true;
}else{
for(int i = 0; i<= vote.length(); i++){
for(int j = 1; j < vote.length(); j++){
char[] voteA = vote.toCharArray();
if(voteA[i] == voteA[j]){
return false;
}else{
char[] candA = candidates.toCharArray();
for(int t = 0; t < vote.length(); t++){
if(candA[t] == voteA[t]){
return false;
}else{
return true;
}
}
}
}
}
}
}else return false;
return true;
}
//Deletes loser from sequence of preferences
public void delete(char loser){
char[] voteA = vote.toCharArray();
for(int i = 0; i < vote.length(); i++){
if(voteA[i] == loser){
for(int j = i; j < vote.length(); j++){
if(j +1 != vote.length()){
voteA[j] = voteA[j+1];
}
}
}
}
voteA[vote.length() - 1] = ' ';
vote = new String(voteA);
}
//returns the preffered surviving candidate
public char preferred(String losers){
char[] losersA = losers.toCharArray();
for(int i = 0; i < losers.length(); i++){
if(vote.charAt(0) == losersA[i]){
char[] voteA = vote.toCharArray();
for(int n = 0; n < vote.length(); n++){
for(int j = n; j < vote.length(); j++){
if(j +1 != vote.length()){
voteA[j] = voteA[j+1];
}
}
}
voteA[vote.length() - 1] = ' ';
vote = new String(voteA);
}
}
return vote.charAt(0);
}
}
Candidate: (FINISHED APART FROM FINAL METHOD -- <<<REALLY NEED HELP)
/**
* Represents a single candidate and their votes
*
* Thomas Bruining
* v1.0
*/
public class Candidate{
char c;
int v = 0;
Vote[] votes;
public Candidate(char name, int n){
c = name;
votes = new Vote[n];
v = n;
}
public char getName(){
return c;
}
public int getCount(){
return v;
}
public Vote[] getVotes(){
return votes;
}
public boolean isWinner(int n){
if(v >= n/2 + 1){return true;}
else return false;}
public void getVotes(Vote[] papers, int n, String losers){
for(int i = 0; i <= n; i++){
if(papers[i].preferred(losers) == c){
v++;
}
}
}
}
public void getVotes(Vote[] papers, int n, String losers){
I'm lost as to how to do this. Any help is greatly appreciated.
Link:
Java Programming (CITS1200)
Election: (Unfinished and relatively unstarted <<NEED HELP)
/**
* Represents the election process
*
* Thomas Bruining
* v1.0
*/
public class Election{
int NumVotes;
int NumCand;
int formal;
String canfile;
String votefile;
FileIO votes;
FileIO cands;
public Election(String candidatesFile, String votesFile){
FileIO cand = new FileIO(candidatesFile);
FileIO tempvotes = new FileIO(votesFile);
canfile = candidatesFile;
votefile = votesFile;
NumCand = cand.lines[0].length();
NumVotes = tempvotes.noOfLines;
votes = tempvotes;
cands = cand;
}
public int getNoOfCandidates(){
return NumCand;
}
public int getNoOfVotes(){
}
public String getLosers(){
return null;
}
public void eliminateInformalVotes(String names, Vote[] votes){
}
public char winner(){
}
public Candidate findAndDeleteLoser(){
}
public void nextRound(){
FileIO tempcand = new FileIO(canfile);
for (int i = 0; i < tempcand.lines.length; i++){
Candidate temp = new Candidate(tempcand.lines[i], getNoOfVotes);
if (temp.isWinner == true){System.out.println("The winner is" + tempcand.lines[i]);}
}
//public void displayCount(){
//}
}}
FileIO: (Supplied for project - Needs to be incorporated into the Election class)
/*
* Reads a file into an array of strings
*
* Simple create a FileIO object with a valid file name,
* and it will read the file into lines, and set noOfLines
*
* e.g. reading the file f.txt containing "abc\nde\n\ngh\n" will return an object with
* file = "f.txt"
* noOfLines = 4
* lines = {"abc", "de", "", "gh"}
*
* Lyndon While, 2009
*/
import java.io.*;
class FileIO
{
public String file;
public String[] lines;
public int noOfLines; // lines and nooflines will be consistent
public FileIO (String f)
{file = f;
try {// Open the file
FileInputStream fstream = new FileInputStream(file);
// Convert fstream to a DataInputStream
BufferedReader in = new BufferedReader(new InputStreamReader(fstream));
// This will usually be way too big...
String[] toomanylines = new String[10000];
// Read lines while they keep coming
while (in.ready())
{toomanylines[noOfLines] = in.readLine(); noOfLines++;}
// Close the data stream
in.close();
// set up lines with the right size
lines = new String[noOfLines];
for (int i = 0; i < noOfLines; i++) lines[i] = toomanylines[i];
}
catch (Exception e) {System.err.println("File input error");}
}
}
Thanks for any help! I need it ASAP. This is worth a large percentage of my course, and I'd rather get a semi-acceptable mark for it to get me out of the depths for my exam.
Thankyou in advance!!! Please help me get Candidate finished first.