Question

In: Computer Science

Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide)...

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

  

Solutions

Expert 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 \n");
   System.out.print("Please Type Quit to exit \n ");
       Scanner scan = new Scanner(System.in);
       String number,secnumber;
       System.out.print("Enter an binary number:\n");
       number = scan.nextLine();
       System.out.print("Enter second binary number:\n");
       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 (+, -, *, /) : \n");
   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);
   }
         
      
      
         
  
   }
      
       }


Related Solutions

Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide...
Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide Power Root Modulus Upon receiving the user's selection, prompt the user for two numeric values and print the corresponding solution based on the user's menu selection. Ask the user if they would like to use the calculator again. If yes, display the calculator menu. otherwise exit the program. EXAMPLE PROGRAM EXECUTION: Add Subtract Multiply Divide Power Root Modulus Please enter the number of the...
Using Java Write a simple calculator which can add, subtract, multiply, and divide. Here are the...
Using Java Write a simple calculator which can add, subtract, multiply, and divide. Here are the specifications for the calculator: The calculator has one “accumulator”, which holds the result of all prior calculations. The calculator is set to 0.0 when the calculator is turned on (i.e., instantiated). On the console, the user then enters an operator (+, -, *, or /) and a number. Your console will then apply the operator and operand to the number in the accumulator, and...
PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide...
PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide using functions. It needs to ask for the two numbers from the user and will ask the user for their choice of arithmetic operation 1- subtract 2- add 3- divide 4- multiply
Must make a "Calculator" using C. The calculator must subtract, add, divide, and multiply inputs, and...
Must make a "Calculator" using C. The calculator must subtract, add, divide, and multiply inputs, and tell whether a number is prime or not. The user chooses how many inputs go into the calculator. For example, the code will ask the user what function they want. If the user chooses to subtract, the the code will then ask the user how many numbers they want to subtract. After, the code will ask the user to input as many numbers as...
Calculator Class Instructions Create a calculator class that will add, subtract, multiply, and divide two numbers....
Calculator Class Instructions Create a calculator class that will add, subtract, multiply, and divide two numbers. It will have a method that will accept three arguments consisting of a string and two numbers example ("+", 4, 5) where the string is the operator and the numbers are what will be used in the calculation. The class must check for a correct operator (+,*,-,/), and a number (integer) for the second and third argument entered. The calculator cannot divide by zero...
Write a Python program to add, multiply and divide any two numbers.
Write a Python program to add, multiply and divide any two numbers.
How to add, multiply, and divide numbers in flowgorithm
How to add, multiply, and divide numbers in flowgorithm
Java program - you are not allowed to use arithmetic operations such as division (/), multiplication,...
Java program - you are not allowed to use arithmetic operations such as division (/), multiplication, or modulo (%) to extract the bits. In this exercise use only logic bit-wise operations. Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you CANNOT use the arithmetic division by...
P-3.36 Write a Java program for a matrix class that can add and multiply arbitrary twodimensional...
P-3.36 Write a Java program for a matrix class that can add and multiply arbitrary twodimensional arrays of integers.
In this Java program you will implement your own doubly linked lists. Implement the following operations...
In this Java program you will implement your own doubly linked lists. Implement the following operations that Java7 LinkedLists have. 1. public DList() This creates the empty list 2. public void addFirst(String element) adds the element to the front of the list 3. public void addLast(String element) adds the element to the end of the list 4. public String getFirst() 5. public String getLast() 6. public String removeLast() removes & returns the last element of the list. 7. public String...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT