Question: JAVA PROGRAMMING: Write a program that performs the following tasks: Display a friendly greeting to the user Prompt the user for the value to convert
JAVA PROGRAMMING:
Write a program that performs the following tasks:
Display a friendly greeting to the user
Prompt the user for the value to convert (a String)
Accept that String
Prompt the user for the value of the initial base (an integer)
Accept that integer
Prompt the user for the desired base of the output (an integer)
Accept that integer
If the String is not a legal expression of a number in the initial base, display an error message and exit the program.
Convert the value represented by the String in the initial base to the desired base.
Display the result.
The program should also accept all three parameters from the command line if provided. Command-line parameters are accepted in the same order as the input prompts. We will accept any base from 2 to 36. Base 36 uses the ten integers 0-9 and the twenty-six letters A-Z.
The validator and the actual conversion routine should be contained in methods:
public static boolean isValidInteger(String theValue, int theBase){
// contract: returns true if theValue is a valid expression in theBase;
// false otherwise.
public static String convertInteger(String theValue, int initialBase, int finalBase){
// contract: converts theValue from initialBase to finalBase and returns the
// result as a String.
// precondition: theValue must be a valid expression in initialBase.
Note that for every method that you write, you should state the contract and any preconditions as shown above. If the preconditions arent met, the method should throw an exception but we dont know how to do that yet, so just exit the program.
You will need to use the BigInteger class, which allows arbitrary-length integers. So your conversion algorithm is:
Check the validity of the input String
Convert the input String into a BigInteger (in base 10) as shown in class.
Convert that BigInteger into a String representation of that value in the desired target base as shown in class.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
