Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary Calculator Assignment

Tags:

java

For this lab, you will enter two numbers in base ten and translate them to binary. You will then add the numbers in binary and print out the result. All numbers entered will be between 0 and 255, inclusive, and binary output is limited to 8 bits. This means that the sum of the two added numbers will also be limited to 8 bits. If the sum of the two numbers is more than 8 bits, please print the first 8 digits of the sum and the message "Error: overflow". Your program should represent binary numbers using integer arrays, with the ones digit (2^0) stored at index 0, the twos digit (2^1) stored at index 1, all the way up to the 2^7 digit stored at index 7. Your program should include the following methods:

  • int[] convertToBinary(int b) Translates the parameter to a binary value and returns it stored as an array of ints.
  • void printBin(int b[]) Outputs the binary number stored in the array on one line. Please note, there should be exactly one space between each output 0 or 1.
  • int[] addBin(int a[], int b[]) Adds the two binary numbers stored in the arrays, and returns the sum in a new array of ints.

When entering my code into CodeRunner (which tests the code and returns a grade back depending on the results of each test) I cannot seem to pass one of the tests. This is the message that I am getting:

*You had 43 out of 44 tests pass correctly. Your score is 97%.

The tests that failed were: Test: addBin() method Incorrect: Incorrect number returned*

Heres my code:

import java.util.Scanner;

class Main {
    public static int[] convertToBinary(int a) {
        int[] bin = {0, 0, 0, 0, 0, 0, 0, 0};
        for (int i = bin.length - 1; i >= 0; i--) {
            bin[i] = a % 2;
            a = a / 2;
        }
        return bin;
    }

    public static void printBin(int[] b) {
        int z;
        for (z = 0; z < b.length; z++) {
            System.out.print(b[z] + " ");
        }
        System.out.println();
    }

    public static int[] addBin(int[] c, int[] d) {
        int[] added = new int[8];
        int remain = 0;
        for (int x = added.length - 1; x >= 0; x--) {
            added[x] = (c[x] + d[x] + remain) % 2;
            remain = (c[x] + d[x] + remain) / 2;
        }
        if (added[0] + c[0] + d[0] == 1) {
            added[0] = 1;
        } else if ((added[0] + c[0] + d[0] == 2) || (added[0] + c[0] + d[0] == 3)) {

            System.out.println("Error: overflow");
        }
        return added;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a base ten number between 0 and 255, inclusive.");
        int num1 = scan.nextInt();
        System.out.println("Enter a base ten number between 0 and 255, inclusive.");
        int num2 = scan.nextInt();

        int[] bin;
        bin = convertToBinary(num1);
        System.out.println("First binary number:");
        printBin(bin);
        int[] bin1 = bin;

        bin = convertToBinary(num2);
        System.out.println("Second binary number:");
        printBin(bin);
        int[] bin2 = bin;


        System.out.println("Added:");
        {
            printBin(addBin(bin1, bin2));
        }
    }
}

If anyone could take a look at my code above and see if they could tell me what needs to be changed to fix the addbin() method so that it passes all of the tests, that'd be great! Any help is greatly appreciated even if you are not sure it would work! Thanks!

like image 633
NotaMan Avatar asked Mar 10 '26 09:03

NotaMan


1 Answers

Hi first of all pardon my English but i guess your assignment accepts 1 and 255 too. So i added two of them and get 1 0 0 0 0 0 0 0 in your code. But i think it need to be 0 0 0 0 0 0 0 0 with an overflow error. So i changed your code a little bit.

public static int[] addBin(int[] c, int[] d) {
    int[] added = new int[8];
    int remain = 0;
    for (int x = added.length - 1; x >= 0; x--) {
        added[x] = (c[x] + d[x] + remain) % 2;
        remain = (c[x] + d[x] + remain) / 2;
    }
    if (remain!=0) {

        System.out.println("Error: overflow");
    }
    return added;
}

It's my first answer on the site so i hope it works for your test

like image 141
Çağdaş Tunca Avatar answered Mar 11 '26 21:03

Çağdaş Tunca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!