Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views1 page

Switch Exercises 2024

Uploaded by

holycowman01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Switch Exercises 2024

Uploaded by

holycowman01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

ICS3U

Switch Exercises
ONE class.

First, open and run the switch statement example programs. Make sure you follow what is happening and how
the programs work. READ the code and comments!! Make changes and play around with the values to get a
better grasp of how they work.

Next, do the following programs.

1. LastnameSwitchGrades.java
Ask the user to enter a number grade between 0 and 100. Handle invalid inputs (grade > 100 or grade < 0)
appropriately using a loop. Use a switch structure with appropriate cases to output the equivalent letter
grade. Print a closing message after the switch.
A: 80 – 100
B: 70 – 79
C: 60 – 69
D: 50 – 59
F: 0 – 49

2. LastnameSwitchVowels.java
Write a program that asks the user to enter a letter of the alphabet and determines if it is a consonant or a
vowel. Use a switch structure, and tell the user what type of letter they entered.
Handle invalid entries (i.e. non-alphabetic characters) by including an appropriate default case.
You may instead wish to use casting and the ASCII character codes to determine whether the character
entered by the user is a letter of the alphabet or not. If not, ask the user to enter a letter again. Use an
appropriate loop for this.

3. LastnameSwitchMenu.java
Write a program that presents the user with a menu of 5 possible items to purchase and their prices, and
then calculates the tax amount and total with tax. The program should display the user’s choice of item
(name, description, and price), along with the price, tax amount, and total with tax.
• The nature of the items is up to you (e.g. food, music, furniture, computer hardware, sports equipment,
etc), just keep it appropriate.
• Store the tax rate and prices of the items as constants, and use these constants in your calculations and
output. Remember, constants are declared using the reserved word final and are named in all capitals.
• Use a switch statement to properly handle the user’s choice of item.

4. LastnameSwitchTriviaGame.java
Write a program that asks the user 5 multiple choice questions, one at a time (each with at least 4 possible
responses). The theme of your questions is up to you (e.g. music, movies, sports, fashion, tech, etc.).
Use switch structures to handle the user's choice for each question.
Tell the user if they are right or wrong for each question.
Keep track of how many questions they get correct (i.e. the score).
Show them their score out of 5, and the percentage (rounded to 1 decimal).
Make sure to declare only necessary variables (for example, the user's choice/selection for each trivia
question can be stored in one variable that is reused for each question).

You might also like