Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question

Need help in writing a program which is a java calculator. Use only procedural constructs of the Java programming language.

You may use all the following: - the Main class since, - Scanner class with all its methods for input and output, - Array and String classes with all their methods

Must do conversions:

  1. Binary to hexadecimal and vice versa
  2. Arithmetic to binary and vice versa
  3. Arithmetic to hexadecimal and vice versa

Please use IntelliJ idea or eclipse.

I have the code already done for:

  1. Binary to decimal
  2. Decimal to binary
  3. Hexadecimal to decimal
  4. Decimal to hexadecimal

Please use this and add it to your code to print out the calculator values:

public static void main(String[] args) {

        //String binary = toBinaryString(number);
        Scanner input = new Scanner(System.in);

        System.out.println("Number Conversion in java \n");

        // Display the menu
        System.out.println("1.\t Decimal to Binary");
        System.out.println("2.\t Decimal to Hexadecimal");
        System.out.println("3.\t Binary to Decimal");
        System.out.println("4.\t Hexadecimal to Decimal \n");
        System.out.println("Your choice?");

        //Get user's choice
        int choice = -1;
        while (choice < 0 || choice > 4) {
            choice = input.nextInt();

            switch (choice) {
            case1:
                System.out.println("\nEnter Decimal Number");
            break;
            case2:
                System.out.println("\nEnter Decimal Number");
            break;
            case3:
                System.out.println("\nEnter Binary");
            break;
            case4:
                System.out.println("\nEnter Hexadecimal");
            break;
            default:
                System.out.println("\nInvalid choice. Please choose a number between 1 and 4.");
                break;
            }
        }
        if (choice == 1) {
            int number = input.nextInt();
            String binary = toBinaryString(number);
            binary = recursive(number);
            System.out.printf("Decimal to Binary (%d) = %s", number, binary);
        }
        else if (choice == 2) {
            int number2 = input.nextInt();
            String hexadecimal = toHexString(number2);
            hexadecimal = recursiveDecHex(number2);
            System.out.printf("Decimal to Hexadecimal (%d) = %s ", number2, hexadecimal);
        }
        else if (choice == 3 ) {
            String binary2 = input.next();
            int decimal = toDecimalUsingParseInt(binary2);
            decimal = recursiveBin(binary2);
            System.out.printf("\n2. Binary to decimal - recursive(%s) = %d ", binary2, decimal);
            
        } else {
            String hex = input.next();
            int decimal = toHexUsingParseInt(hex);
            decimal = recursiveHexDec(hex);
            System.out.printf("Hexadecimal to Decimal (%s) = %d ", hex, decimal);
        }

        input.close();
    }
    private static String toBinaryString(int number) {
        return Integer.toBinaryString(number);
    }
    private static String toHexString(int number) {
        return Integer.toHexString(number);
    }
    private static int toDecimalUsingParseInt(String binaryNumber) {
        return Integer.parseInt(binaryNumber, 2);
    }
    private static int toHexUsingParseInt(String number) {
        return Integer.parseInt(number, 16);
    }
    private static String recursive(int number) {
        StringBuilder builder = new StringBuilder();

        if (number > 0) {
            String binaryNumber = recursive(number / 2);
            int digit = number % 2;
            builder.append(binaryNumber + digit);
        }
        return builder.toString();
    }
    private static String recursiveDecHex(int number) {
        StringBuilder builder = new StringBuilder();
        if (number > 0) {
            String hexNumber = recursiveDecHex(number / 16);
            String hexCode = "0123456789ABCDEF";
            int hexDigit = number % 16;
            builder.append(hexNumber + hexCode.charAt(hexDigit));
        }
        return builder.toString();
    }
    private static int recursiveBin(String binaryNumber) {
        int decimal = 0;
        int length = binaryNumber.length();
        if (length > 0) {
            String substring = binaryNumber.substring(1);
            int digit = Character.getNumericValue(binaryNumber.charAt(0));
            decimal = digit * (int) Math.pow(2, length - 1) + recursiveBin(substring);
        }
        return decimal;

    }
    private static int recursiveHexDec(String hexNumber) {
        int decimal = 0;
        String hexCode = "0123456789ABCDEF";
        hexNumber = hexNumber.toUpperCase();
        int length = hexNumber.length();
        if (length > 0) {
            char ch = hexNumber.charAt(0);
            int digit = hexCode.indexOf(ch);
            String substring = hexNumber.substring(1);
            decimal = digit * (int) Math.pow(16, length - 1) + recursiveHexDec(substring);
        }
        return decimal;
    }
}

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Similar questions
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY