Question

Write a program that performs the following tasks: Display a friendly greeting to the user Prompt...

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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;
import java.math.BigInteger;
public class Main
{
private int x;

public static boolean isValidInteger(String theValue, int theBase)
{

BigInteger number = new BigInteger(theValue,theBase);
if(theValue.equals(number.toString(theBase)))
{
return true;
}
else
return false;
}
public static String convertInteger(String theValue, int initialBase, int finalBase)
{
boolean valid=isValidInteger(theValue,initialBase);
if(valid==true)
{
BigInteger number = new BigInteger(theValue);
return(number.toString(finalBase));
}
else
return("String not in initial base");

}
public static void main(String[] args)
{
System.out.println("Welcome");
String value;
int ibase, dbase;
if(args.length==0)
{
Scanner S = new Scanner(System.in);
System.out.println("Enter Value to be converted :: ");
value = S.nextLine();
System.out.println("Enter initial base :: ");
ibase = S.nextInt();
System.out.println("Enter desired base :: ");
dbase = S.nextInt();
}
else
{
value=args[0];
ibase=Integer.parseInt(args[1]);
dbase=Integer.parseInt(args[2]);
}
   System.out.println("Change base "+convertInteger(value,ibase,dbase));

}
}
Output

Add a comment
Know the answer?
Add Answer to:
Write a program that performs the following tasks: Display a friendly greeting to the user Prompt...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java programming How do i change this program to scan data from file and find the...

    Java programming How do i change this program to scan data from file and find the sum of the valid entry and count vaild and invalid entries The provided data file contains entries in the form ABCDE BB That is - a value ABCDE, followed by the base BB. Process this file, convert each to decimal (base 10) and determine the sum of the decimal (base 10) values. Reject any entry that is invalid. Report the sum of the values...

  • Complete the Python program below that performs the following operations. First prompt the user to input...

    Complete the Python program below that performs the following operations. First prompt the user to input two integers, n and m. If n<m, then print the odd positive integers that are less than m (in order, on a single line, separated by spaces). If man, then print the even positive integers that are less than n (in order, on a single line, separated by spaces). If neem, then print nothing. For instance, if the user enters 5 followed by 10,...

  • 1. Prompt the user for one of the arithmetic operators ('op'): +, -, *,/, or **...

    1. Prompt the user for one of the arithmetic operators ('op'): +, -, *,/, or ** 2. Prompt the user for two input integers ('a' and'b') 3. The input numbers can be positive or negative 4. Perform the arithmetic operation using the two input numbers 5. The first entered number ('a') is the left side, the second ('b') the right side of the operation For exponentiation, the first number is the base, the second the exponent Print (display) the following...

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • Write a Java program that performs these operations Prompt the user to enter a low height...

    Write a Java program that performs these operations Prompt the user to enter a low height in feet (an integer) Prompt the user to enter a high height in feet (an integer) Prompt the user to enter a low weight in pounds (an integer) Prompt the user to enter a high weight in pounds (an integer) Print a table of Body Mass Index (BMI) for the heights and weights entered, ranging from the low height to the high height (inclusive),...

  • Write a complete program that will prompt the user for an integer You must keep asking...

    Write a complete program that will prompt the user for an integer You must keep asking the user until they give you a number that is greater than 700. Once they give a valid number, display all multiples of 7 from 7 to that number inclusively). You may assume the user will give you correct type of input, but your code must continue to prompt them until they give a number greater than 700.

  • Prompt the user to enter two character strings. The program will then create a new string...

    Prompt the user to enter two character strings. The program will then create a new string which has the string that appears second, alphabetically, appended to the end of the string that shows up first alphabetically. Use a dash (-') to separate the two strings. You must implement the function string appendStrings(string, string), where the return value is the combined string. Display the newly created string. Your program must be able to handle both uppercase and lowercase characters. You are...

  • C++ Write a complete program that will prompt the user for a digit in the range...

    C++ Write a complete program that will prompt the user for a digit in the range 5 to 15. Using that digit to determin scale display the graphic shown below. Your program must use a nested loop. You may not use the string class SAMPLE RUN: Input an integer in the range 5 to 20: 100 Incorrect. The number is out of range. Input an integer in the range 5 to 20: 10 * ** *** **** ***** ****** *******...

  • Write a program that performs the following: 1. Presents the user a menu where they choose...

    Write a program that performs the following: 1. Presents the user a menu where they choose between:              a. Add a new student to the class                           i. Prompts for first name, last name                           ii. If assignments already exist, ask user for new student’s scores to assignments              b. Assign grades for a new assignment                           i. If students already exist, prompt user with student name, ask them for score                           ii. Students created after assignment will need to...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT