Java
TOPIC 20 – WHILE STATEMENTS – PART 2

AP EXTRA - COUNTING ITERATIONS

 

AP LESSON WORK

 

 

QUESTION 1


Create a program that offers the user four options.  If the user correctly chooses one of the four options, then the program simply outputs a related message.  However, if the user enters an illegal value, then the user needs to be informed of the error and asked to choose again.

 

Sample output:

 

Welcome to the appointment app for Campeau’s Cars Garage.  Here are the four types of services that we provide:

 

1-Oil Change

2-Brake Examination

3-Undercoating

4-Car Wash

 

Please enter a choice:

5

 

Illegal value.

Please enter a number between 1 and 4.

0

 

Illegal value.

Please enter a number between 1 and 4.

2

 

Great.  We’ll examine your breaks this upcoming Tuesday at 4:00.  See you then!


Sample output 2:

 

What would you like to do today?

 

1-Eat Bacon

2-Save the World

3-Write open source software

4-Stand on a stool during Oh Canada!

 

Please enter a choice:

0

 

Illegal value.

Please enter a number between 1 and 4.

3

 

You actually prefer writing open source software over saving the world?  This must be Justin Michaud.

 

QUESTION 2


Copy your code from Question1 into a new document.  Alter the code so that your response to illegal entries starts off polite but gets increasingly annoyed.

 

 

Sample output:

 

Welcome to the appointment app for Campeau’s Cars Garage.  Welcome to the appointment app for Campeau’s Cars Garage.  Here are the four types of services that we provide:

 

1-Oil Change

2-Brake Examination

3-Undercoating

4-Car Wash

 

Please enter a choice:

5

 

Sorry, you entered an illegal value.

Please enter a number between 1 and 4.

0

 

Oh, that’s not a number between 1 and 4.

Please enter a number between 1 and 4.

7

 

Um, that’s not right.

Please enter a number between 1 and 4.

9

 

Seriously?

Please enter a number between 1 and 4.

-11

 

Cmon, you got this!

Please enter a number between 1 and 4.

6

 

For the love of god!

Please enter a number between 1 and 4.

5

 

Ok, you should not be driving car!

Please enter a number between 1 and 4.

4

 

Sounds good.  Come by on Thursday at 12pm for a car wash.