Question

In: Computer Science

JAVA Program Write a program that prompts the user for data until the user wishes to...

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:

  • The registration of the voter (Democrat, Republican or other)
  • The gender of the voter
  • The Presidential candidate the voter is choosing (Trump or Biden)
  • Which candidate has done better to manage the economy? (Trump or Biden)
  • Which candidate has done better to manage civil unrest? (Trump or Biden)
  • Which candidate has done better to manage the coronavirus? (Trump or Biden)

You will then print the following statistics:

  • Print the Candidate who won
  • Total number of voters
  • Total number of female voters
  • Total number of male voters
  • Total number of Democrats
  • Total number of Republicans
  • percent of female voters for Trump
  • percent of female voters for Biden
  • percent of male voters for Trump
  • percent of male voters for Biden
  • percent of Democrats voting for Trump
  • percent of Republicans voting for Biden
  • percent of voters thinking Trump has done better to manage the economy
  • percent of voters thinking Trump has done better to manage the civil unrest
  • percent of voters thinking Trump has done better to manage the coronavirus
  • You must use dialog boxes for input, but may use System.out statements or a dialog box for output.
  • You are to copy your source code and a screen print of the final program output to Word and upload the Word document to Blackboard

Design help with this assignment:

  • Define all variables needed at the beginning of your program. You will need a variable for everything you need to track.
  • Start the loop (you will have a while loop to run until the user wants to stop. You will NOT have a for loop)
  • At the very beginning of the loop, read in ALL data
  • Count each thing you need to count, for example, how many for Trump, how many for Biden, number of females, number of males, number of females for Trump, number of females for Biden etc… You will have a lot of if statements in the loop
  • End the loop, ask the user if he wants to enter more data
  • After the loop, do all of your calculations, the percentage of males for Trump, the percentage of females for Trump, etc….
  • Print all required data

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.

Solutions

Expert Solution


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 )  +"%");
       
    }
    
}

Related Solutions

In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
Write a program in JAVA that prompts the user for a lower bound and an upper...
Write a program in JAVA that prompts the user for a lower bound and an upper bound. Use a loop to output all of the even integers within the range inputted by the user on a single line.
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a program that prompts the user for an even number from 2 to 100 until...
Write a program that prompts the user for an even number from 2 to 100 until the number 90 is encountered. Not including the 90, calculate the minimum value. In case you know what this means: DO NOT USE LISTS! We will look into the use of lists later. This has to be done in the python program. Here's what I have so far: inp = 0 min = 0 while inp != 90:     inp = int(input("Please enter an even...
Write a complete C++ program that prompts the user for and takes as input, numbers until...
Write a complete C++ program that prompts the user for and takes as input, numbers until the user types in a negative number. the program should add all of the numbers together. Then if the result is less than 20 the program should multiply the result by 3, otherwise subtract 2 from the result. Finally, the program should printout the result.
Write a JAVA program that prompts the user for the number of names they’d like to...
Write a JAVA program that prompts the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT