Back to 111 Main Page

Mobius strip

Assignment 04

Task

As in A01a, print the results of 5 simple math operations to the screen--only this time get the two numbers from the user each time.

Textbook: 2.6
New concepts: Input, Scanner.

Steps

Create a class named UsernameA04 in a file named UsernameA04.java. Remember to replace Username with your actual UH username. In the main method, do the following:

Explain briefly to the user what your program is going to do. Then ask the user to enter the first number, and read it in using a Scanner. Then do the same for the second number. (Remember, you only need to create one Scanner object which you reuse to read in both numbers.)

Now print the results of using these two numbers in five different math expressions using the +, -, *, /, and % operators. Don't just print out the result though; also print out what the expression is.

So your final output should now look something like this. (Note how I executed the program twice; the command prompts and java command are not part of the output of the program. The stuff in green here is the input I typed when prompted by the program.)

D:\TA\grading\A04>java ZtomaszeA04
This program performs 5 math operations on two integers.
Enter the first number: 7
Enter the second number: 3

7 + 3 = 10
7 - 3 = 4
7 * 3 = 21
7 / 3 = 2
7 % 3 = 1

D:\TA\grading\A04>java ZtomaszeA04
This program performs 5 math operations on two integers.
Enter the first number: 9
Enter the second number: -2

9 + -2 = 7
9 - -2 = 11
9 * -2 = -18
9 / -2 = -4
9 % -2 = 1

Your program will probably crash if you enter decimal numbers or Strings instead of integers. At this point, that is okay; we'll soon learn how to prevent this.

What to Submit

Upload your UsernameA04.java file to Tamarin.

Grading [3 points]

1 - Compiles
Your program compiles successfully (no errors)
1 - Input
You prompt the user to enter two integers, which you use to do the calculations.
1 - Operators
Program displays the correct results of all 5 operations. (0.5) Prints the expression for each, as well as the result. (0.5)

FAQs

Where's the demo code from class?
Here: UserAge.java


~ztomasze Index : TA Details: ICS111: A04
http://www2.hawaii.edu/~ztomasze
Last Edited: 10 Sep 2008
©2008 by Z. Tomaszewski.