This lab will introduce you to how Java programs work with numbers and expressions
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.
| Target | Your guess |
|---|---|
| 20 | |
| 43 | |
| 66 | |
| 99 | |
| 127 | |
| 128 |
Now try out binary representations using 16 bits.
These correspond to Java short integers.
| Target | Your guess |
|---|---|
| 128 | |
| 201 | |
| -201 | |
| 32767 | |
| -32768 | |
| -1 |
Ask your lab instructor to verify that you've pushed all the right buttons.
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:
[user@mach dir] cd csci/201 [user@mach dir] jar xfv Expr.jar
[user@mach dir] netbeans &
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:
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 |
|---|---|
| 1 | Average of y and z |
| 2 | Average of i and j |
| 3 | The 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. |
After you have thoroughly tested your code, and you understand why the expressions print as they do, ask your instructor to test your code.