Assignment 14
Due Tuesday, November 13 at 9:20am on Canvas

Assignment 12.2: Tallying Votes (10 pts)

  • 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

Assignment 14.2: Really Cool Ice Cream (10 pts)

  • Encoding and decoding information can save memory space and speed transmission of data.
  • Waitstaff at ice cream shops often encode orders to reduce writing and speed up customer service.
  • Develop a program that asks a user for a single input -- where the first two characters is a flavor of ice cream in the following shorthand code and the last characters are the number of scoops for the type.
Code Ice Cream Flavor
CHChocolate
CCChocolate Chocolate Chip
CMChocolate Mint
HCHorchata
MCMocha Chip
RRRocky Road
SBStrawberry
VBVanilla Bean


  • For example, the code for twelve scoops of Chocolate Mint is: CM12. Notice there are no spaces between the code and quantity.
  • Name the source code file Icecream.java and include all your code in this single file.
  • Ask the user for the following inputs (and no other input) in this order, as shown in the Example Run below:
    • A single order code like: CM12 (no spaces allowed!!)
    • A 'y' or 'n' (without the quotes) for the repeat loop
  • Also, assume the user enters all the codes with UPPERCASE letters as shown.
  • Store the order code in a single String variable.
    • Then use substring() to analyze and process the input string to print a full description of the ice cream order.
    • Use the length() method so that your program adapts to orders of any size.
    • What happens if a user wants to order 1000 cones of one flavor, 10,000 etc?
  • Add a while statement that allows the user to repeat the program by entering a 'y' (without the quotes).
  • If the user enters an invalid code, display the message, "We don't have that flavor." as shown in the example output.
Your Output Should Look Indentical to the Following (except user input will vary):

Welcome to the Really Cool Ice Creamery!

Enter the ice cream order code: CM12
12 Chocolate Mint
Another order? (y/n) y

Enter the ice cream order code: HC2
2 Horchata
Another order? (y/n) y

Enter the ice cream order code: XY123
We don't have that flavor.
Another order? (y/n) n

Your ice cream order will be right up!

Assignment 14.3: Vowels (10 pts)
  • Write a program that takes in a sentence from the user and then counts the number of vowels in the sentence.
  • The user should be able to enter as many sentences as desired.
  • You will need to use String.charAt(), String.length(), and two loops - a while loop and a for loop to solve this problem correctly.
  • Open up a new Java class file called Vowels.java
  • First, welcome the user to the program and explain its purpose:
Welcome! Give me a sentence and I will count its vowels.
  • Then prompt the user to enter a sentence.
Please enter a sentence or q to quit: |
  • Store the user input as a string variable.
  • Now, using a for loop and the String.length() method, count how many vowels (a, e, i, o, u) there are in the sentence.
  • Note that the program should also count capital vowels (A, E, I, O, U).
  • Finally, output the number of vowels like so:
There are 5 vowels in your sentence.
  • Next, place the prompt and for loop inside of a while loop so that the user can enter multiple sentences.
  • If the user enters q, the loop should end and the program should print out a message "Goodbye!"
  • Once your program is working identically to the examples below, submit it to Canvas.

The output of your program should look identical to the sample output below except user input may vary.

Welcome! Give me a sentence and I will count its vowels.

Please enter a sentence or q to quit: Power to the people!
There are 7 vowels in your sentence.

Please enter a sentence or q to quit: Chillax!
There are 2 vowels in your sentence.

Please enter a sentence or q to quit: Animals are friends not food.
There are 10 vowels in your sentence.

Please enter a sentence or q to quit: q

Goodbye!