Question: Number Conversions: Using the NumberConverter.java example provided, write a java program that prompts the user for a type of number 1)binary, 2) Octal, 3) Decimal,

Number Conversions: Using the NumberConverter.java example provided, write a java program that prompts the user for a type of number 1)binary, 2) Octal, 3) Decimal, 4) Hexadecimal and then outputs the result when the number is converted to the other 3 types.

For example: Enter Type of Number to Convert:

1) Binary

2) Octal

3) Decimal

4) Hexadecimal

1) Enter Binary Number: 10101

10101 Converted is:

25 Octal

21 Decimal

15 Hex

Note: Your Program should use proper exception handling for invalid inputs and include javadoc comments

"NumberConverter.java"

package numberconverter; import java.util.Scanner; /** @author sheehanj * Java Program to Convert Numbers from Binary, Hex, and Octal */ public class NumberConverter { /** Convert from Binary to Decimal * @param BinaryNumber a String representing ones and zeros * @throws InvalidNumberException Number is not Binary */ public static int BinaryToDecimal(String BinaryNumber) { int result = 0; int power = BinaryNumber.length() - 1; int index = 0; while (index < BinaryNumber.length()) { if (BinaryNumber.charAt(index)=='1') result = result + 1 * (int) (Math.pow(2, power)); index++; // look at the next digit power--; // change the power of 2 to next digit } return result; } /** Convert from Decimal to Binary * @param DecimalNumber an integer we wish to convert * @throws InvalidNumberException Number is not an int */ public static String DecimalToBinary (int DecimalNumber) { String result = ""; // Calculate Largest Positional Value int position = 10; // arbitary assumption //int index = 0; while (DecimalNumber / Math.pow(2, position) < 1) { position--; } while (position>=0) { if ( DecimalNumber / (int) Math.pow(2, position)==1) result = result + "1"; else result = result + "0"; DecimalNumber = DecimalNumber % (int) Math.pow(2, position); position--; } return result; } /** Convert from Octal to Decimal * @param OctalNumber a String representing a number with digits from 0 to 7 * @throws InvalidNumberException Number is not Octal */ public static int OctalToDecimal(String OctalNumber) { int result = 0; int power = OctalNumber.length() - 1; int index = 0; while (index < OctalNumber.length()) { switch (OctalNumber.charAt(index)) { //case '0': result = result + 0 * (int) (Math.pow(8, power)); break; case '1': result = result + 1 * (int) (Math.pow(8, power)); break; case '2': result = result + 2 * (int) (Math.pow(8, power)); break; case '3': result = result + 3 * (int) (Math.pow(8, power)); break; case '4': result = result + 4 * (int) (Math.pow(8, power)); break; case '5': result = result + 5 * (int) (Math.pow(8, power)); break; case '6': result = result + 6 * (int) (Math.pow(8, power)); break; case '7': result = result + 7 * (int) (Math.pow(8, power)); break; } index++; // look at the next digit power--; // change the power of 8 to next digit } return result; } /** Convert from Hex to Decimal * @param HexNumber a String representing a number with digits from 0-9 and A-F * @throws InvalidNumberException Number is not Hex */ public static int HexToDecimal(String HexNumber) { int result = 0; int power = HexNumber.length() - 1; int index = 0; HexNumber.toUpperCase(); while (index < HexNumber.length()) { switch (HexNumber.charAt(index)) { case '1': result = result + 1 * (int) (Math.pow(16, power)); break; case '2': result = result + 2 * (int) (Math.pow(16, power)); break; case '3': result = result + 3 * (int) (Math.pow(16, power)); break; case '4': result = result + 4 * (int) (Math.pow(16, power)); break; case '5': result = result + 5 * (int) (Math.pow(16, power)); break; case '6': result = result + 6 * (int) (Math.pow(16, power)); break; case '7': result = result + 7 * (int) (Math.pow(16, power)); break; case '8': result = result + 8 * (int) (Math.pow(16, power)); break; case '9': result = result + 9 * (int) (Math.pow(16, power)); break; case 'A': case 'a': result = result + 10 * (int) (Math.pow(16, power)); break; case 'B': case 'b': result = result + 11 * (int) (Math.pow(16, power)); break; case 'C': case 'c': result = result + 12 * (int) (Math.pow(16, power)); break; case 'D': case 'd': result = result + 13 * (int) (Math.pow(16, power)); break; case 'E': case 'e': result = result + 14 * (int) (Math.pow(16, power)); break; case 'F': case 'f': result = result + 15 * (int) (Math.pow(16, power)); break; } index++; // look at the next digit power--; // change the power of ? to next digit } return result; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("Enter Type of Number to Convert: " + "1) Binary " + "2) Octal " + "3) Decimal " + "4) Hexadecimal " + ""); int choice = input.nextInt(); switch(choice) { case 1: // Convert from Binary System.out.print("Enter Binary Number: "); String binaryNumber = input.next(); System.out.printf("Converted from Binary %d%n", BinaryToDecimal(binaryNumber)); break; case 2: // Covert from Octal System.out.print("Enter Octal Number: "); String octalNumber = input.next(); System.out.printf("Converted from Octal %d%n", OctalToDecimal(octalNumber)); break; case 3: // convert from Decimal System.out.print("Enter Decimal Number: "); int decimalNumber = input.nextInt(); //System.out.printf("Converted from Hex %d%n", HexToDecimal("12f")); System.out.printf("Converted from Decimal %s%n", DecimalToBinary(decimalNumber)); break; case 4: // Convert from Hexidecimal // Add your code here break; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!