Question & Answer: THIS PROGRAM NEEDS TO BE IN JAVA…..

THIS PROGRAM NEEDS TO BE IN JAVA.

Design and implement a program processes voting data of presidential election for the country of RichlandCollege where the said country consists of 4 states. At this time, there are 6 candidates are running for the position as seen in the candData.txt text file. The results of the votes are sent to you for processing, see sample in voteData.txt. It lists each candidate’s name and the votes they received by states. You should have a nicely formatted output of the election results with the winner’s name printed.

voteData.txt
Your program driver should generate a randomly generated voteData based on the given candidate names, random state selection ( 1 – 4 ), and random votes ( 0 – 50 ) per state.
You should generate 10,000 – 1,000,000 entries in the voteData.txt before processing the data. In your design ( UML/Flowchart, UML/pseudo code ) have a BigO analysis for your algorithms ( where it applies ) and justify your selection of algorithms based on speed and storage characteristics.

Candidate
This class implements a candidate as an object.

Methods ( besides standard methods, implement the specific methods especially their post condition requirements )

public void method_name_chsen_by_you(int state, int votes)
Method to set the votes of a candidate for a particular state.
Postcondition: The votes specified by the parameter votes are assigned to the state specified by the parameter state.

public void method_name_chsen_by_you(int state, int votes)
Method to update the votes of a candidate for a particular state.
Postcondition: The votes specified by the parameter votes are added to the state specified by the parameter state.

public boolean method_name_chsen_by_you(Candidate otherCan)
Method to determine if two candidates have the same name.
Postcondition: Returns true if the name of this candidate is the same as the name of the candidate specified by the parameter otherCan.

public int method_name_chsen_by_you(Candidate otherCan)
Method to compare the names of two candidates.
Postcondition: Returns 0 if the name of this candidate is the same as the name of otherCan; returns < 0 if the name of this candidate is less then the name of otherCan; returns > 0 if the name of this candidate is greater than the name of otherCan.

ElectionResult
This class contains methods for processing voting data for presidential election.

Methods ( besides standard methods, implement the specific methods especially their post condition requirements )

public static void method_name_chsen_by_you(Candidate[] list, int length)
Algorithm post condition: list objects are in ascending order.

public static int method_name_chsen_by_you(Candidate[] list, int length,Candidate searchItem)
Algorithm post condition: If searchItem is found in the list,it returns the location of searchItem; otherwise it returns -1.

To help you get started, here is a basic diagram of classes that you should design and implement.

Candidate ElectionResult TestProgElectionResult

voteData.txt
Mia 2 34
Mickey 1 56
Donald 2 56
Mia 1 78
Danny 4 29
Ashley 4 78
Mickey 2 63
Donald 1 23
Peter 2 56
Danny 1 25
Peter 4 23
Danny 4 12
Ashley 4 82
Mickey 3 67
Donald 2 67
Mia 3 67
Ashley 1 23
Mia 1 56
Donald 2 35
Peter 1 27
Danny 2 34
Peter 4 23
Mickey 4 89
Peter 1 23
Danny 3 89
Mia 3 89
Peter 1 67
Danny 2 37
Ashley 2 89
Mia 2 78
Donald 1 87
Peter 1 90
Danny 4 56

candData.txt
Mia
Mickey
Donald
Peter
Danny
Ashley

Show transcribed image textCandidate ElectionResult TestProgElectionResult

Expert Answer

 

Solution===============================================

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

