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 1When 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 5When 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);
//asking the user to input a number and reading this number
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: Example: for the string 5C,
Solution:
public static void main(String [] args){
//designing a tool to read from a keyboard
Scanner keyboard = new Scanner(System.in);
//asking the user to input a number and reading this number
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.