# CSCI 201 Lab 3 -- Binary numbers and Arithmetic Expressions

## Binary numbers

One of the things that you need to understand when writing programs is how different data types are stored in the computer. Here, we are going to look at how integers are stored in a binary representation which is the representation used in a 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 LED's. 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, be prepared to explain your answers.
20
43
66
99
127
128

Now try out binary representations using 16 bits.
128
256
511
2000
32767

## Lab Checkoff 1

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

## Arithmetic Expressions in Java

We are giving you a JCreator project to work on in this lab. The project is stored in a zip archive so you will use the program PowerZip to extract it. First, download a zipped copy of the Lab03 project by right-clicking on this link, and storing the target file in the C:\files directory. Now, you should be able to double-click on the Lab03.Zip icon in the C:\files directory to run the PowerZip program. On PowerZip's menu, hit the extract button, set the download directory to C:\files and then select ok to create:

C:\files\Lab03

Check to make sure that this folder exists before continuing.

Start up JCreator by opening the Lab03 folder and double-clicking on the ".jcw" file (i.e., Lab03.jcw). Once the project is open in JCreator, you should be able to execute the program by selecting the Execute Project option under the Build Menu of JCreator's toolbar. Run the program giving it the input that it requests.

Now, look in the FileView window and notice that this is a project that contains more than one class. In the FileView window, select the file MyClass.java which will open that file in the edit window. Notice that this class consists of a single method called TestFunc(), and that the method contains only one statement which is a call to System.out's println() method. (The main() method for this program is in the TestLab03 class). Look at the argument to the println() method (i.e., the information inside the ()'s), and then compare it to the information that is printed when the program is run. Notice that println() prints the final value of the expression that it receives as its argument.

Your job is to modify the method TestFunc() to print the eight expressions listed in the following table. Notice that the first expression is already done for you.

Expression
Number
Expression
1Average of y and z
2Average of i and j
3The reminder when i is divided by j
4
``` 2
y  +  4 j z
```
5
```   2   3
-(y - z )
```
6
``` y + z
-------
y - z
```
7
```   i
--------
i + 1
```
8
```   i
---------
i + 1.0
```
9
```
i
--- * j
j
```

## Lab Checkoff 2

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

## Extra Credit (3 extra credit points)

Write a program that prompts the user to enter 3 integer numbers and then prints their average, sum and product. You will need to close the Lab03 workspace and start a new project as described in Lab02. Do not use the MyClass.java from Lab03 as a model for your new program, instead use the program in Listing 2.13 of your text. The program in Listing 2.13 of your text uses the Keyboard class, and so will you if you want to make it easy to get the information that the user enters on the keyboard. You can download the Keyboard class from this link. Follow the instructions given in assignment 1 for adding the Keyboard class to your JCreator project.