Question

In: Computer Science

Write a program to convert the input numbers to another number system. 1. Decimal to Binary...

Write a program to convert the input numbers to another number system.

1. Decimal to Binary

2. Binary to Decimal

3. Hexadecimal to Decimal

4. Decimal to Hexadecimal

5. Binary to Hexadecimal

6. Hexadecimal to Binary

The user will type in the input number as following:

Binary number : up to 8 bits

Hexadecimal number: up to 2 bytes

Decimal number: Less than 256

As a result, print out the output after the conversion with their input numbers.

The program must repeat the procedure until the user wants to stop the program

Solutions

Expert Solution

Following is the program written in JAVA to perform the conversions.

  • This program utilizes the in-built functions of java.lang.Integer class for most of the conversions (due to time constraint), but also illustrates the division method for decimal to binary conversion.
  • A similar approach could be used for conversion to hexadecimal, where an array of 16 symbols of hex radix (namely 0-9 and a-f) can be taken and utilized in the division method.

Code:

==========================================================================================

import java.util.Scanner;

public class NumberConversionDemo {
  
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        NumberConverter nc = new NumberConverter();

        Integer option = null;
        String num = null;
        do {
            System.out.println("Welcome to the number converter. Please select a number to execute the option in front of it:\n");
            System.out.println("1. Decimal to Binary\n");
            System.out.println("2. Binary to Decimal\n");
            System.out.println("3. Hexadecimal to Decimal\n");
            System.out.println("4. Decimal to Hexadecimal\n");
            System.out.println("5. Binary to Hexadecimal\n");
            System.out.println("6. Hexadecimal to Binary\n");
            System.out.println("Press 0 to exit\n");

            option = Integer.parseInt(sc.nextLine());

            switch(option) {
                case 0:     sc.close();
                            return;

                default:    System.out.println("Please enter the number to be converted.\n");
                            num = sc.nextLine();
                            System.out.println(nc.convert(num, option));
                            sc.nextLine();

            }

            System.out.println("\n\n\n=============================================================================================");

        }
        while(true);
    }
}

class NumberConverter {

    private Integer number;

    public String convert(String num, Integer option) {
        switch(option) {
            case 1: return convertDecToBin(num);

            case 2: return convertBinToDec(num);

            case 3: return convertHexToDec(num);

            case 4: return convertDecToHex(num);

            case 5: return convertBinToHex(num);

            case 6: return convertHexToBin(num);
        }
        return "Invalid option. Please try again.\n";
    }

    public String convertDecToBin(String num) {
        this.number = Integer.parseInt(num);
        if(this.number > 256) {
            return "Invalid input or number out of range. Please try again.\n";
        }
        String revNum = "";
        Integer rem;
        while (this.number>0) {
            rem = this.number % 2;
            revNum = revNum + "" + rem;
            this.number = this.number/2;
        }
        StringBuilder sb = new StringBuilder(revNum);
        return "Input: " + num + "\nOutput: " + sb.reverse().toString();
    }

    public String convertBinToDec(String num) {
        if(num.matches("(.*)[2-9](.*)") || num.length() > 8) {
            return "Invalid input or number out of range. Please try again.\n";
        }
        return "Input: " + num + "\nOutput: " + Integer.parseInt(num, 2);
    }

    public String convertHexToDec(String num) {
        num = num.toLowerCase();
        if(num.matches("(.*)[g-zG-Z](.*)") || num.length() > 4) {
            return "Invalid input or number out of range. Please try again.\n";
        }
        return "Input: " + num + "\nOutput: " + Integer.parseInt(num, 16);
    }

    public String convertDecToHex(String num) {
        this.number = Integer.parseInt(num);
        if(this.number > 256) {
            return "Invalid input or number out of range. Please try again.\n";
        }
        return "Input: " + num + "\nOutput: " + Integer.toHexString(this.number);
    }

    public String convertBinToHex(String num) {
        if(num.matches("(.*)[2-9](.*)") || num.length() > 8) {
            return "Invalid input or number out of range. Please try again.\n";
        }
        return "Input: " + num + "\nOutput: " + Integer.toHexString(Integer.parseInt(num, 2));
    }

    public String convertHexToBin(String num) {
        num = num.toLowerCase();
        if(num.matches("(.*)[g-zG-Z](.*)") || num.length() > 4) {
            return "Invalid input or number out of range. Please try again.\n";
        }
        return "Input: " + num + "\nOutput: " + Integer.toBinaryString(Integer.parseInt(num, 16));
    }
}


Related Solutions

Write a c++ program to convert any decimal number to either binary base  or Hex base...
Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings: Convert 15 from decimal to binary.  Convert 255 from decimal to binary. Convert BAC4 from hexadecimal to binary Convert DEED from hexadecimal to binary.  Convert 311 from decimal to hexadecimal. Convert your age to hexadecimal.
Convert the decimal number, 315.56 into binary form?
Convert the decimal number, 315.56 into binary form?
Convert the binary numbers to decimal. Show a single sample calculation for the first number (10010010)....
Convert the binary numbers to decimal. Show a single sample calculation for the first number (10010010). 0111 1111 1001 0110 0101 1100 1100 0111
(a) Convert the decimal numbers, 70 and -26 to binary in the signed 2’s complement system....
(a) Convert the decimal numbers, 70 and -26 to binary in the signed 2’s complement system. Make sure there are enough digits in the results to be able to perform arithmetic operations with these two numbers. (b) Perform in the signed 2’s complement system, (+70) + (-26) (c) Perform in the signed 2’s complement system, (-70) - (-26) (d) Perform in the signed 2’s complement system, (+70) + (+26)
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
Convert these numbers from Decimal to Binary 111: 66: 252 11 20 Convert these numbers from...
Convert these numbers from Decimal to Binary 111: 66: 252 11 20 Convert these numbers from Binary to Decimal 00110110 11111000 00000111 10101010 What is the Default Subnet mask of Class A IPV4 What is the Default Subnet mask of Class B IPV4 What is the Default Subnet mask of Class C IPV4 What is the CIDR notation or / short handwriting of Subnet masks: Class A: /?. Explain the reason Class B: /? Explain the reason Class C: /?...
Convert the following binary number to dotted decimal format. 11000000100110010000100101011001
Convert the following binary number to dotted decimal format. 11000000100110010000100101011001
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal....
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal. 1. Request input data type. 2. Request input data. 3. Request output data type. 4. Output the data. Use any algorithm
Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
Convert the following unsigned numbers to the requested form: 01100001 binary to: hex, and also decimal...
Convert the following unsigned numbers to the requested form: 01100001 binary to: hex, and also decimal Hex: Decimal: b) 136 decimal to: hex, and also binary Hex: Binary:
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT