Implement a Java program that is capable of performingthe ba

Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide) on binary numbers using only logical operations (i.e., not using the actual mathematical operators thatJava already supports).A skeleton for the implementation is provided and can be downloaded from Canvas.In this source file  (BinaryCalculator.java), there is already code to read stringsfrom the keyboard.  The program will exit if the string “QUIT” is received, otherwiseit will attempt to process commands of the form:

<binary operand 1> <operator> <binary operand 2>

For example:

01111 + 00010011 * 0100111011 / 00001110101 – 0001

The binary numbers consist of strings of “1” and “0” and both operands must havethe same number of bits.The four operators that must be supported are: +, *, / and – (addition,multiplication, divide, and subtract).The output of the program should re-state the mathematical equation, but with thenumbers in decimal, and also show the result. For example, using the commandsabove, the a session might look like:

Welcome to the BinaryCalculator01111 + 00010

15 + 2 = 17
011 * 010
3 * 2 = 6
0111011 / 0000111
59 / 7 = 8R3
0101 – 0001
5 – 1 = 4

QUIT

  

Solution

public class BinaryCalculator {
  
  
  
   public int binaryToDecimal(int binaryNumber){
      
       int decimal = 0;
       int p = 0;
       int length = Integer.toString(binaryNumber).length();
       while(true)
       {
       if( binaryNumber == 0)
               {
       break;
       } else {
       int temp = binaryNumber %10;
       decimal += temp*Math.pow(2, p);
       binaryNumber = binaryNumber / 10;
       p++;
       }
       }
       return decimal;
       }
  
   public void calculate(int firstNumber,int secondNumber,char operator )
   {
       int result =0;
  
       switch(operator)
           {
               case \'+\':
                   result=firstNumber+secondNumber;
                   break;
               case \'-\':
                   result=firstNumber-secondNumber;
                   break;
               case \'*\':
                   result=firstNumber*secondNumber;
                   break;
               case \'/\':
                   if(secondNumber==0)//when denominator becomes zero
                   {
                       System.out.println(\"DIVISION NOT POSSIBLE\");
                       break;
                   }
                   else
                       result=firstNumber/secondNumber;
             
               default:
                   System.out.println(\"YOU HAVE ENTERED A WRONG CHOICE\");
             
      
                  
           }
       System.out.println( \"\" +firstNumber+\"\" +operator+\"\"+secondNumber+\" \"+result);
   }
  
   public static void main(String args[]){
       BinaryCalculator obj = new BinaryCalculator();
       while(true)
       {
   System.out.println(\"Welcome to BInary Calculator \ \");
   System.out.print(\"Please Type Quit to exit \ \");
       Scanner scan = new Scanner(System.in);
       String number,secnumber;
       System.out.print(\"Enter an binary number:\ \");
       number = scan.nextLine();
       System.out.print(\"Enter second binary number:\ \");
       secnumber=scan.nextLine();
      
       if(number.equals(\"Quit\") || secnumber.equals(\"Quit\")){
   System.out.println(\"Thanks for using my programe\");
   System.exit(0);
   }
       System.out.print(\"Enter Operator (+, -, *, /) : \ \");
   char operator = scan.next().charAt(0);
   int firstNumber= obj.binaryToDecimal(Integer.parseInt(number));
       int secondNumber= obj.binaryToDecimal(Integer.parseInt(secnumber));
       obj.calculate(firstNumber, secondNumber, operator);
   }
         
      
      
         
  
   }
      
       }

Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide) on binary numbers using only logical ope
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide) on binary numbers using only logical ope

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site