Gettysburg College

CS 221
Computer Organization and Assembly Language Programming

Spring 2023

Assignment 1

Due: Thu, Jan 26, by 11:59pm

Readings

Description

This assignment will focus on conversion between different base. The algorithms we discussed are:

Preliminaries

Create Java project called Converter with class Converter that contains the methods listed below.

Recall the following properties of integer division:

Required Methods

Below is the list of required methods. All methods are public, static.

The following should not be used in this assignment:

long from10(long number, int base)

Converts the given number, which is in Base10, to its equivalent value in the given base which is assumed to be in [2..10].

For testing show at least bases 2, 3, 7, 8, 10 with at least one case of 1-,2-,...,5-digit numbers. You can try longer numbers, just keep in mind that base 2 will not work after ~50,000.

Hint (highlight space below if you want to read):

To collect the remainders in the correct order keep a variable that changes 1, 10, 100, 1000, ..., i.e. corresponds to position 1, 2, 3, ... . Update the result by adding to it the positioned remainder. For example, if current result is 261 and current remainder is 7, then result becomes 7*1000+261 = 7261 (position must be 1000 at this point).

Example test cases:

System.out.println( "9 in base 2 is " + from10( 9, 2 ) );  // 1001
System.out.println( "9 in base 7 is " + from10( 9, 7 ) );  // 12
String from10(long number)

Same as previous, but the base is fixed to 16 and the result is of type String.

There is no need to keep a position variable -- simply glue the new remainder to the front as you update the result.

For testing show at least one case of 1-,2-,...,5-digit numbers with a result that contains some of the letters.

Copy the following method which will return the hexadecimal digit 0..F given a decimal value 0..15:

public static char getSymbol(long digit)
{
    if (digit >= 0 && digit <= 9) {
        return (char)('0' + digit);
    }
    else {
        return (char)('A' + (digit - 10));
    }
}

Example test cases:

System.out.println( "13 in base 16 is " + from10( 13 ) );  // D
System.out.println( "20 in base 16 is " + from10( 20 ) );  // 14
long to10(long number, int base)

Converts the given number from the given base to Base10. The given base is assumed to be in [2..10].

For testing at least convert back the test cases for method from10(long,int).

Example test cases:

System.out.println( "1001 in base 2 is " + to10( 1001, 2 ) );  // 9
System.out.println( "12 in base 7 is " + to10( 12, 7 ) );      // 9
long to10(String number)

Same as previous, but the base is fixed to 16 and the given number is of type String.

For testing at least convert back the test cases for method from10(String).

Copy the following method which will return the decimal value 0..15 given a hexadecimal digit 0..F:

public static long getValue(char symbol)
{
    if (symbol >= '0' && symbol <= '9') {
        return symbol - '0';
    }
    else {
        return (symbol - 'A') + 10;
    }
}

Example test cases:

System.out.println( "D in base 16 is " + to10( "D" ) );    // 13
System.out.println( "14 in base 16 is " + to10( "14" ) );  // 20
HashMap<Character, Integer> buildRomanMap()

Returns a hash map that contains the "single" Roman "digits" and their decimal equivalents, i.e. only rows 1 and 5 in the following table:

Wikipedia: Roman Numerals, Standard Form.

Java HashMap is created and used as follows:

HashMap<Character, Integer> theNameOfMap = new HashMap<Character, Integer>();   // import java.util.HashMap;

theNameOfMap.put( symbol, value );   // CS111:  map[symbol] = value;

value = theNameOfMap.get( symbol );  // CS111:  value = map[symbol];
int fromRoman(String number)

Converts the given Roman numeral to its equivalent in Base 10 using the following algorithm:

Process the given number from left to right. If the value of the current Roman digit is smaller than the value of the next Roman digit, subtract the value of the current Roman digit; otherwise, add the value of the current Roman digit.

For testing, show the conversion of the Roman Numerals:

  • in rows 4 and rows 6 in the table
  • the 13 examples after the table (all are given in bullets except one)

Example test cases:

System.out.println( "V is " + fromRoman( "V" ) );    // 5


What to turn in

Upload Converter.java to the Assignment 1 Moodle dropbox.