## CS 1401 Assignment #5, Solutions

Date Assigned: Monday, February 19, or Tuesday, February 20, 2007.

Due Date: Monday, February 26, or Tuesday, February 27, 2007, before the beginning of your lab section.

Objective: The main objective of this assignment is to practice using loops.

Programming assignment: It is well known that computers use a binary system to represent integers. In some computer applications, it is also useful to use octal code (base 8), and hexadecimal (hex) code (base 16). In the past, some computers have used ternary code (base 3) as well. The following algorithm is used to convert a decimal positive integer n into the binary form: you divide n by 2, keep a remainder. Divide the result by 2, keep a remainder, etc. until we get 0. The remainders, when read bottom to top, form the desired binary number.

Similarly, you can convert a decimal positive integer n into the hex form: you divide n by 16, keep a remainder. Remainders 10, 11, 12, 13, 14, and 15 are described as, correspondingly, A, B, C, D, E, and F. Divide the result by 16, keep a remainder, etc. until we get 0. The remainders, when read bottom to top, form the desired hex number.

Write a program that, given an integer, uses the above algorithm to return a hex representation of this number (as a string).

Example of a binary code: for n = 13, we have

```13 / 2 = 6 rem 1
6 / 2 = 3 rem 0
3 / 2 = 1 rem 1
1 / 2 = 0 rem 1
```
When we read the remainders from bottom to top, we get 1101, which is exactly the binary representation of 13 -- since 1 * 8 + 1 * 4 + 0 * 2 + 1 * 1 = 8 + 4 + 1 = 13.

Example of a hex code: for n = 92, we have

```92 / 16 = 5 rem 12 which is C
5 / 16 = 0 rem 5
```
When we read the remainders from bottom to top, we get 5C, which is exactly the binary representation of 92 -- since 5 * 16 + 12 = 80 + 12 = 92.

Hint: originally, the division-result is 92, the string-so-far is empty. Within the loop, you divide the division-result by 16, compute the remainder, and add the corresponding one-character string ("0", "1", ..., "9", "A", ..., "F") to the left of the string-so-far.

```
Solution:

import java.util.*;

public static void main(String [] args){
//designing a tool to read from a keyboard
Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter a positive decimal integer");
int decimal = keyboard.nextInt();

//declaration and initialization of variables
String string_so_far = "";
int remainder;
String corresponding_symbol = "";
int division_result = decimal;

while (division_result > 0){
//dividing by 16 and computing the remainder
remainder = division_result % 16;
division_result /= 16;

//forming a symbol based on the remainder;
if (remainder < 10)
{corresponding_symbol = "" + remainder;}
else{
if (remainder == 10) corresponding_symbol = "A";
if (remainder == 11) corresponding_symbol = "B";
if (remainder == 12) corresponding_symbol = "C";
if (remainder == 13) corresponding_symbol = "D";
if (remainder == 14) corresponding_symbol = "E";
if (remainder == 15) corresponding_symbol = "F";
}
//adding the symbol in front of the string-so-far
string_so_far = corresponding_symbol + string_so_far;
}

//printing the result of conversion
System.out.println("The hex code of a decimal number " + decimal +
" is " + string_so_far);
}

```
For extra credit: write a program that transforms hex numbers back into decimal. Main idea:
• start with the first (0th, in Java terms) symbol of the string, and make value-so-far 0, 1, ..., 9, 10, ..., 15 depending on whether this symbol is 0, 1, ..., 9, A, ..., or F; then, delete this symbol from the string;
• at any step, if you have one more symbol, you multiply the value-so-far by 16 and add the corresponding hex digit to the resulting value;
• stop when there are no symbols left.

Example: for the string 5C,

• the first symbol is 5, so the corresponding value-so-far is 5 and the remaining string is C;
• the remaining string is not empty, so we take the first symbol of the remaining string, it is C; we multiply the value-so-far by 16, it is now 5 * 16 = 80, and add the value (12) corresponding to this symbol C, we now get 80 + 12 = 92; the remaining string is empty.
Since the remaining string is empty, the value-so-far (92) is returned as the result.

```
Solution:

public static void main(String [] args){
//designing a tool to read from a keyboard
Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter a number in hex code");
String hex = keyboard.next();

//declaration and initialization of variables
String remaining_string = hex;
int value_so_far = 0;
char symbol;
int corresponding_value;

while (remaining_string.length() > 0){
//separating the first (in Java, 0th) character of the string
symbol = remaining_string.charAt(0);
remaining_string =
remaining_string.substring(1, remaining_string.length());

//forming a value corresponding to the symbol
if (symbol == 'A')
{corresponding_value = 10;}
else if (symbol == 'B')
{corresponding_value = 11;}
else if (symbol == 'C')
{corresponding_value = 12;}
else if (symbol == 'D')
{corresponding_value = 13;}
else if (symbol == 'E')
{corresponding_value = 14;}
else if (symbol == 'F')
{corresponding_value = 15;}
else
{corresponding_value = symbol - '0';}

//updating the value-so-far
value_so_far = value_so_far * 16 + corresponding_value;
}

//printing the result of conversion
System.out.println("The decimal representation of a hex number " +
hex + " is " + value_so_far);
}

}

```
Homework assignment: on a separate sheet of paper, solve Ex. 6, 10, 18, and 24 at the end of Chapter 5.

Deliverables: as instructed by your TA.