# CSCI 201 Lab 3 -- Numbers and expressions

## Binary numbers

Programmers need to understand a bit about how different data types are stored in the computer. Here we are going to look at how Java's 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 as required by the Java virtual machine specification. If you first find the related positive number and then "flip" all the bits, you will be very warm.

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

### Binary number checkoff

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

## Another word on jar files

Hopefully you remember from the last lab that a collection of Java programs can be stored in a jar, Java archive, file. In fact the twos complement applet that you just ran is stored in a jar file named TwosComp.jar which contains two class files: One draws the circles, and the other processes your mouse clicks.

In many CSCI 201 labs you will work from a partially-completed project that has been stored in a jar file. In most labs you will complete two projects, but today you have only one to finish.

We will not give detailed directions for un-jar-ing every week. You are expected to master this skill! If you are in need of a refresher course, review the section Downloading a Java Archive from Lab 2.

## Arithmetic Expressions in Java

We are going to start you off with a jar-file containing a nearly complete jGRASP project. The jar file is called Lab03.jar and can be retrieved via this link. Remember, you need to save the file on your computer and then start up jGRASP to extract its contents.

Windows users should create a directory called C:\Files\Lab03 and extract the components of the jar file into that directory. Linux users should create the directory csci/201/Lab03 and extract the files there.

Once you have extracted the files, connect to your newly created Lab03 directory and open the project file Lab03.gpj .

### Jar extraction setup checkoff

Show your lab instructor the Browse and Project panels of jGRASP. The should resemble the panels shown below.

 Linux Windows

### Initial project files

In the browse panel, you should see a file PromptIO.class followed by a directory called either edu/unca/cs/csci201/LabAids/ or edu\unca\cs\csci201\LabAids\ depending on your operating system. This file is a compiled Java class provided by the CSCI 201 instructors as an "aid" for this lab. You'll see many files like this throughout the semester.

Now go to the Project panel and double-click on the file Lab03.java. Note that this file starts with a line that imports the class edu.unca.cs.csci201.LabAids.PromptIO. This is the same PromptIO you saw in the Project panel. The PromptIO class is similar to the Keyboard class used in your textbook. It does the heavy lifting required to process keyboard input in Java.

Compile and run the Lab03 class. Notice that it prompts you for four numbers and then displays an expression as shown 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 following table. Your program does not prompt the user for each of these ten. It prompts the user once and then calculates the values of the following ten expressions using the values of `i`, `j`, `y`, and `z` 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 Math class.

### Code Execution 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.