Question
NumberConverter.java package numberconverter; import java.util.Scanner; /** @author * Java Program to Convert Numbers from Binary, Hex, and Octal */ public class NumberConverter { /** Convert
"NumberConverter.java" package numberconverter; import java.util.Scanner; /** @author * 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 =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 (indexthese are some other codes i have, but i dont think this is the right way to do it like the example above. i need it to look like the example above
"decimal"
package numberconverter;
import java.util.Scanner;
public class Decimal { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long binaryNumber, decimalNumber = 0, j = 1, remainder; System.out.print("Input a binary number: "); binaryNumber = sc.nextLong();
while (binaryNumber != 0) { remainder = binaryNumber % 10; decimalNumber = decimalNumber + remainder * j; j = j * 2; binaryNumber = binaryNumber / 10; } System.out.println("Decimal Number: " + decimalNumber); } }
"hexadecimal"
package numberconverter;
import java.util.Scanner;
public class HexaDecimal { public static void main(String[] args) { int[] hex = new int[1000]; int i = 1, j = 0, rem, dec = 0, bin; Scanner in = new Scanner(System.in); System.out.print("Input a Binary Number: "); bin = in.nextInt(); while (bin > 0) { rem = bin % 2; dec = dec + rem * i; i = i * 2; bin = bin / 10; } i = 0; while (dec != 0) { hex[i] = dec % 16; dec = dec / 16; i++; } System.out.print("HexaDecimal value: "); for (j = i - 1; j >= 0; j--) { if (hex[j] > 9) { System.out.print((char)(hex[j] + 55)+" "); } else { System.out.print(hex[j]+" "); } } } }
"octal"
package numberconverter; import java.util.*;
public class Octal {
public static void main(String[] args) {
int binnum, binnum1,rem, decnum=0, quot, i=1, j;
int octnum[] = new int[100];
Scanner scan = new Scanner(System.in); System.out.print("Input a Binary Number : ");
binnum = scan.nextInt();
binnum1=binnum;
while(binnum > 0) { rem = binnum % 10; decnum = decnum + rem*i; //System.out.println(rem); i = i*2; binnum = binnum/10; }
i=1; quot = decnum; while(quot > 0) { octnum[i++] = quot % 8; quot = quot / 8; } System.out.print("Equivalent Octal Value of " +binnum1+ " is :"); for(j=i-1; j>0; j--) { System.out.print(octnum[j]); } System.out.print(" "); } }
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started