Homework 1

Part A - Printing

First, get Netbeans set up at home.

Create a Java project (hwk01userid) in Netbeans at home. In it you should have a class with a main (if I don't tell you what to name a class, you may choose the name (NetBeans' default is fine)

In the main method ("in main") do exercise 2.18 from the Deitel textbook.

Part B - Assigning Variables

Continuing in the same main after the previous code:

Create an int variable num and set it equal to 1. We would often say "an int num starts as 1"

Print num -- this means to use println to print out the variable itself, but you would usually include very brief extra text so your output is easy to read; so it might end up looking like "num is 1" or "num = 1" or "num: 1" when printed. Remember to never type the value of the variable when you print, use the variable itself in the print statement.

Now go back and change the code so that num is equal to 5 instead. You shouldn't have to change the printout, it should automatically print something like "num is 5" when you run it now. Note that I said "go back" to tell you that you are changing code you already wrote. Without wording like this, you should assume that each thing I tell you to do is new code to add after the previous.

Make an int x that starts as 10. Print it.

Now we want to swap the values of num and x. To do this you will need a third variable, temp (also an int, since the other two are). To swap, you will need to do three statements to move the values around. If you think of them as three glasses, what you want to do is pour the value of num into temp, pour the value of x into num, and pour the value of temp into x.

Print both num and x after the swap to check that it worked. We will often just say "print the results" and know that since temp was just a temporary variable, it doesn't need to be printed.

Check that if you go back and change the original values of num and x but no other code, the swap still works.

Part C - Expressions

Continuing in the same main (at this point, num is 10 and x is 5):

We will now make some changes to num. Every change should involve java doing the math, not you doing the math and typing in the result.

After each change, print again. Don't do the math in the println; first change num, then print num to see its new value. If we wanted to do the math in the print we would say something like "print a number one bigger than num without changing num"

Make num one bigger than it was. We often say "increment" or "increase".

Make num 4 bigger than it was. We would often say "increase num by 4" or just "add 4 to num" to mean the same thing. So now num, which was 10, should be up to 15, because we added 1 then 4.

Now make num x bigger than it was. We would often say "add x to num" (hint: if you could do this with 4, you can do it with x)

Make num 2 smaller. We would say "decrease num by 2" or "subtract 2 from num" to mean this.

Make num twice as big. We might say "double num" or "multiply num by 2" (note that this use of "double" has nothing to do with double type variables)

Make num 5 times as big. We might say "multiply num by 5"

Multiply num by x. (hint: if you can do this with 5, you can do this with x)

Multiply num by itself. (hint: if you can do this with x, you can do this with num)

Now go back and change the original value of num and the original value of x. When you run the code again without changing any other lines, check that your code gives the right results for the new values.


After you submit, remember to download your zip from blackboard, unzip it, and drag the unzipped directory to netbeans to make sure you submitted what you intended.