Back to 111 Main Page

Mobius strip

Assignment 1

Task

Print to the screen, using variables and basic math operators.

Steps

Create a class named Assignment1 in a file named Assignment1.java. 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: 5
Second number: 2

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: 5
Second number: 2
5 + 2 = 7
5 - 2 = 3
5 * 2 = 10
5 / 2 = 2
5 % 2 = 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 '5' and the number '2' each occur only once in my code--when I initialize my two variables.

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

Questions

Now that you have written your program, change the values assigned to your two variables in order to answer the following five questions. Remember to recompile each time you change your code.

  1. What happens if you assign 5.5 (rather than 5) to the first variable? Why?
  2. What happens to your program when the second number is 0?
  3. What is the result of 300,000 * 10,000? Why?
  4. What is the result of 14 / 5? Why is this not 2.8? Why is it not 3?
  5. What is the name of the % operator? What does it do?

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

What to submit

Attach your Assignment1.java file to an email to me as described in the submission guidelines. Paste the answers to the 5 questions above into the body of the email. (If you also paste a copy of your code into the email, put it below your answers.)

FAQs

How do I print out the expressions?
You need to print out a String, something like: "5 + 2 = ". 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 "5"s and "2". So how do you print out "5 + 2 = ", but replace the 5 and 2 in your code with the appropriate variable names?

Grading

Out of 10 points:

1 - Submission
Follows required submission policies.
2 - Compiles
Your program compiles successfully (no errors)
2 - Variables
You use variables, not literals, for the two numbers in all your calculations and printing. You display the values of your two variables before you do the calculations.
2.5 - Operators
Program displays the results of all 5 operations. Prints the expression for each, as well as the result.
2.5 - Questions
You correctly answer the 5 questions above.


~ztomasze Index : TA Details: ICS111: Assignment 1
http://www2.hawaii.edu/~ztomasze
Last Edited: 22 May 2008
©2008 by Z. Tomaszewski.