So, I have a project and the task is stated this way :
You need to create a class called Candidate which stores the last name of the a person running in an election, and the number of votes they got. You should also have methods for accessing each of the fields. At this point, there is no need for mutators on these fields, since they should not change.
Then create a class called Election which has an ArrayList of Candidate objects. You should have methods to1) print the list of candidates and their number of votes, 2) print the number of votes a single candidate got, given his name, 3) compute the total number of votes, and 4) print the list of the candidates, the number of votes they got, and percentage of the total vote they got, along with the winner.
Example:
Candidate Votes Received % of Votes
Johnson 5000 25.91
Miller 4000 20.73
Duffy 6000 31.09
Robinson 2500 12.95
Ashtony 1800 9.33
Total 19300
The winner of the election is Duffy.
Feel free to use this as your test data
This is the code I have so far in BlueJ
import java.util.*;
public class Candidate
{
public String lastName;
public int votes;
public Candidate(String surname, int voteNumber)
{
lastName = surname;
votes = voteNumber;
}
public String getCandidateAll()
{
return "' " + lastName + " has " + votes + " votes " + " '";
}
public int getVotes()
{
return votes;
}
public String getCandidateName()
{
return lastName;
}
}
What I don't understand if I am supposed to pull all second names from the first class into the ArrayList or do something else. Also, the professor mentioned that in second class iterator should be used. Please, someone, help and guide throughout the process of what to do next. Thanks in advance
Having a minimal class Candidate
of just
public String lastName;
public int votes;
public Candidate(String surname, int voteNumber)
{
lastName = surname;
votes = voteNumber;
}
is fine.
What you also need is another class
which has a main
method in which you can get user entry and also to create a List
of these Candidates
Once you have the ArrayList
you can loop (iterate) through to sum
or search
etc.