- Activity 1: Downloading a Program
-
Create a new project named L03,
with a main program class l03.MoviePrices.
Download MoviePrices.java (linked above)
into your CSCI1226/L03/src/l03/ folder.
Tell your computer
that you would like to replace the one that's there.
That program is the sample code version
of one we did in class.
It reads the user's age
and looks up the price they need to pay
based on that age.
Run the program a few times,
trying different ages.
- Activity 2: Checking the User's Input
-
Try running the program
with some "stupid" values for the age:
negative numbers,
and numbers way over 100.
Notice that the program doesn't bat an eye
(not that it has eyes to bat).
It's generally a good idea to check any number the user enters
to make sure that it makes sense.
The first change we'd like you to make to the program
is to add code to check that the user's age makes sense.
If their age is under zero,
the program should say "That can't be right!"
and ask them to try again:
How old are you? -5
That can't be right!
How old are you, really?
The program should read in the age again
(using the same variable),
then carry on directly into the code for looking up the price:
How old are you, really? 7
Children's price.
That'll be $2.99, please.
- Activity 3: More Checking
-
Next,
add code to check whether the first age they entered
is 100 or more.
The program should express surprise,
and ask them if they really are that old:
How old are you? 115
Wow, that's surprising!
Are you really 115?
The program should read a word as their answer.
If their answer starts with "no"
(or "NO" or "No" or "nO")
then the program should ask them for their actual age:
Are you really 115? No.
So what's your real age, then? 15
Youth's price.
That'll be $7.99, please.
If their answer is anything else,
the program should proceed using the original number they entered:
Are you really 115? Sure am!
Senior's price.
That'll be $6.99, please.
- Activity 4: Yet More Checking the User's Input
-
In two cases above,
we asked the user to re-enter their age,
but then we just accepted their second answer
without checking it.
The user could have re-entered a "stupid" number:
How old are you, really? -700
Children's price.
That'll be $2.99, please.
We'd like to add one last check for their age.
Just before we get to the part that looks up their price,
add a test to see whether their age is less than 0
or more than 120.
If it is,
then the program should skip
announcing the price
and instead print the message
"I don't believe that you're (whatever age). Please go away."
How old are you, really? -700
I don't believe that you're -700. Please go away.
Of course,
if their age is in the valid range,
then the normal lookup should proceed:
Are you really 115? Sure am!
Senior's price.
That'll be $6.99, please.
Once all the activities are completed you can submit
your Java program by one of the SUBMIT links above/below.