Question

Assignment 11.2: New Hampshire Primary (10 pts) The New Hampshire Primary is taking place this Tuesday,...

Assignment 11.2: New Hampshire Primary (10 pts)

  • The New Hampshire Primary is taking place this Tuesday, February 11, 2020.
  • According to Wikipedia, a primary "Primary elections or often just primaries, are the process by which voters can indicate their preference for their party's candidate, or a candidate in general, in an upcoming general election... with the goal of narrowing the field of candidates"
  • The New Hampshire primary is the first of the primary elections, which will be held to select the Democratic nominee for the 2020 presidential race.
    • Note that the Republican nominee has essentially already been determined
  • Although New Hampshire is just a small state, its primary receives massive media coverage, and, therefore, has a greater influence over the Democratic race than is small size would suggest.
  • In this assignment we will develop a program to count votes for the 2020 New Hampshire Primary.
  • The user enters votes based on a simple menu that presents the candidates.
  • The user then inputs a single character (upper or lowercase A-C) to choose their candidate from the menu.
  • Entering an 'X' (without the quotes) will exit the tallying operation and present a summary of the votes.
  • Write your code inside the file Primary.java.
  • Present a list of candidates to the user and allow her to enter a single character as input, and no other input, as shown in the Example Output.
  • Use an indefinite while loop to allow multiple votes to be cast on your voting machine.
  • After all the votes are entered and the user enters an 'X', exit the loop and report the vote count by candidate names and neatly aligned as shown in the Example Run.
  • Your program should also report an error message if the user enters a letter other than A-C or X, as shown below.
  • When your program works identically to the example output below, submit it to Canvas.

Example Output:

***New Hampshire Primary Candidates!***


Candidate A: Bernie Sanders
Candidate B: Elizabeth Warren

Candidate C: Joe Biden

Candidate D: Pete Buttigieg

Enter your choice (A-D) or X to exit: C
You chose candidate Joe Biden

Candidate A: Bernie Sanders
Candidate B: Elizabeth Warren

Candidate C: Joe Biden

Candidate D: Pete Buttigieg


Enter your choice (A-D) or X to exit: b
You chose candidate Elizabeth Warren

Candidate A: Bernie Sanders
Candidate B: Elizabeth Warren

Candidate C: Joe Biden

Candidate D: Pete Buttigieg


Enter your choice (A-D) or X to exit: A
You chose candidate Bernie Sanders

Candidate A: Bernie Sanders
Candidate B: Elizabeth Warren

Candidate C: Joe Biden

Candidate D: Pete Buttigieg


Enter your choice (A-D) or X to exit: Z
Invalid choice. Please enter A, B, C, or D.

Candidate A: Bernie Sanders
Candidate B: Elizabeth Warren

Candidate C: Joe Biden

Candidate D: Pete Buttigieg


Enter your choice (A-D) or X to exit: a
You chose candidate Bernie Sanders

Candidate A: Bernie Sanders
Candidate B: Elizabeth Warren

Candidate C: Joe Biden

Candidate D: Pete Buttigieg


Enter your choice (A-D) or X to exit: D
You chose candidate Pete Buttigieg
Candidate A: Bernie Sanders
Candidate B: Elizabeth Warren

Candidate C: Joe Biden

Candidate D: Pete Buttigieg


Enter your choice (A-D) or X to exit: X

***Primary Results!***

Bernie Sanders: 2

Elizabeth Warren: 1

Joe Biden: 1

Pete Buttigieg: 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;
import java.lang.*;
import java.util.*;
class primary
{
public static void main(String args[])throws IOException
{
InputStreamReader ob = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ob);
{

int a,b,c,d; a=0;b=0;c=0;d=0;
System.out.println("***New Hampshire Primary Candidates!***");

for(;;)
{
System.out.println("Candidate A: Bernie Sanders");
System.out.println("Candidate B: Elizabeth Warren");
System.out.println("Candidate C: Joe Biden");
System.out.println("Candidate D: Pete Buttigieg");
System.out.println("Enter your choice (A-D) or X to exit:");
char ch=(char)br.read();

switch(ch)
{
case 'A':
{
System.out.println("You chose candidate Bernie Sanders");
a++;

break;
}
case 'B':
{
System.out.println("You chose candidate Elizabeth Warren");
b++;
break;
}
case 'C':
{
System.out.println("You chose candidate Joe Biden");
c++;
break;
}
case 'D':
{
System.out.println("You chose candidate Pete Buttigieg");
d++;
break;

}
case 'X':
{
System.out.println("***Primary Results!***");
System.out.println("Bernie Sanders:"+a);
System.out.println("Elizabeth Warren:"+b);
System.out.println("Joe Biden:"+c);
System.out.println("Pete Buttigieg:"+d);
System.exit(0);
}
default:

{
System.out.println("Invalid choice. Please enter A, B, C, or D.");
}
}
}
}

}
}

Add a comment
Know the answer?
Add Answer to:
Assignment 11.2: New Hampshire Primary (10 pts) The New Hampshire Primary is taking place this Tuesday,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 15.1: Tallying Votes Elections are a formal group-decision making process by which a population chooses a...

    15.1: Tallying Votes Elections are a formal group-decision making process by which a population chooses a person to hold office, such as the mayor of a city. Another use of elections is to accept or reject a political proposition. Most election results are tallied, or counted, using electronic voting. Most electronic voting machines use a computer to take care of the chore of casting and counting votes. This Tuesday, November 6 (the date your assignment is due) is election day....

  • To combat election fraud, your city is instituting a new voting procedure. The ballot has a...

    To combat election fraud, your city is instituting a new voting procedure. The ballot has a letter associated with every selection a voter may make. A sample ballot is shown:- (Voter places a tick next to his or her preferred candidate, proposition and measure to indicate his/her vote) 1. Mayoral Candidates     A. Pincher, Penny     B. Dover, Skip     C. Perman, Sue 2. PROP 17     D. YES     E. NO 3. MEASURE 1     F. YES     G....

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • Please answer the following in Python: The cost of a travel pass includes a $5 administrative charge and the cost of one unit of trips (or 10 trips) at $1.50 per trip. A passenger who has a tr...

    Please answer the following in Python: The cost of a travel pass includes a $5 administrative charge and the cost of one unit of trips (or 10 trips) at $1.50 per trip. A passenger who has a travel pass can perform these operations make trips as long as there is at least one unconsumed trip, top up in multiples of units of trips without incurring administrative charge get a refund at $1.20 per unconsumed trip C) C) C) When a...

  • Requires Python to answer A client wishes to keep track of his investment in shares. Write...

    Requires Python to answer A client wishes to keep track of his investment in shares. Write a program to help him manage his stock portfolio. You are to record both the shares that he is holding as well as shares that he has sold. For shares be is curreatly holding, record the following data: .a 3-character share code, share name, last purchase date, volume currently held and average purchase price (refer to description under part cii) option 2) For shares...

  • Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void...

    Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void main(String[] args) { int currentVertex, userChoice; Scanner input = new Scanner(System.in); // create graph using your WeightedGraph based on author's Graph WeightedGraph myGraph = new WeightedGraph(4); // add labels myGraph.setLabel(0,"Spot zero"); myGraph.setLabel(1,"Spot one"); myGraph.setLabel(2,"Spot two"); myGraph.setLabel(3,"Spot three"); // Add each edge (this directed Graph has 5 edges, // so we add 5 edges) myGraph.addEdge(0,2,9); myGraph.addEdge(1,0,7); myGraph.addEdge(2,3,12); myGraph.addEdge(3,0,15); myGraph.addEdge(3,1,6); // let's pretend we are on...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT