In: Computer Science
JAVA Program
Write a program that prompts the user for data until the user wishes to stop (you must have a while loop)
(You must read in at least 8 to 10 sets of voter data using dialog boxes)
The data to read in is:
You will then print the following statistics:
Design help with this assignment:
First, make sure you can read in all data. Then take one requirement at a time and print that result. For example, count the number of people for Trump and the number of people for Biden and print that out. Then move on to the next requirement.
Again, thanks in advance. I need to check my work with an expert's help before the following Tuesday. Virtual schooling leaves me no choice but to post on here to learn my mistakes. Please post the java code, and output, as well as a screenshot of both.
package election;
import javax.swing.JOptionPane;
public class Election {
public static void main(String[] args) {
// initializing varibales to zero
int no_of_male_voters = 0, no_of_female_voters = 0;
int no_of_voters = 0;
int no_of_trump_voters = 0, no_of_biden_voters = 0;
int no_of_democrats = 0, no_of_republicans =0 , no_of_others = 0;
int no_of_male_trump_voters = 0, no_of_female_trump_voters = 0;
int no_of_male_biden_voters = 0, no_of_female_biden_voters = 0;
int no_of_trump_democrat_voters = 0, no_of_biden_republican_voters = 0;
int trump_manage_economy = 0, biden_manage_economy = 0;
int trump_civil_unrest = 0, biden_civil_unrest = 0;
int trump_manage_corona = 0, biden_manage_corona = 0;
while(true)
{
// count the number of voters
no_of_voters ++;
// option for parties
Object[] options1 = {"Democrat", "Republican", "Other"};
int reg = JOptionPane.showOptionDialog(null,"Registration of the voter ", "Choose your Option !!",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options1,options1[0]);
if(reg == 0) {
no_of_democrats ++;
} else if(reg == 1){
no_of_republicans ++;
} else {
no_of_others ++;
}
// option for gender
Object[] options2 = {"Male","Female"};
int gen = JOptionPane.showOptionDialog(null,"Your geneder ", "Choose your opinion !!",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options2,options2[0]);
if(gen == 0) {
no_of_male_voters ++;
} else {
no_of_female_voters ++;
}
// option for president
Object[] options3 = {"Trump","Biden"};
int pres = JOptionPane.showOptionDialog(null,"Presidential candidate you choose ", "Choose your opinion !!",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options3,options3[0]);
// option checking for president
if(pres == 0) {
no_of_trump_voters ++;
} else {
no_of_biden_voters ++;
}
// male votesrs for trump & biden
if(pres == 0 && gen == 0) {
no_of_male_trump_voters ++;
}
if(pres == 1 && gen == 0) {
no_of_male_biden_voters ++;
}
// female votesrs for trump & biden
if(pres == 0 && gen == 1) {
no_of_female_trump_voters ++;
}
if(pres == 1 && gen == 1) {
no_of_female_biden_voters ++;
}
// democrats for trump
if(reg == 0 && pres == 0) {
no_of_trump_democrat_voters ++;
}
// republican for biden
if(reg == 1 && pres == 1) {
no_of_biden_republican_voters ++;
}
// manage economy option
int manage_economy = JOptionPane.showOptionDialog(null,"Which candidate has done better to manage the economy? ", "Choose your opinion !!",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options3,options3[0]);
if(pres == 0 && manage_economy == 0) {
trump_manage_economy ++;
}
if(pres == 1 && manage_economy == 1) {
biden_manage_economy ++;
}
// civil unrest option
int civil_unrest = JOptionPane.showOptionDialog(null,"Which candidate has done better to manage civil unrest? ", "Choose your opinion !!",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options3,options3[0]);
if(pres == 0 && civil_unrest == 0) {
trump_civil_unrest ++;
}
if(pres == 1 && civil_unrest == 1) {
biden_civil_unrest ++;
}
// manage corona option
int manage_corona = JOptionPane.showOptionDialog(null,"Which candidate has done better to manage the coronavirus? ", "Choose your opinion !!",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options3,options3[0]);
if(pres == 0 && manage_corona == 0) {
trump_manage_corona ++;
}
if(pres == 1 && manage_corona == 1) {
biden_manage_corona ++;
}
//EXIT option
Object[] options4 = {"Yes", "No"};
int continue_loop = JOptionPane.showOptionDialog(null,"Do you want to enter more data ? ", "Choose your opinion !!",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options4,options4[0]);
if(continue_loop != 0)
break;
} // end of fro loop
System.out.println("========================================================");
// Print the Winner
if(no_of_trump_voters > no_of_biden_voters) {
System.out.println("The Winner of the election ::: DONALD TRUMP !!");
} else if(no_of_biden_voters > no_of_trump_voters) {
System.out.println("The Winner of the election ::: JOE BIDEN !!");
} else {
System.out.println("DONALD TRUMP & JOE BIDEN HAS EQUAL NUMBER OF VOTES !!");
}
System.out.println("========================================================");
// Print statistics
System.out.println("Total number of voters : " + no_of_voters);
System.out.println("Total number of female voters : " + no_of_male_voters);
System.out.println("Total number of male voters : " + no_of_female_voters);
System.out.println("Total number of Democrats : " + no_of_democrats);
System.out.println("Total number of Republicans : " + no_of_republicans);
System.out.println("Percent of female voters for Trump : " + (no_of_female_trump_voters * 100/no_of_female_voters ) +"%");
System.out.println("Percent of female voters for Biden : " + (no_of_female_biden_voters * 100/no_of_female_voters ) +"%");
System.out.println("Percent of male voters for Trump : " + (no_of_male_trump_voters * 100/no_of_male_voters ) +"%");
System.out.println("Percent of male voters for Biden : " + (no_of_male_biden_voters * 100/no_of_male_voters ) +"%");
System.out.println("Percent of Democrats voting for Trump : " + (no_of_trump_democrat_voters * 100/no_of_democrats ) +"%");
System.out.println("Percent of Republicans voting for Biden : " + (no_of_biden_republican_voters * 100/no_of_republicans ) +"%");
System.out.println("Percent of voters thinking Trump has done better to manage the economy : " + (trump_manage_economy /no_of_voters ) +"%");
System.out.println("Percent of voters thinking Trump has done better to manage the civil unrest : " + (trump_civil_unrest /no_of_voters ) +"%");
System.out.println("Percent of voters thinking Trump has done better to manage the coronavirus : " + (trump_manage_corona /no_of_voters ) +"%");
}
}