Back to 111 Main Page

Mobius strip

Assignment 1 (a & b)

Task

Print the results of 5 simple math operations to the screen; then answer a few questions related to variables.

Textbook: 2.1 - 2.3
New concepts: Literals, data types, variables, assignment, initialization, string concatanation, simple math operators.

Part A01a [3 points]

Create a class named UsernameA01a in a file named UsernameA01a.java. Remember to replace Username with your actual UH username.

In the main method, do the following:

First, create (that is, declare and initialize) two int variables to hold two whole numbers. Print these to the screen so that your output looks something like this:

First number: 9
Second number: 4

Now print the results of using these two variables 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 like this:

First number: 9
Second number: 4
9 + 4 = 13
9 - 4 = 5
9 * 4 = 36
9 / 4 = 2
9 % 4 = 1

In all your calculations and printing, use the two variables you declared at the beginning of your program. So, for example, in producing the above output, the number '9' and the number '4' each occur only once in my code--when I initialize my two variables. You can check you have this correct by changing the initial values of your variables to 5 and 2 and all your output should change appropriately. (Change them back to 9 and 4 when you're done though.)

You may use more than just two variables if you like. For instance, you might want a variable that holds the result of each operation until you print it out.

Part A01b [3 points]

Now that you have written your program, change the values assigned to your two variables in order to answer the following questions. Remember to recompile each time you change your code, and to change your code back to its original form after each question. You may also need to look in your book to learn more.

  1. What happens if you assign 9.5 (rather than 9) to the first variable? Why?
  2. What happens if you assign '9' (rather than 9) to the first variable? Why?
    (Hint: Unicode, ASCII, and ASCII character maps.)
  3. What happens to your program when the second number is 0?
  4. What is the result of 3000000 * 1000? Why?
  5. What is the result of 14 / 5? Why is this not 2.8? Why is it not 3?
  6. What is the name of the % operator? What does it do?

Save your answers in a plain text file with a .txt extension.

When you are done answering these questions, set your two variables back to 9 and 4, so that your program produces output as above.

What to Submit

Part A01a: Upload your UsernameA01a.java file to Tamarin.

Part A01b: Upload your UsernameA01b.txt file to Tamarin.

Grading [6 points]

1 - Compiles
Your program compiles successfully (no errors)
1 - Variables
You use variables (not literals) for the two numbers in all your calculations and printing. (0.5) You display the values of your two variables before you do the calculations. (0.5)
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)
3 - Questions Answered
0.5 points per question.

FAQs

Where's the demo code from class?
Here: ZtomaszeAge.java
So we actually need to print the expressions to the screen?
Yes, your output should look like that shown above. So, for example, 9 - 4 = 5 will actually show up in your output. However, the numbers (9 and 4, in this case) should each only occur once in your code.
How do I print out the expressions?
You need to print out a String, something like: System.out.println("9 + 4 = "); But this alone doesn't work, since now, if you change the value of your variables, you need to go through every line of your code changing all the the "9"s and "4"s. So how do you print out "9 + 4 = ", but replace the 9 and 4 in your code with the appropriate variable names? (Hint: string concatanation.)
When I try to submit A01b to Tamarin, I get some error about it being a binary file, but it's text. What's up with that?
Sometimes if you copy and paste text out of Word or other word processors, you'll get fancy characters that are not strictly plain text. These are usually curvy single or double quote characters, em dashes, etc. Tamarin will tell you the line and character number of the first non-ASCII character in your file, so you may have to go to that location and see which character is causing the problem.


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