ITEC 120 LAB 7 Back to Lab index page

Today's menu

You will write Java program that will ask the user to enter and integer and then chose from a menu of commands. They will be able to Square the number, Cube the number, or calculate the multiplicitive inverse of the number. The program will then ask the user to enter another number. To quit the program, the user will enter 0 (zero).

Sample run

Program outputs:

Please enter an integer (0 to quit):

User inputs: 3
Program outputs:

Please choose a command:
S - Square the number
C - Cube the number n
I - Find the number's inverse

User inputs: s
Program outputs:

3 squared is 9

Please enter an integer (0 to quit):

User inputs: -4
Program outputs:

Please choose a command:
S - Square the number
C - Cube the number
I - Find the number's inverse

User inputs: C
Program outputs:

-4 cubed is -64

Please enter an integer (0 to quit):

User inputs: 6
Program outputs: Please choose a command:
S - Square the number
C - Cube the number
I - Find the number's inverse
User inputs: x
Program outputs:

x is not a command!

Please enter an integer (0 to quit):

User inputs: 2
Program outputs:

Please choose a command:
S - Square the number
C - Cube the number
I - Find the number's inverse

User inputs: i
Program outputs:

The inverse of 2 is 0.5

Please enter an integer (0 to quit):
Thanks for using today's menu
GOODBYE!!!
------------------------------

You will use the Keyboard class for this assignment. Make sure the Keyboard.class file is in a folder called cs1 in the same directory as your .java file. You will use a switch statement and a loop for this assignment. Use incremental development to write this program. First, write a loop that will read integers from the user until they enter a zero. Then, write the switch statement. You could get one piece of the switch statement to work at a time.

Be sure the user can enter a capital or a lowercase letter for their commands. If they enter a command other than s, c, or i, you should give an error message, and then ask for an integer again.

Once you have your program working, show your lab to the Peer Instructor to be checked off. The Peer Instructor will run the program.