Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Base Conversions IN JAVA (Please dont use built in function) Don't copy from others please Write a program that performs the following tasks: Display a

Base Conversions IN JAVA (Please dont use built in function)

Don't copy from others please

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 functions:

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 Java 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Database Technology Edbt 88 International Conference On Extending Database Technology Venice Italy March 14 18 1988 Proceedings Lncs 303

Authors: Joachim W. Schmidt ,Stefano Ceri ,Michele Missikoff

1988th Edition

3540190740, 978-3540190745

More Books

Students also viewed these Databases questions

Question

What are the consolidated financial statements?

Answered: 1 week ago

Question

1. Which position would you take?

Answered: 1 week ago