Write a Java class “NumericConversion” that has the method “numericConverter” (s
ID: 3889845 • Letter: W
Question
Write a Java class “NumericConversion” that has the method “numericConverter” (see below). The “numericConverter” method will convert a positive integer in a specified base to another base. The function should work for bases 2 to 25, using capital alphabetic letters A-O for digits beyond 9. Values of the numbers to be converted should not exceed the equivalent of 263-1. You may not use any existing class or function to do the conversion; you must program the conversion algorithm yourself.
public static String numericConverter(String toBeConverted, int startingBase, int targetBase) {}
The “numericConverter” function will take a positive integer as a String and return the resulting positive integer as a String. Use startingBase for the base of the number to be converted and targetBase for the base being converted to. You may use a main method for testing purposes, but all of the conversion must take place within the “numericConverter” function. You may use helper functions to be called within the “numericConverter” function as you see fit.
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
public class AnyBaseConversion {
static int val(char c)
{
if (c >= '0' && c <= '9')
return (int)c - '0';
else
return (int)c - 'A' + 10;
}
// Function to convert a number from given base 'b'
// to decimal
static int toDeci(String str, int base)
{
int len = str.length();
int power = 1; // Initialize power of base
int num = 0; // Initialize result
int i;
// Decimal equivalent is str[len-1]*1 +
// str[len-1]*base + str[len-1]*(base^2) + ...
for (i = len - 1; i >= 0; i--)
{
// A digit in input number must be
// less than number's base
if (val(str.charAt(i)) >= base)
{
System.out.println("Invalid Number");
return -1;
}
num += val(str.charAt(i)) * power;
power = power * base;
}
return num;
}
// To return char for a value. For example '2'
// is returned for 2. 'A' is returned for 10. 'B'
// for 11
static char reVal(int num)
{
if (num >= 0 && num <= 9)
return (char)(num + '0');
else
return (char)(num - 10 + 'A');
}
// Utility function to reverse a string
static void strev(char []str)
{
int len = str.length;
int i;
for (i = 0; i < len/2; i++)
{
char temp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = temp;
}
}
// Function to convert a given decimal number
// to a base 'base' and
static String fromDeci(int base, int inputNum)
{
int index = 0; // Initialize index of result
// array to store result
char[] res = new char[100];
// Convert input number is given base by repeatedly
// dividing it by base and taking remainder
while (inputNum > 0)
{
res[index++] = reVal(inputNum % base);
inputNum /= base;
}
res[index] = '';
// Reverse the result
strev(res);
return new String(res);
}
public static String numericConverter(String toBeConverted, int startingBase, int targetBase) {
// first converting "toBeConverted" to decimal
int decimal = toDeci(toBeConverted, startingBase);
// converting decimal to target base
String target = fromDeci(targetBase, decimal);
return target;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
String num = sc.next();
System.out.print("Enter starting base: ");
int sb = sc.nextInt();
System.out.print("Enter target base: ");
int tb = sc.nextInt();
System.out.println("Number in base "+tb+" is: "+numericConverter(num, sb, tb));
sc.close();
}
}
/*
Sample output:
Enter number: A
Enter starting base: 16
Enter target base: 2
Number in base 2 is: 1010
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.