Non-robotic Programs

Note:  Please keep the programs that you create today, in case you have a dispute about your grades for the ICEs at the end of the quarter. When you're working with a partner, each person should save their own, individual copy

 

Part 1: Variable Design

 

            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:

 

  1. The number of times that a loop has executed within a method (command)
  2. A robot odometer value
    (how many intersections the robot has traveled, throughout the robot's lifetime)
  3. A car odometer value
    (how many miles the car has traveled, throughout the car's lifetime)
  4. The robot's "home intersection" – the avenue and street where the robot was created.
  5. An indicator about whether or not the robot has been rented before

 

Part  2: Nonrobotic Software : Reviewing Input/Output

 

            Using the Nonrobotic_Template.java.  Next, add code that will accomplish the following:

 

  1. Print out "Hello, User!  Please type an integer!"
    <At this point, you should stop and make sure that your program compiles and runs>

     

  2. 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) >

     

  3. 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.
     

  4. 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.

Part  3: Nonrobotic Software : Calculating Tip

 

            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?