Question

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.
  • In this assignment we will develop a program to count votes.
  • 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.
  • Name the source code file Vote.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.
  • Feel free to chose any candidate names you like for the voters to select. You do not have to use the 3 candidates from my example output.
  • Use an indefinite loop to allow multiple users to vote 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.
  • Please use proper style as demonstrated in class
  • When your program works identically to the example output below, submit it to Canvas.

Example Output:

***Voting Machine!***


Candidate A: Dalai Lama
Candidate B: Bernie Sanders
Candidate C: The Black Panther

Enter your choice (A-C) or X to exit: C
You chose candidate The Black Panther

Candidate A: Dalai Lama
Candidate B: Bernie Sanders
Candidate C: The Black Panther

Enter your choice (A-C) or X to exit: c
You chose candidate The Black Panther

Candidate A: Dalai Lama
Candidate B: Bernie Sanders
Candidate C: The Black Panther

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

Candidate A: Dalai Lama
Candidate B: Bernie Sanders
Candidate C: The Black Panther

Enter your choice (A-C) or X to exit: A
You chose candidate Dalai Lama

Candidate A: Dalai Lama
Candidate B: Bernie Sanders
Candidate C: The Black Panther

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

Candidate A: Dalai Lama
Candidate B: Bernie Sanders
Candidate C: The Black Panther

Enter your choice (A-C) or X to exit: a
You chose candidate Dalai Lama

Candidate A: Dalai Lama
Candidate B: Bernie Sanders
Candidate C: The Black Panther

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

Candidate A: Dalai Lama
Candidate B: Bernie Sanders
Candidate C: The Black Panther

Enter your choice (A-C) or X to exit: C
You chose candidate The Black Panther

Candidate A: Dalai Lama
Candidate B: Bernie Sanders
Candidate C: The Black Panther

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

***Election Results!***
Dalai Lama: 2
Bernie Sanders: 2
The Black Panther: 3

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

Source Code in Java:

import java.util.*; //importing util package to use Scanner class for inputs
class Vote
{
public static void main(String args[]) //main function
{
Scanner sc=new Scanner(System.in); //creating object of Scanner class to use for inputs
System.out.println("***Voting Machine!***");
System.out.println();
String candidates[]={"CM Punk","Seth Rollins","Johnny Gargano"}; //storing candidate names in an array
int votes[]={0,0,0}; //using array to store vote for each candidate
char ch;
while(true)
{
//printing vote choices
System.out.println("Candidate A: "+candidates[0]);
System.out.println("Candidate B: "+candidates[1]);
System.out.println("Candidate C: "+candidates[2]);
System.out.println();
//asking for vote
System.out.print("Enter your choice (A-C) or X to exit: ");
ch=sc.next().charAt(0);
//tallying vote using if-else
if(ch=='A' || ch=='a')
{
System.out.println("You chose candidate "+candidates[0]);
votes[0]++;
System.out.println();
}
else if(ch=='B' || ch=='b')
{
System.out.println("You chose candidate "+candidates[1]);
votes[1]++;
System.out.println();
}
else if(ch=='C' || ch=='c')
{
System.out.println("You chose candidate "+candidates[2]);
votes[2]++;
System.out.println();
}
else if(ch=='X' || ch=='x')
{
System.out.println();
break;
}
else
{
System.out.println("Error! Invalid choice!");
System.out.println();
}
}
//publishing the result
System.out.println("***Election Results!***");
System.out.println(candidates[0]+": "+votes[0]);
System.out.println(candidates[1]+": "+votes[1]);
System.out.println(candidates[2]+": "+votes[2]);
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
15.1: Tallying Votes Elections are a formal group-decision making process by which a population chooses a...
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
  • 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...

  • Write a program that supports the three phases (setup, voting and result-tallying) which sets up and...

    Write a program that supports the three phases (setup, voting and result-tallying) which sets up and executes a voting procedure as described above. In more details: You can start with the given skeleton (lab6_skeleton.cpp). There are two structures: Participant structure, which has the following members: id -- an integer variable storing a unique id of the participant (either a candidate or a voter). name -- a Cstring for storing the name of the participant.. hasVoted -- a boolean variable for...

  • Ranked choice voting is a system of tallying election ballots that is used in many national...

    Ranked choice voting is a system of tallying election ballots that is used in many national and local elections throughout the world. Instead of choosing a single candidate, voters must rank the available candidates in the order of their choice. For example, if three candidates are available, a voter might choose #2, #1, and #3 as their choices, with #2 being their first choice, #1 the second, and #3 the third. The outcome is determined by a runoff, which follows...

  • COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize...

    COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize votes for a local election Specification: Write a program that keeps track the votes that each candidate received in a local election. The program should output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. To solve this problem, you will use one dynamic...

  • You are tallying votes from an election in which n people voted. If any candidate gets...

    You are tallying votes from an election in which n people voted. If any candidate gets more than half (at least ⌊n/2⌋ + 1 votes), they win. Otherwise a runoff election is needed. For privacy reasons you are not allowed to look at any one ballot, but you have a machine that can take any two ballots and answer the question: “are these two ballots for the same candidate, or no?” (a) Design and analyze a divide and conquer algorithm...

  • 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....

  • Please post following code with criteria below... please leave as many comments as possible Exam Four:...

    Please post following code with criteria below... please leave as many comments as possible Exam Four: the election Outcome: Student will demonstrate all core competencies from this class: Program Design (design tools) Variables Decision Error Checking Looping Functions Arrays Program Specifications: Let’s pretend that we are tracking votes for the next presidential election. There will be two candidates: Ivanka Trump and Michele Obama. You can assume that there are fifty states casting votes. You will do not need to deal...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • In November 1993, the state of Pennsylvania conducted clections for its state legislature. The re...

    In November 1993, the state of Pennsylvania conducted clections for its state legislature. The result in the Senate election in the 2nd district (based in Philadelphia) was challenged in court and ultimately overturned. The Democratic candidate won 19,127 of the votes cast by voting machine, while the Republican won 19,691 votes cast by voting machine, giving the Republican a lead of 564 votes. However, the Democrat won 1,391 absentee ballots, while the Republican won just 366 absentee ballots, more than...

  • (2 points) 5 4 4 0 6 Dominic Annie Florence Dominic Florence Annie Annie Dominic Florence...

    (2 points) 5 4 4 0 6 Dominic Annie Florence Dominic Florence Annie Annie Dominic Florence Annie Florence Dominic Florence Dominic Annie Florence Annie Dominic Construct an example with 19 voters in which: 1) Dominic would win with the Plurality method 2) Annie would win with the Borda count method and be a Condorcet candidate (and win with Pairwise Comparison as well), 3) Florence would win if Plurality with Elimination is used 4) Also, Annie has a total of two...

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