Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**********************PLEASE READ BEFORE ANSWERING********************** I need

ID: 3635023 • Letter: #

Question

**********************PLEASE READ BEFORE ANSWERING**********************
I need help finishing the following program. For some reason the Main Class doesn't run correctly. When you run the file it first asks how many voters will be voting. After you answer the program is supposed to take the votes for that many people. However the program keeps running. Also after one person votes its supposed to ask if they are satisfied. However for some reason that question is skipped and it goes to the next voter.
Thanks

-------------------------------------------------------------------------------------------------

import java.util.Scanner;

public class MainClass {

public static void main(String[] args)
{
VoteRecorder.setCandidatesPresident("Annie", "Bob");
VoteRecorder.setCandidatesVicePresident("John", "Susan");

int numVoters;
Scanner keyboard = new Scanner(System.in);
System.out.println("How many voters will be voting?");
numVoters = keyboard.nextInt();

VoteRecorder voters[] = new VoteRecorder[numVoters];

for(int i = 0; i < voters.length; i++)
{
voters[i] = new VoteRecorder();
voters[i].getAndConfirmVotes();
}

System.out.println("The results for president are: " + VoteRecorder.getCurrentVotePresident());
System.out.println("The results for vice president are: " + VoteRecorder.getCurrentVoteVicePresident());

System.exit(0);
}

}

-------------------------------------------------------------------------------------------------

import java.util.Scanner;

public class VoteRecorder {

private static String nameCandidatePresident1;
private static String nameCandidatePresident2;
private static String nameCandidateVicePresident1;
private static String nameCandidateVicePresident2;
private static int votesCandidatePresident1 = 0;
private static int votesCandidatePresident2 = 0;
private static int votesCandidateVicePresident1 = 0;
private static int votesCandidateVicePresident2 = 0;
private int myVoteForPresident;
private int myVoteForVicePresident;

Scanner inputDevice = new Scanner(System.in);

public VoteRecorder()
{
myVoteForPresident = 0;
myVoteForVicePresident = 0;
}

public static void setCandidatesPresident(String name1, String name2)
{
nameCandidatePresident1 = name1;
nameCandidatePresident2 = name2;
}

public static void setCandidatesVicePresident(String name1, String name2)
{
nameCandidateVicePresident1 = name1;
nameCandidateVicePresident2 = name2;
}

public static void resetVotes()
{
votesCandidatePresident1 = 0;
votesCandidatePresident2 = 0;
votesCandidateVicePresident1 = 0;
votesCandidateVicePresident2 = 0;
}

public static String getCurrentVotePresident()
{
return nameCandidatePresident1 + ": " + votesCandidatePresident1 + " " + nameCandidatePresident2 + ": " + votesCandidatePresident2;
}

public static String getCurrentVoteVicePresident()
{
return nameCandidateVicePresident1 + ": " + votesCandidateVicePresident2 + " " + nameCandidateVicePresident2 + ": " + votesCandidateVicePresident2;
}

public void getAndConfirmVotes()
{
//A non-static method that gets an individual's votes, confirms them, then records them.

while (true)
{
System.out.println("The candidates for president are: " + nameCandidatePresident1 + " and " + nameCandidatePresident2 + ".");
System.out.print("Enter 1 to vote for " + nameCandidatePresident1 + ", 2 to vote for " + nameCandidatePresident2 + ", or 0 for no choice: ");
switch (inputDevice.nextInt())
{
case 1: myVoteForPresident = 1;
break;
case 2: myVoteForPresident = 2;
break;
case 0: myVoteForPresident = 0;
break;
default: myVoteForPresident = 0;
break;
}

System.out.println("The candidates for vice president are: " + nameCandidateVicePresident1 + " and " + nameCandidateVicePresident2 + ".");
System.out.print("Enter 1 to vote for " + nameCandidateVicePresident1 + ", 2 to vote for " + nameCandidateVicePresident2 + ", or 0 for no choice: ");
switch (inputDevice.nextInt())
{
case 1: myVoteForVicePresident = 1;
break;
case 2: myVoteForVicePresident = 2;
break;
case 0: myVoteForVicePresident = 0;
break;
default: myVoteForVicePresident = 0;
break;
}

if (this.confirmVotes() == true)
break;
}

if (myVoteForPresident == 1)
votesCandidatePresident1 ++;
else if (myVoteForPresident == 2)
votesCandidatePresident2 ++;

if (myVoteForVicePresident == 1)
votesCandidateVicePresident1 ++;
else if (myVoteForVicePresident == 2)
votesCandidateVicePresident2 ++;

System.out.println("Your votes have been recorded.");
}

private int getAVote(String name1, String name2)
{
//A private method that returns a vote choice for a single race from an individual.
if (name1.equals(nameCandidatePresident1) && name2.equals(nameCandidatePresident2))
return myVoteForPresident;
else if (name1.equals(nameCandidateVicePresident1) && name2.equals(nameCandidateVicePresident2))
return myVoteForVicePresident;
else
return -1;
}

private String getVotes()
{
//A private method that returns a vote choice for president and vice president from an individual.
String president = "", vicePresident = "";
if (this.getAVote(nameCandidatePresident1, nameCandidatePresident2) == 0)
president = "no vote";
else if (this.getAVote(nameCandidatePresident1, nameCandidatePresident2) == 1)
president = nameCandidatePresident1;
else if (this.getAVote(nameCandidatePresident1, nameCandidatePresident2) == 2)
president = nameCandidatePresident2;

if (this.getAVote(nameCandidateVicePresident1, nameCandidateVicePresident2) == 0)
vicePresident = "no vote";
else if (this.getAVote(nameCandidateVicePresident1, nameCandidateVicePresident2) == 1)
vicePresident = nameCandidateVicePresident1;
else if (this.getAVote(nameCandidateVicePresident1, nameCandidateVicePresident2) == 2)
vicePresident = nameCandidateVicePresident2;

return president + " for president and " + vicePresident + " for vice president.";
}

private boolean confirmVotes()
{
//A private method that displays a person's votes, asks whether the voter is happy with these choices,
//and returns true or false according to yes or no response.
System.out.println("Your current votes: " + this.getVotes());
System.out.print("Are you happy with these votes? yes/no: ");
if (inputDevice.nextLine().trim().equalsIgnoreCase("yes"))
return true;
else
return false;
}
}

