Question: Can you rewrite my program so that it doesn't use any of JAVA's built in base conversions? I ' m not allowed to use them
Can you rewrite my program so that it doesn't use any of JAVA's built in base conversions? Im not allowed to use them and I can't figure out how to do it
import java.math.BigInteger;
import java.util.Scanner;
public class converter
public static void mainString args
Scanner scanner new ScannerSystemin;
System.out.printlnWelcome to the Base Conversions Program!";
String value;
int initialBase, finalBase;
if argslength
value args;
initialBase Integer.parseIntargs;
finalBase Integer.parseIntargs;
else
System.out.printEnter the value to convert: ;
value scanner.next;
System.out.printEnter the initial base: ;
initialBase scanner.nextInt;
System.out.printEnter the final base: ;
finalBase scanner.nextInt;
if isValidIntegervalue initialBase
String result convertIntegervalue initialBase, finalBase;
System.out.printlnResult: result;
else
System.out.printlnError: Invalid input for the specified base.";
public static boolean isValidIntegerString theValue, int theBase
Check if theValue is a valid expression in theBase
for char digit : theValue.toCharArray
int digitValue Character.digitdigit theBase;
if digitValue digitValue theBase
return false; Invalid digit for the specified base
return true;
public static String convertIntegerString theValue, int initialBase, int finalBase
Convert theValue from initialBase to finalBase
BigInteger decimalValue convertToDecimaltheValue initialBase;
return convertFromDecimaldecimalValue finalBase;
private static BigInteger convertToDecimalString value, int base
BigInteger decimalValue BigInteger.ZERO;
BigInteger baseValue BigInteger.ONE;
for int i value.length; i ; i
int digit Character.digitvaluecharAti base;
decimalValue decimalValue.addbaseValuemultiplyBigIntegervalueOfdigit;
baseValue baseValue.multiplyBigIntegervalueOfbase;
return decimalValue;
private static String convertFromDecimalBigInteger decimalValue, int base
StringBuilder result new StringBuilder;
while decimalValuecompareToBigIntegerZERO
BigInteger quotientAndRemainder decimalValue.divideAndRemainderBigIntegervalueOfbase;
BigInteger quotient quotientAndRemainder;
BigInteger remainder quotientAndRemainder;
result.insert remainder.toStringbase;
decimalValue quotient;
return result.toString;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
