Programming 5. Numeral Systems in Computing and Java Methods
This is a revisit of the numeral systems. In this project, you are to develop a “robust” program that convert a user-entered decimal number to a hexadecimal number, and convert a user-entered hexadecimal number to a decimal number, based on the user’s choice. The program is “robust” against rogue inputs.
You are required to design several Java methods and use them in your program. For this, we divide the project into several exercises.
Exercise 5.1 Java Method to Convert Decimal Number to Hexadecimal Number
You are to implement the following Java method:
public static String convertToHexadecimal(String decText)
Given that decText
contains a valid String representation of a decimal number, the method returns the
string that represents the hexadecimal number for the decimal number. Here the string in decText
is valid
if it contains only decimal digits (i.e., 0 - 9) and is no longer than 18 digits.
Exercise 5.2 Java Method to Convert Hexadecimal Number to Decimal Number
You are to implement the following Java method:
public static long convertToDecimal(String hexText)
Given that hexText
contains a valid String representation of a hexadecimal number, the method returns the
string that represents the decimal number for the hexadecimal number. Here the string in hexText
is valid
if it contains only hexadecimal digits (i.e., 0 - 9 and A - F) and is no longer than 15 digits.
Exercise 5.3 Java Method to Check Validity of User’s Input for Menu Options
You are to implement a Java method:
public static int checkUserOptionInput(String input)
The final program you are to complete must have a text menu where a valid menu option can only be either
0, 1, or 2. Any user input for the menu option other than these three values are invalid. This method checks
whether the string in input
after the leading and trailing white spaces are trimmed constitutes a valid
user input for the menu option. It returns integer either 0, 1, or 2 if the input is valid; -1 otherwise.
Exercise 5.4 Java Method to Check Validity of User’s Input for Decimal Numbers
You are to implement a Java method:
public static boolean isValidDecimalNumberInput(String input)
This method is to check whether the user’s input in input
constitutes a valid decimal number. The input
is valid if after the leading and trailing white spaces are trimmed, the input is no longer than 18 characters, contains only decimal digits (i.e., 0 - 9). The method returns true
if the input is valid; false
otherwise.
Exercise 5.5 Java Method to Check Validity of User’s Input for Hexadecimal Numbers
You are to implement a Java method:
public static boolean isValidHexadecimalNumberInput(String input)
This method is to check whether the user’s input in input
constitutes a valid hexadecimal number. The input
is valid if after the leading and trailing white spaces are trimmed, the input is no longer than 15
characters, contains only hexadecimal digits (i.e., 0 - 9, A - F). The method returns true
if the input is
valid; false
otherwise.
Subproject 5.6 Robust Handling of Conversion between Decimal and Hexadecimal Numbers
In this
subproject, you are to create a program called RobustNumberConverter
. This program
should display a text menu (as exhibited in the running examples below), and convert
a hexadecimal number to a decimal number or vice versa, based on the user’s option.
Make sure you meet the following requirements:
- The program should be robust against rogue user inputs.
- Use all the methods from Exercises 5.1 - 5.5 in the program
The following is a test run of the program:
$ java RobustNumberConverter
1. Convert hexadecimal number to decimal
2. Decimal decimal number to hexadecimal
0. Exit
Enter option (0, 1, or 2) and hit enter: 99
1. Convert hexadecimal number to decimal
2. Decimal decimal number to hexadecimal
0. Exit
Enter option (0, 1, or 2) and hit enter: 1
Enter an valid hexadecimal number (no more than 15 digits): ABCEDEF
Hexadecimal number ABCEDEF is decimal number 180153839.
1. Convert hexadecimal number to decimal
2. Decimal decimal number to hexadecimal
0. Exit
Enter option (0, 1, or 2) and hit enter: 1
Enter an valid hexadecimal number (no more than 15 digits): 1234567890ABCDEF
Enter an valid hexadecimal number (no more than 15 digits): 1234567890ABCDE
Hexadecimal number 1234567890ABCDE is decimal number 81985529205931230.
1. Convert hexadecimal number to decimal
2. Decimal decimal number to hexadecimal
0. Exit
Enter option (0, 1, or 2) and hit enter: 2
Enter an valid decimal number (no more than 18 digits): 345
Decimal number 345 is hexadecimal number 159.
1. Convert hexadecimal number to decimal
2. Decimal decimal number to hexadecimal
0. Exit
Enter option (0, 1, or 2) and hit enter: 999
1. Convert hexadecimal number to decimal
2. Decimal decimal number to hexadecimal
0. Exit
Enter option (0, 1, or 2) and hit enter: FF
1. Convert hexadecimal number to decimal
2. Decimal decimal number to hexadecimal
0. Exit
Enter option (0, 1, or 2) and hit enter: 2
Enter an valid decimal number (no more than 18 digits): 999
Decimal number 999 is hexadecimal number 3E7.
1. Convert hexadecimal number to decimal
2. Decimal decimal number to hexadecimal
0. Exit
Enter option (0, 1, or 2) and hit enter: 2
Enter an valid decimal number (no more than 18 digits): 123456789012345678
Decimal number 123456789012345678 is hexadecimal number 1B69B4BA630F34E.
1. Convert hexadecimal number to decimal
2. Decimal decimal number to hexadecimal
0. Exit
Enter option (0, 1, or 2) and hit enter: 1234567890123456789
1. Convert hexadecimal number to decimal
2. Decimal decimal number to hexadecimal
0. Exit
Enter option (0, 1, or 2) and hit enter: 2
Enter an valid decimal number (no more than 18 digits): 123456789012345678
Decimal number 123456789012345678 is hexadecimal number 1B69B4BA630F34E.
1. Convert hexadecimal number to decimal
2. Decimal decimal number to hexadecimal
0. Exit
Enter option (0, 1, or 2) and hit enter: 2
Enter an valid decimal number (no more than 18 digits): 1234567890123456789
Enter an valid decimal number (no more than 18 digits): 987654321012345678
Decimal number 987654321012345678 is hexadecimal number DB4DA5F44D20B4E.
1. Convert hexadecimal number to decimal
2. Decimal decimal number to hexadecimal
0. Exit
Enter option (0, 1, or 2) and hit enter: 0
Exit the program