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

Develop a python program to convert two decimal numbers (A and B) to binary numbers. You...
Develop a python program to convert two decimal numbers (A and B) to binary numbers. You should generate B complement signal (flip all the bits of Input B,
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number...
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number to fractional binary representation. 1. 103.5625
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 following binary values to hexadecimal and decimal (1 pt each) Write Hex Numbers as...
Convert the following binary values to hexadecimal and decimal (1 pt each) Write Hex Numbers as 0x##(ex 0x0A, 0xFF) Binary Hexadecimal Decimal 0001-1011 0x 0000-1000 0000-0100 0000-1001 0001-1111 1001-1001 0111-1010 1100-0010 1110-0101 1000-1010 0011-0100 0001-1001 0100-0011 1111-1111 1110-0111 0001-0010 0100-1000 0100-1110 1001-0001 0110-1100 Name: Convert the following hexadecimal values to binary and decimal Write binary numbers as 0000-0000 Hexadecimal Binary Decimal 0xf1 0xac 0x56 0x6c 0x32 0x30 0x05 0x28 0xf0 0x07 0x42 0xb9 0x6d 0x2f 0x71 0x0e 0x2d 0xfb 0xba...
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: /?...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are not allowed to use library functions for conversion. The output should look exactly as follows: DECIMAL      BINARY                     HEXDECIMAL                      BCD 0                      0000 0000                   00                                            0000 0000 0000 1                      0000 0001                   01                                            0000 0000 0001 2                      0000 0010                   02                                            0000 0000 0010 .                       .                                   .                                               . .                       .                                   .                                               . 255                  1111 1111                   FF                                            0010 0101 0101
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT