Converter
with class Converter
that contains the methods listed below.
Recall the following properties of integer division:
x / y
will produce an integer and ignore the fractional part; when y=10
this has the effect of removing the last digitx % y
will produce the remainder of the divisionpublic, static
.
The following should not be used in this assignment:
Math.pow()
String
in methods whose signature does not mention String
XXX.parseXXX
StringBuilder
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 :
Example test cases:public static char getSymbol(long digit) { if (digit >= 0 && digit <= 9) { return (char)('0' + digit); } else { return (char)('A' + (digit - 10)); } } 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 :
Example test cases:public static long getValue(char symbol) { if (symbol >= '0' && symbol <= '9') { return symbol - '0'; } else { return (symbol - 'A') + 10; } } 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:
System.out.println( "V is " + fromRoman( "V" ) ); // 5 |
Converter.java
to the Assignment 1 Moodle dropbox.