class Candidate implements Comparable<Candidate>{
private String name;
private int stateWiseVotes[];
private int totalVotes;

public Candidate(String name,int numOfStates){
this.name=name;
stateWiseVotes=new int[numOfStates];
for(int i=0;i<numOfStates;i++){
stateWiseVotes[i]=0;
}
totalVotes=0;
}

public void calculateTotalVotes(){
for(int i=0;i<stateWiseVotes.length;i++){
totalVotes+=stateWiseVotes[i];
}
}

public void setVotesOfState(int votes, int stateId){
stateWiseVotes[stateId]=votes;
}

public void updateVotesOfState(int votes, int stateId){
stateWiseVotes[stateId]+=votes;
}

public boolean doWeHaveSameNames(Candidate c)
{
if(name.equals(c)){
return true;
}

return false;
}

public int compareNames(Candidate c){
return name.compareToIgnoreCase(c.name);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int[] getStateWiseVotes() {
return stateWiseVotes;
}

public void setStateWiseVotes(int[] stateWiseVotes) {
this.stateWiseVotes = stateWiseVotes;
}

public int getTotalVotes() {
return totalVotes;
}

public void setTotalVotes(int totalVotes) {
this.totalVotes = totalVotes;
}

@Override
public int compareTo(Candidate c) {
return compareNames(c);
}

}

class ElectionResult{

private Candidate candidates[];

public ElectionResult(String[] candidates, int numberOfStates){
this.candidates=new Candidate[candidates.length];
for(int i=0;i<candidates.length;i++){
Candidate candidate = new Candidate(candidates[i],numberOfStates);
this.candidates[i]=candidate;
}
}

void processResults() throws FileNotFoundException{
Scanner in = new Scanner(new File(“voteData.txt”));
while(in.hasNextLine()){

String data[] = in.nextLine().split(” “);
Candidate cand=candidates[getCandidateIndex(data[0].trim())];
int state=Integer.parseInt(data[1]);
int votes=Integer.parseInt(data[2]);

cand.updateVotesOfState(votes, state-1);
}

for(Candidate cand:candidates){
cand.calculateTotalVotes();
}

in.close();
}

public static void sortCandidates(Candidate[] list,int length){
Arrays.sort(list);
}

public int getCandidateIndex(Candidate[] list, int length,Candidate searchItem){
for(int i=0;i<length;i++){
if(list[i]==searchItem){
return i;
}
}
return -1;
}

public int getCandidateIndex(String searchItem){
for(int i=0;i<candidates.length;i++){
if(candidates[i].getName().equals(searchItem)){
return i;
}
}
return -1;
}

public void dispalyResults() {
System.out.println(“============Election Results================”);
System.out.print(“Name        “);
for(int i=0;i<4;i++){
System.out.print(“State#”+(i+1)+”    “);
}
System.out.println(“Total”);

sortCandidates(candidates,candidates.length);
int max=-1;
Candidate winner= new Candidate(“dummy”,4);

for(Candidate cand:candidates){
System.out.format(“%10s”,cand.getName());

for(int i=0;i<cand.getStateWiseVotes().length;i++){
System.out.format(“%10d”,cand.getStateWiseVotes()[i]);
}

System.out.format(“%10dn”,cand.getTotalVotes());
if(cand.getTotalVotes()>max){
max=cand.getTotalVotes();
winner=cand;
}
}
System.out.println(“nnWinner is: “+winner.getName()+” with Votes: “+winner.getTotalVotes());
}

}

public class ElectionDriver {

private ElectionResult processor;

public ElectionDriver(String[] candidates, int numberOfStates){
processor=new ElectionResult(candidates, numberOfStates);
}

void generateBogusElectionData(String[] candidates, int numOfStates, int dataSize) throws IOException{
Random rand = new Random();
FileWriter fw=new FileWriter(“voteData.txt”);

for(int i=0;i<dataSize;i++){
String candidate=candidates[rand.nextInt(candidates.length)];
int state=rand.nextInt(numOfStates)+1;

int votes = rand.nextInt(50);
String line = candidate+” “+state+” “+votes+”n”;
fw.append(line);
}

fw.close();
}

public void drive() throws Exception{
processor.processResults();
processor.dispalyResults();
}

public static void main(String[] args) throws Exception {
Scanner in = new Scanner(new File(“candData.txt”));
ArrayList<String> cands = new ArrayList<>();

while(in.hasNextLine()){
cands.add(in.nextLine().trim());
}
in.close();

String[] cds=new String[cands.size()];
for(int i=0;i<cands.size();i++){
cds[i]=cands.get(i);
}

ElectionDriver driver =new ElectionDriver(cds,4);
driver.generateBogusElectionData(cds, 4, 50);
driver.drive();
}

}

Output=============================================================

Question & Answer: THIS PROGRAM NEEDS TO BE IN JAVA..... 1

Still stressed from student homework?
Get quality assistance from academic writers!