Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP!!!! Assignment : Create a program which can maintain binary numbers, being

ID: 668958 • Letter: H

Question

HELP!!!!

Assignment: Create a program which can maintain binary numbers, being able to add them, as well as being able to convert them from binary to decimal, and decimal to binary. There are a number of ways to do this, from creating your own binary class to using an array of chars to being tricky with normal integers. To keep the assignment from getting too complicated, you’ll never need more than SIX binary digits, even in your addition results, so keep that in mind. Your code will need to perform the following feats:

Convert 34 to Binary

Convert 22 to Binary

Convert 1001 to Decimal

Convert 111011 to Decimal

Add 11 and 1110 in Binary, Return the Answer in Binary

Add 1010 and 111 in Binary, then Convert the Answer to Decimal

A general reminder, double-check your code’s output to make sure it’s correctly converting. And although many compilers offer built-in Binary-to-Decimal/Decimal-to-Binary converters, PLEASE DO NOT USE THESE BUILT IN CONVERTERS, as I will intentionally remove points for doing so. (40pts code, 10 pts per problem)

Explanation / Answer

/*Binary to Decimal Convertion*/
import java.util.Scanner;      
class binaryToDecimalConvertion
{
   public static void main(String args[])
   {
       int binaryNumber, decimalNumber=0,i=1;
       Scanner scanner=new Scanner(System.in);
       System.out.println("Please enter the binary number :- ");
       binaryNumber=scanner.nextInt();
       while(binaryNumber!=0)
       {
           decimalNumber=decimalNumber+ (binaryNumber%10)*i;   
           i*=2;
           binaryNumber/=10;   
       }
       System.out.println("Decimal Equivalent of the Binary Number "+ binaryNumber + " is " +decimalNumber);
   }
}

Steps:-
1) If the number is 101, "1*2 power 2 + 1*2 power 1 + 1*2 power 0" convertion will be applied to get the decimal value
2) We accept an input from the end user
3) We extract the last digit and apply the same formula mentioned in step 1 in a loop until we run of digits
  
  

/*Decimal to Binary Convertion*/
import java.util.Scanner;      
import java.lang.StringBuffer;
class decimalToBinaryConvertion
{
   public static void main(String args[])
   {
       int decimalNumber ;
       StringBuffer stringBuf=new StringBuffer();
       Scanner scanner=new Scanner(System.in);
       System.out.println("Please enter the Decimal number :- ");
       decimalNumber=scanner.nextInt();
       while(decimalNumber>=1)
       {
           if(decimalNumber%2!=0)
           {
               stringBuf.append(1);
           }
           else if(decimalNumber%2==0)
           {
               stringBuf.append(0);
           }
           decimalNumber=decimalNumber/2;
       }
       System.out.println("The Binary Equivalent of the Decimal Number " + decimalNumber +" is " +stringBuf.reverse());
   }
}

Steps:-
1) Approach followed here is to take the remainder of the number by dividing it by 2 and appending it and appending it to a temporary variable
2) In the Final step, we reverse the string and print the same

/*Adding Sum of 2 binary Numbers and printing the same in Binary Format*/
/*Adding Sum of 2 binary Numbers and printing the same in Decimal Format*/
import java.util.*;
import java.io.*;
import java.lang.*;
class binaryAdditionOfNumbers{
   public static void main(String args[]){
   int binary1, binary2, sum;
   Scanner scanner=new Scanner(System.in);
   System.out.print("Enter first number :- ");
   binary1=scanner.nextInt();
   System.out.print("Enter second number:- ");
   binary2=scanner.nextInt();
   binarySum=getSumInBinaryFormat(binary1, binary1);
   //decimalSum = Integer.parseInt(binary1, 2) + Integer.parseInt(binary2, 2) ; //Using built in methods
   //decimalSum = binaryToDecimal(binary1) + binaryToDecimal(binary2); // Using custom method
   System.out.println("Sum of 2 binary Numbers in Binary Format is "+binarySum);
   System.out.println("Sum of 2 binary Numbers in Decimal Format is "+decimalSum);
}

public static String binaryToDecimal(int binary1){
       int decimalNumber=0,i=1;
       while(binary1!=0)
       {
           decimalNumber=decimalNumber+ (binary1%10)*i;   
           i*=2;
           binary1/=10;   
       }
       return decimalNumber;
}

public static String getSumInBinaryFormat(int binary1, int binary2){
   String sum=new String();
   int carryfwd=0, i;
   if(binary1.length()<binary2.length()){
       while(binary1.length()<binary2.length())
       binary1=0+binary1;
   }
   else if(binary2.length()<binary1.length()){
       while(binary2.length()<binary1.length())
       binary2=0+binary2;
   }
   for(i=binary1.length()-1;i>=0;i--){
           if(binary1.charAt(i)=='0' && binary2.charAt(i)=='0'){
           sum=carryfwd+sum;
           carryfwd=0;
       }
       else if(binary1.charAt(i)=='1' && binary2.charAt(i)=='1'){
           if(carryfwd==0)
               sum='0'+sum;
           else{
               sum='1'+sum;
               carryfwd=1;
           }
       }
       else if(binary1.charAt(i)=='1' || binary2.charAt(i)=='1'){
           if(carryfwd==0)
               sum='1'+sum;
           else{
               sum='0'+sum;
               carryfwd=1;
           }
       }
   }
   if(carryfwd==1)
       sum='1'+sum;
   return sum;
   }
}

Steps:-
1) Approach followed here is the rule of "1+0=1, 0+1=1, 0+0=0, 1+1=1 with carryfwd 1"
2) If there is a mismatch in the length of the binary numbers, then we append leading 0's
3) Finally we loop over one binary number and take one character at a time in both the binary numbers to get the summation
4) Also note that in order to get the Decimal summation, we convert the binary numbers into their decimal equivalent using Integer.parseInt() and then sum both of these values