1. Engineering
  2. Computer Science
  3. write a program that performs the following tasks display...

Question: write a program that performs the following tasks display...

Question details

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. For example:

C:> java BaseConversions ABCDE 25 8

will convert the input string ABCDE from base 25 into base 8 and display the result. We will accept any base from 2 to 36. Base 36 uses the integers 0-9 and the twenty-six letters A-Z / 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.

You will need to use the BigInteger class, which allows arbitrary-length integers. You may not use the built-in base conversion functions in Integer or BigInteger. The only operations allowed are add, subtract, multiply, divide, and mod. Your conversion algorithm is:

•   Check the validity of the input String
•   Convert the input String from the initial base into a BigInteger (in base 10).
•   Convert that base-10 BigInteger into a String representation of that value in the desired target base.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution