MUST be in JAVA please!! code does not have to be complicated, just want to chec
ID: 3791638 • Letter: M
Question
MUST be in JAVA please!! code does not have to be complicated, just want to check and see if my own is correct!
brief comments would be appreciated to fully understand where I went right or wrong! thank youuuu so much!
class Voter
assuming I have already completed Candidates list candidates
a object of this class represents a single voter's selections. It is also knows what candidates are being voted on.
Fields to be used
CandidateList candidates . this repres. the candidates that are up for election, in a certain order.
int[] selection. this will store the ranks for each candidate, in the same order as it was found in candidates. Must be the same length as candidates, and store the rank values (1 through size of =candidates). A zero will represent the voter who hasn't yet given that candidate a ranking - this will only happen during the voter's decision making process, and it should never be discovered when counting votes (we will check against this when validating votes).
Methods to be used
public Voter(CandidateListing candidates) . This constructor will initialize the candidates list to the given value, and creates the selections array, but it will be filled with zeros and the choices will need to be recorded and this should be before the vote is completed.
public Voter(CandidateListing candidates, int[] selections) . initializes the instance variables with the given values. this will make the assumption that the lengths do in fact match, and it does not check that valid ranks are present in the selections.
public int indexOfTheRank(int ranking) . this will return at which index position the given rank is found in the selections.
****it has to throw a RuntimeException when the rank is not there.
public String getTheRankedCandidate(int ranking) . this will locate and find where that specific rank is, and it looks up that candidate, and returns the candidate string.
****it has to throw a RuntimeException when the rank is not there.
public record(int ranking, int index) . this will record the given rank at the indicated index.
****it has to throw a RuntimeException when the index is not valid.
****it has to throw a RuntimeException when the rank is not there.
public record(int ranking, String candidate) . this will record the given rank at the specific index for the given candidate.
****it has to throw a RuntimeException when the rank is not valid
****it has to throw a RuntimeException when the candidate is not there
public String greatestChoice(CandidateList options) . Who will recieve this vote when only the given options are allowed? maybe your favorite candidate has already been eliminated, but our 2nd favorite or 3drd choice is still available.
if no choice is there, which shouldn't be the case in a consistent election, this method will return null.
public CandidateList duplicateOfCandidateList() . returns a copy of the list of candidates.
public boolean confirmVote() . this will performs some internal check, and will return true when all checks are passed and false when they fail
1) there has to be a list of candidates there so it cannot be null.
2) there has to be an array of selections there, and must be same size as candidates list.
3) every selection has to be valid ranks, and Each Rank must appear exactly once!
@Override public String toString() . this returns a string representation that matches and pairs candidate strings with their specific rankings. also the order of candidates must be preserved, and order of selections always is tied to order of candidates.
@Override public boolean equivalence(Object another) . this will compare this vote to the other vote. But first must check if other object is even a vote; this deals with inheritance, or we can do the following as the initial check, and creation of an actual Vote variable to refer to the other:
Explanation / Answer
Hi,
Please see beow the java classes. Please comment for any queries/feedbacks.
Thanks.
CandidateListing.java
public class CandidateListing {
private String [] candidateList;
public CandidateListing() {
candidateList = new String[5];
}
public String[] getCandidateList() {
return candidateList;
}
public void setCandidateList(String[] candidateList) {
this.candidateList = candidateList;
}
}
Voter.java
public class Voter {
private CandidateListing candidates ;
int[] selection;
public Voter(CandidateListing candidates) {
this.candidates.setCandidateList(candidates.getCandidateList());
//creating the selections array
//it will be filled with zeros and the choices will need to be recorded before the vote is completed
this.selection= new int[5];
this.selection[0] = 0;
this.selection[1] = 0;
this.selection[2] = 0;
this.selection[3] = 0;
this.selection[4] = 0;
}
public Voter(CandidateListing candidates, int[] selections){
this.candidates.setCandidateList(candidates.getCandidateList());
//creating the selections array
//it will be filled with zeros and the choices will need to be recorded before the vote is completed
this.selection= new int[5];
this.selection[0] = selections[0];
this.selection[1] = selections[1];
this.selection[2] = selections[2];
this.selection[3] = selections[3];
this.selection[4] = selections[4];
}
public int indexOfTheRank(int ranking){
int index =-1 ;
try {
for(int i=0; i<this.selection.length; i++){
//Checking at which index position the given Rank is in
if(ranking == this.selection[i]){
index = i;
}
}
if(index < 0){ //if rank is not present throw an exception
throw new Exception("Rank is Invalid!");
}
} catch (Exception e) {
e.printStackTrace();
}
return index;
}
public String getTheRankedCandidate(int ranking) {
String candidate = "";
try{
int indexOfCandidate = indexOfTheRank(ranking); //Calling indexOfTheRank() to get the index
candidate= this.candidates.getCandidateList()[indexOfCandidate]; //getting the candidate name
} catch (Exception e) {
e.printStackTrace();
}
return candidate;
}
public void record(int ranking, int index) {
try
{
if(index<0 || index >=this.selection.length){
throw new Exception("No candidate present for the index."); //throw exception if index is invalid
}
if(ranking <= 0 || ranking >this.selection.length){
throw new Exception("Invalid ranking"); //throw exception if ranking is invalid
}
for(int i=0;i<this.selection.length; i ++){ //looping through each selection element
if(index == i){ //if a match found for the given index, set the rank for that index
this.selection[i] = ranking;
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void record(int ranking, String candidate){
int indexForCandidate =-1;
try
{
for(int i=0;i<this.candidates.getCandidateList().length; i ++){ //looping through each candidateslist element
if(candidate.equalsIgnoreCase(this.candidates.getCandidateList()[i])){
indexForCandidate = i ;
}
}
record( ranking, indexForCandidate) ; //record(int ranking, int index) to record the ranking
}
catch(Exception e){
e.printStackTrace();
}
}
public String greatestChoice(CandidateListing options){
String preferredCandidate=null;
for(int i=0;i<this.candidates.getCandidateList().length; i ++){ //looping through each candidateslist element
if(null != (this.candidates.getCandidateList()[i])){
preferredCandidate = this.candidates.getCandidateList()[i]; //setting preffered candidate
}
}
return preferredCandidate;
}
public CandidateListing duplicateOfCandidateList(){
CandidateListing duplicateListing = new CandidateListing();
for(int i=0;i<this.candidates.getCandidateList().length; i ++){ //looping through each candidateslist element
if(null != (this.candidates.getCandidateList()[i])){
duplicateListing.getCandidateList() [i] = this.candidates.getCandidateList()[i]; //setting the duplicate candidates
}
}
return duplicateListing;
}
public boolean confirmVote(){
boolean retVal = true;
//there has to be a list of candidates there so it cannot be null.
if(null!=this.candidates.getCandidateList()){
// there has to be an array of selections there, and must be same size as candidates list.
if(null!=this.selection && this.candidates.getCandidateList().length == this.selection.length){
// every selection has to be valid ranks,
for(int i=0;i<this.selection.length; i ++){ //looping through each selection element
if(selection[i]<=0 || selection[i] >this.selection.length){
//and Each Rank must appear exactly once!
for(int j=i+1; j<selection.length;j++){
if(selection[i] == selection[j]){
retVal = false;
return retVal;
}
}
}
else{
retVal = false;
return retVal;
}
}
}
else{
retVal = false;
return retVal;
}
}
else{
retVal =false;
return retVal;
}
return retVal;
}
@Override
public String toString(){
String retval ="";
for(int i=0;i<this.candidates.getCandidateList().length; i ++){ //looping through each candidateslist element
retval = retval +" Candidate : "+this.candidates.getCandidateList()[i]+", Ranking : "+this.selection[i];
}
return retval;
}
//@Override
public boolean equivalence(Object another){
Voter voter = (Voter) another;
for(int i=0;i<this.candidates.getCandidateList().length; i ++){ //looping through each candidateslist element
if(this.candidates.getCandidateList()[i] == voter.getCandidates().getCandidateList()[i]
&& this.selection[i] == voter.getSelection()[i] ){
return true;
}
}
return false;
}
public CandidateListing getCandidates() {
return candidates;
}
public void setCandidates(CandidateListing candidates) {
this.candidates = candidates;
}
public int[] getSelection() {
return selection;
}
public void setSelection(int[] selection) {
this.selection = selection;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.