CS255 - Home/Project Assignment

## CS 255 - Computer Organization/Architecture I Homework 2

Due date: See Class Webpage

Observe the Emory Honor Code when doing homeworks.

1. "Nano" Numbers

The people on planet Neptune (let's suppose there are people on Neptune) use the "Nano" number system that is based on the number 9. These creatures use the following symbols for digits:

 ``` Digit # ! % @ ( ) [ ] \$ Value of the digit = 0 1 2 3 4 5 6 7 8 ```

Their number system is positional, meaning that when a number contains multiple digits, then the value of a digit is equal to 9 times the successor digit.

It happens that Neptunians use the minus sign '−' to denote negative values

Examples:

 ``` Nano number Value represent (in human decimal system) -------------- ------------------------------------------- ! 1 !# 9 !! 10 !% 11 !@ 12 !## 81 !#! 82 !#% 83 % 2 %# 18 %! 19 %% 20 (@ 39 (( 40 [[ 60 -[[ -60 And so on.... ```

2. Preparation

• To do this project, you must obtain a copy of the prepared files with these commands:

 ``` mkdir ~/cs255/hw2 cp /home/cs255001/Handouts/hw2/* ~/cs255/hw2 ```

• If you have Java install on your PC, and you want to work from home, you must download all the files in your ~/cs255/hw2 directory into your PC (you should create a cs255/hw2 directory in your PC to store the files)

If you do work from home on your PC:

 After you have completed the project, you must upload your program(s) into your ~/cs255/hw2 project directory in Emory so that you can turn in your code (using the command given at the end of this handout).

I do not accept email turnins !

• The files that you have copied into your ~/cs255/hw2 directory should be these:

 ``` hw2.java: The main program that is used to test the 2 methods that you need to write for this assignment Do NOT change this file Nano.java: The Java class file that implements the 2 conversion methods specified in this assignment ```

3. Assignment

Write a Java class Nano that contains the static methods:

 int parseNano(String s) and String toString(int value)

which allows you to convert between:

 "Nano" number representations (denoted as a string using the Nano digits, such as "@#!"), and int (which uses the 2's complement binary numbers) used by the computer.

The Nono class definition looks like this:

 ``` public class Nano { /* ========================================================== Return the 2's complement binary representation for the Nano number given in String s ========================================================== */ public static int parseNano(String s) { ... } /* ========================================================== Return the String of Nano digit that represent the value of the 2's complement binary number given in the input parameter 'value' ========================================================== */ public static String toString(int value) { ... } } ```

• The int parseNano(String s) method converts a "Nano" number (stored as String s) into a 2's complementary int number.

Examples:

 ``` parseNano("#") returns the int representation: 00000000 00000000 00000000 00000000 (value = 0) parseNano("!") returns the int representation: 00000000 00000000 00000000 00000001 (value = 1) parseNano("-!") returns the int representation: 11111111 11111111 11111111 11111111 (value = -1) parseNano("%") returns the int representation: 00000000 00000000 00000000 00000010 (value = 2) parseNano("-%") returns the int representation: 11111111 11111111 11111111 11111110 (value = -2) parseNano("!#") returns the int representation: 00000000 00000000 00000000 00001001 (value = 9) parseNano("!!") returns the int representation: 00000000 00000000 00000000 00001010 (value = 10) ```

(Recall that int uses the 2's complement number encoding scheme)

• The String toString(int value) method converts a 2's complementary int number (in the input parameter value) to a "Nano" number (which is represented as String of Nano digits)

Examples:

 ``` toString(0) returns the String: "#" toString(1) returns the String: "!" toString(2) returns the String: "%" toString(9) returns the String: "!#" toString(10) returns the String: "!!" ```

Note to students:

• Some students in previous classes thought that because we write:

 ``` Nano.toString(10) Nano.toString(16) Nano.toString( x ) ```

that the Nano.toString() method receives a decimal number as a parameter.

(decimal number = a positional representation for whole numbers based on the number 10)

That is wrong !!!

The data type int in Java contains a binary representation (everything stored in the computer are in binary) !!!

In fact, Java uses the 2's complement encoding to represent the int data type.

The following binary representations are passed as parameter in the method calls:

 ``` Java statement: input parameter (int = 4 bytes) ----------------------- --------------------------------------- Nano.toString(10) 00000000 00000000 00000000 00001010 Nano.toString(16) 00000000 00000000 00000000 00010000 ```

The reason is:

• The Java compiler is to help you write a program that the computer can execute.

This includes representation of numbers used in the program !!!

• Humans likes to see (use) a whole value represented in the decimal number system

So: the value ********** (= 10) is represented as 10 and we like to write it that way.

• The same whole value ********** (10 dots) must be represented inside the computer as: 00000000000000000000000000001010

• So when you write this in Java:

 ``` Nano.toString(10) // 10 in decimal ```

The Java compiler will translate the representation of 10 into: 00000000000000000000000000001010 !!!

4. Test programs

• The handout contains 3 test programs:

• ParseNanoTest.java:

• This program only tests the parseNano( ) method

 Use this test program when you are writing the parseNano( ) method

• Compile with:

 ``` javac ParseNanoTest.java ```

• Run test program with:

 ``` java ParseNanoTest ```

• ToStringTest.java:

• This program only tests the toString( ) method

 Use this test program when you are writing the toString( ) method

• Compile with:

 ``` javac ToStringTest.java ```

• Run test program with:

 ``` java ToStringTest ```

• hw2.jav:

 This program allow you to enter two arbitrary Nano numbers; add them and print the result out as a Nano number. Use hw2.java only when you pass the above test programs !!! See Section 5 on how to use hw2.java

5. Compile and run hw2.java

• To compile the hw2.java test program (after you have passed the ParseNano.java and ToStringTest.java tests), use:

 ``` javac hw2.java ```

• After the compilation is successful, you can run the program using:

 ``` java hw2 ```

The program will ask you to enter 2 Nano numbers and then:

 Print out the values of the Nano number in decimal And then print the sum in Nano number representation

Example 1: (my inputs are in red)

 ``` For your convenience, here are the Nano digits: Nano digit: # ! % @ ( ) [ ] \$ Value of digit: 0 1 2 3 4 5 6 7 8 Enter first Nano number x = @ The value of this Nano number in decimal system = 3 Enter second Nano number y = ) The value of this Nano number in decimal system = 5 The sum in decimal number system = 8 The sum in Nano number system = \$ ```

Example 2: (my inputs are in red)

 ``` For your convenience, here are the Nano digits: Nano digit: # ! % @ ( ) [ ] \$ Value of digit: 0 1 2 3 4 5 6 7 8 Enter first Nano number x = !%@ The value of this Nano number in decimal system = 102 Enter second Nano number y = )() The value of this Nano number in decimal system = 446 The sum in decimal number system = 548 The sum in Nano number system = [[\$ ```

Example 3: your program must handle negative values !!!

 ``` For your convenience, here are the Nano digits: Nano digit: # ! % @ ( ) [ ] \$ Value of digit: 0 1 2 3 4 5 6 7 8 Enter first Nano number x = !%(](\$ The value of this Nano number in decimal system = 75698 Enter second Nano number y = -\$\$@)#( (Negative value !!!!) The value of this Nano number in decimal system = -527476 The sum in decimal number system = -451778 The sum in Nano number system = -])][() ```

6. Turn in ********************************** You must do this !!!

• Turn in your Nano.java program SOURCE file using the commands:

 ``` cd ~/cs255/hw2 /home/cs255001/turnin Nano.java hw2 ```

• Extension request

• To request an extension for hw2, you must execute the following command before the deadline of the homework:

 ``` /home/cs255001/req-ext hw2 ```

You request will be successful if you have not exceeded the maximum number of "free" (no-questions-asked) requests allowed

Statement of Policy on Project/Homework Assignments

 ``` THIS CODE IS MY OWN WORK, IT WAS WRITTEN WITHOUT CONSULTING A TUTOR OR CODE WRITTEN BY OTHER STUDENTS - your name ```