In: Computer Science
Your program should be in a new project named "Program 3: Number System Conversion" and should be in a package named "numberSystem". Your program must be in a file called "Program3NumberConversion.java". Your goal is to convert a number in some other number system (such as binary) to decimal. The base (also known as a radix) will have a maximum of 16. Before beginning your main method, write two other methods: public static int parseNumber (char letter) will basically be a big switch statement. There should be a case for each one of the digits (0-9) as well as a, A, b, B, c, C, d, D, e, E, f ,and F. Each of these should just return their numeric value: 0 is 0, 1 is 1, 2 is 2, ... a and A is 10, b and B is 11, etc. Note that none of these cases need a break statement since they are returning values. The heart of the matter is handled in public static int valueOfDigit(int place, int radix, int number). Remember that Math.pow returns a double, so the result will have to be cast to an integer before it is returned. The math is pretty simple, just consider the following example from the number 43205 . Remember to start with an index of 0 and to start from the right hand side of the number. If the 2nd place of the number in base 5 has value 3, it has a result of 3*(52) or 75. After you have computed the formula, perform the following output before returning the value for error checking: System.out.print(result+"+"); Your main method needs to follow this algorithm: Make a new Scanner named scanner bound to the keyboard Make a new String named response and set it equal to an empty String Do the following while response equals "y" (use equalsIgnoreCase) initialize an integer named sum and set it to 0 Prompt for a radix (or base) up to 16 and read it in Prompt for a number named inputNumber and read it in as a String (use scanner.next()) Start a for that run backwards through to characters of inputNumber. Do the following inside the for: Get the current character based on i Get the currentDigit based on the parseNumber of the result of the previous step Accumulate in sum the value of the digit. Make sure to compensate for the fact that your loop is running backwards. Now, just output " " + inputNumber+" in base "+radix+" converts to "+sum+" in base 10." Prompt the use to enter 'y' to enter another number to convert and read in their response close the scanner Here are some test cases for you to try: 1011 base 2 = 1+2+8 = 11
A54 base 16 = 4+80+2560 = 2644
5310 base 6 = 0+6+108+1080 = 1194
310 base 4 = 0+4+48 = 52
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU
NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR
YOU
import java.util.*;
class Program3NumberConversion {
public static int parseNumber(char x) {
switch (x) {
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
case 'f':
case 'F':
return 15;
}
//default return
return -1;
}
public static int valueOfDigit(int place, int radix, int number) {
int temp = (int) Math.pow(radix, place);
int result = temp * number;
System.out.print("\nfor " + (place + 1) + "th place result is : " + result);
return result;
}
public static void main(String[] args) {
String number;
int radix;
Scanner sc = new Scanner(System.in);
System.out.println("Enter no and radix(max 16) of that number : ");
number = sc.next();
radix = sc.nextInt();
int j = 0;
//no in decimal
int finalNumber = 0;
/*for each character conversion
and add result to finalNumber */
for (int i = number.length() - 1; i >= 0; i--) {
finalNumber += valueOfDigit(j, radix, parseNumber(number.charAt(i)));
j++;
}
System.out.println(
"\nNumber in base " +
radix +
" is : " +
number +
" & number in decimal : " +
finalNumber
);
}
}