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.

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: THIS PROGRAM NEEDS TO BE IN JAVA…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

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=============================================================

Grand Paper Writers
Order NOW For A 10% Discount!
Pages (550 words)
Approximate price: -

Our Advantages

Plagiarism Free Papers

All papers are written by the best professional writers to ensure 100% originality. We always provide plagiarism reports whenever we deliver completed papers.

Free Revisions

All papers by Grand Paper Writers are completed and submitted on time. This timely delivery of papers gives you time to go through the paper before the official deadline.

Title-page

As an additional service, we will provide a title page that precedes the contents of your paper. Here, you will provide your personal details.

Bibliography

We also ensure that we provide an extra page for the references or bibliographies following referencing rules.

Originality & Security

At Grandpaperwriters.com, we guarantee students for the provision of security and original work. All your personal information is handled with confidentiality and is not shared with third parties. Additionally, we ensure that we provide original content with accompanying plagiarism reports to show originality.

24/7 Customer Support

Our customer support team is always available 24/7 to provide instant responses to any queries raised by students.

Try it now!

Calculate the price of your order

We'll send you the first draft for approval by at
Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

Grandpaperwriters.com has the best professional essay writers for quality services.

Pricing

Flexible Pricing

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Communication

Admission Help & Client-Writer Contact

Grandpaperwriters.com provides an interactive portal where students can communicate directly to their writers.

Deadlines

Paper Submission

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Reviews

Customer Feedback

Grandpaperwriters.com appreciates feedback from our clients to help us improve the delivery of essay writing services. As such, we are constantly changing our policies to ensure maximum customer and writer satisfaction.