Numbers and Expressions

This lab will introduce you to how Java programs work with numbers and expressions

To Get Started ...

Programmers need to understand a bit about how different data types are stored in the computer. Here we are going to look at how integers are stored in the computer. We've written a Java applet to give you a little practice in converting decimal numbers to their binary representations.

In the table below, you'll see some decimal numbers in the left hand side. These are your targets. In the middle of the table, you see a row of black and red dots. These represent bits or binary numbers. Red means binary 1. Black means binary 0. Go ahead and mash one of the dots. It changes state -- 0 becomes 1, 1 becomes 0. Notice that the rightmost number also changes. It is the decimal number corresponding to the binary number.

Your job is to set the binary bits so that the right column matches the left column, if possible. This can be a little tricky for negative numbers as they are encoded in twos complement notation (not covered in lecture) as required by the Java virtual machine specification. First, find the positive number (i.e.,. if -127 find 127). Then reverse all the bits. Then, add 1 (Do binary addition).

Start off with eight bit numbers. These correspond to the Java byte type.

20
43
66
99
127
128

Now try out binary representations using 16 bits. These correspond to Java `short` integers.

128
201
-201
32767
-32768
-1
Instructor Checkoff ...

## Binary number checkoff

Ask your lab instructor to verify that you've pushed all the right buttons.

You will not be writing this program from scratch, we have done most of the hard work for you.

Download the jar file containing the partially completed program Expr.jar. Follow the instructions that you learned in the Netbeans Lab to load this file into a project called Expr. Here is a brief summary, in case you have forgotten:

1. Depress the shift key and right-click on the link to save the file to your hard drive. Save the file in your csci/201 folder.
2. Enter the following commands in your Linux session window to unjar the file:
```[user@mach dir] cd csci/201
[user@mach dir] jar xfv Expr.jar
```
3. If netbeans is not already running, give the command to start Netbeans in your Linux session window.
```[user@mach dir] netbeans &
```
4. Create a new Netbeans' project called Expr (Help).
5. Mount the Expr directory (Help). (In lab, always mount the directory containing the java file that you'll be working on.)
6. Open the file in Netbeans' editor by double-clicking on it in Netbeans' explorer window.
Instructor Checkoff ...
Show your lab instructor your Explorer window to make sure you have copied everything OK.

Java executes expressions according to the order of operations defined in Algebra. We will assume you know this by heart, but if you've forgotten, just remember that expressions inside of parenthesis are evaluated first, and that the operators: multiplication, division and remainder are evaluated before the operators: addition and subtraction.

An expression contains operators (and their operands) that must be evaluated. Usually expressions appear in statements of the form: (variable) = (expression). In such statements, the expression is evaluated and the result of the evaluation (a single value) is then assigned to the variable. This process is illustrated in the following Stack N' Heap:

Stack N' Heap What's This? The Stack N' Heap simulates a trace program. It shows the code being executed, the contents of the stack, and the contents of the heap. To move through the code, use the >> button. This will take you one step through the code. To go back one step, use the << button.

Primative variables are shown in the stack with their names on the left side and values on the right. Reference variables do not have any value on the right, but instead have a yellow line drawn to show which object in the heap it points to. Reference variables with a NULL value don't show anything on the right.
 Code int x; x = 100 / 5; int y; y = ((10 + x) * 20) % 18; Stack Heap Temporary

Compile and run the Expr class (Help). Notice that it prompts you for four numbers and then displays the result of evaluating expression 1 in the table below.

```Enter value for i> 5
Enter value for j> 6
Enter value for y> 7
Enter value for z> 8

i = 5
j = 6
y = 7.0
z = 8.0

Expression 1 is 7.5
```

Your job is to modify the main() method to print all of the ten expressions listed in the table below. Your program does not prompt the user for input for each of these ten expressions. It prompts the user for input once, and then calculates the values of the following ten expressions using the values of i, j, y, and z initially provided by the user.

Notice that the first expression is already done for you.

Expression
Number
Expression
1Average of `y` and `z`
2Average of `i` and `j`
3The remainder when `i` is divided by `j`
4```y2 + 4 j z```
5`-(y2 - z3)`
6```(y + z) / (y - z)```
7`i / (i + 1)`
8`i / (i + 1.0)`
9`(i / j) * j`
10`iy`
Hint: Use the pow method of the Math class.
Instructor Checkoff ...

After you have thoroughly tested your code, and you understand why the expressions print as they do, ask your instructor to test your code.