Non-robotic Programs |
Selecting and naming variables effectively is an important part of writing code that is understandable. For each of the following situations, pick:
You must do that for each of the following scenarios:
Using the Nonrobotic_Template.java. Next, add code that will accomplish the following:
Print
out "Hello, User! Please type an integer!"
<At this point, you should stop and make sure that your program compiles and
runs>
Get the
number that the user typed.
<You should go back, and review the In-Class Exercises from Topic #3, if you
need to. You may want to do this review by re-downloading the example
/ demo .JAVA files, running them, examining their code, examining the blue &
yellow boxes in the In-Class Exercises document, etc. >
< At this point, don't worry about checking that the user actually typed in
a number (as opposed to, say, their name) >
Print
out the message "You just typed:" followed by whatever the user typed. Then
print out "Twice what you just typed is: " followed by the appropriate
value.
Go back & make sure that the program will NOT crash if the user types in, say, their name. If they do type in a non-integer number, print an error message telling them that they need to type in a number, instead.
Download a copy of the TipCalc.java file. Using what's provided there as a starting point, modify it so that the program will ask the user for the total cost of a meal, and then tell them what a 15%, 20%, or 30% tip would be. You should also print a message telling the user if the meal is "pricey", where "pricey" means more than $20.
Since this is one of your first completely non-robotic programs, we'll include some detailed directions here:
1. Make sure that you've imported everything you need to, at the top of the file
2. For this program, we'll put everything into main.
3. Make sure that you start by creating a Scanner object (which we've been naming keyboard, but you're welcome to pick any name you want), and a variable to store the user's input into.
A.
Because we want to store a double-precision floating point number, we'll
need to use a double
variable, instead of an int
variable.
double userInput;
4. Print out a message telling the user that you expect them to type in a number, which is the cost of a meal.
A.
<At this point, you should stop and make sure that your program compiles
and runs>
5. Get the number that the user typed.
A. <You should go back, and review Topic #3, if you need to>
B.
If you go to
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html, you'll see
that there are
hasNextDouble()
and
nextDouble()
methods, just like there are
hasNextInt()
and
nextInt()
methods.
C.
<Make sure that your program compiles and runs>
6.
Create more variables to store the various tips. You might name them
tip15, tip20, and tip 30.
7.
Calculate the appropriate tips, and store them into the appropriate
variables.
8.
Print out a message informing the user what the various tips are.
9. Once you've got the above program working, try testing this out with different inputs. Does this work for positive integers (like "10"?) What about for positive floating point numbers (like "10.75"?) What about zero? What about negative numbers (both integers and decimal numbers) What happens if you type in your name instead of a number?