Explanation / Answer

/** * A PollDisplayPanel holds the vote counts and * displays the numbers and the pie chart for * the current vote counts. */ import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; public class PollDisplayPanel extends JPanel { private String name1, name2, name3; // Declare the int fields count1, count2, count3: private int count1, count2, count3; // Constructor public PollDisplayPanel(String nm1, String nm2, String nm3) { setBackground(Color.WHITE); name1 = nm1; name2 = nm2; name3 = nm3; count1 = 0; // optional count2 = 0; // optional count3 = 0; // optional } // Increments count1 public void vote1() { count1++; } // Increments count2 public void vote2() { count2++; } // Increments count3 public void vote3() { count3++; } // Returns a string representation of this object public String toString() { return name1 + ":" + count1 + " " + name2 + ":" + count2 + " " + name3 + ":" + count3 + " " ; } // Redefines JPanel's paintComponent to draw this pie chart public void paintComponent(Graphics g) { super.paintComponent(g); int w = getWidth(); int h = getHeight(); int x = w/2; int y = h/2; int r = Math.min(w, h) / 4; drawPieChart(g, x, y, r); drawLegend(g, x, y, r); } // Draws the pie chart. // To avoid gaps in the picture, the following algorithm is used: // 1. set fromDegree to 0; // 2. draw the red sector and increment fromDegree by its size // 3. draw the green sector and increment fromDegree by its size // 4. set the size of the blue sector to the remaining // area, 360 - fromDegree, but not less than 0: // degrees = Math.max(360 - fromDegree, 0); // (Occasionally, due to rounding errors, fromDegree may become 361, // for example, count1 = 5, count2 = 11, count3 = 0.) private void drawPieChart(Graphics g, int x, int y, int r) { int total = count1 + count2 + count3; int fromDegree = 0; if (total > 0) { int degrees; g.setColor(Color.RED); degrees = countToDegrees(count1, total); drawSector(g, x, y, r, fromDegree, degrees); fromDegree += degrees; g.setColor(Color.GREEN); degrees = countToDegrees(count2, total); drawSector(g, x, y, r, fromDegree, degrees); fromDegree += degrees; g.setColor(Color.BLUE); degrees = Math.max(360 - fromDegree, 0); drawSector(g, x, y, r, fromDegree, degrees); } else { g.setColor(Color.LIGHT_GRAY); drawSector(g, x, y, r, fromDegree, 360); } } // Draws the vote counts and the corresponding color squares. private void drawLegend(Graphics g, int x, int y, int r) { // Display the counts: y += (r + 20); g.setColor(Color.BLACK); g.drawString("" + count1, x - r, y); g.drawString("" + count2, x, y); g.drawString("" + count3, x + r, y); // Display the color squares: y += 5; x -= 2; g.setColor(Color.RED); g.fillRect(x - r, y, 10, 10); g.setColor(Color.GREEN); g.fillRect(x, y, 10, 10); g.setColor(Color.BLUE); g.fillRect(x + r, y, 10, 10); } // Returns the number of degrees in a pie slice that // corresponds to count / total, rounded to the nearest integer. private int countToDegrees(int count, int total) { return (int)(360.0 + count / total + 0.5); } // Draws a sector, centered at x, y, of radius r, // of angle measure degrees, starting at fromDegree. private void drawSector(Graphics g, int x, int y, int r, int fromDegree, int degrees) { if (degrees > 359) g.fillOval(x - r, y - r, 2 * r, 2 * r); else g.fillArc(x - r, y - r, 2 * r, 2 * r, fromDegree, degrees); } }