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 division; when y=10
this has the effect of producing the last digitpublic static <E1, E2> void assertEquals( E1 a, E2 b ) { String strA = Objects.toString( a ); String strB = Objects.toString( b ); if ( !strA.equals( strB ) ) { throw new RuntimeException(); } }
public, static
.
The following should not be used in this assignment:
Math.pow
and similarString
in methods whose signature does not mention String
XXX.parseXXX
StringBuilder
System.out.println
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 (if you want to see the hint, highlight the space below):
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: assertEquals( from10( 9, 2 ), 1001 ); assertEquals( 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(int digit) { if (digit >= 0 && digit <= 9) { return (char)('0' + digit); } else { return (char)('A' + (digit - 10)); } } assertEquals( from10( 13 ), "D" ); assertEquals( 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:
assertEquals( to10( 1001, 2 ), 9 ); assertEquals( 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 int getValue(char symbol) { if (symbol >= '0' && symbol <= '9') { return symbol - '0'; } else { return (symbol - 'A') + 10; } } assertEquals( to10( "D" ), 13 ); assertEquals( 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:
assertEquals( fromRoman( "V" ), 5 ); |
Converter.java
to the Moodle dropbox.