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...
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...
Subtract/Multiply/Divide using Significant figures 1. (16.75 - 6.2) x (9.56 x 10-19) ------------------------------------------ (6.753.2) x (8.5679...
Subtract/Multiply/Divide using Significant figures 1. (16.75 - 6.2) x (9.56 x 10-19) ------------------------------------------ (6.753.2) x (8.5679 x 1011) This is just one problem, just dividing it 2. Convert 9.64 x 1014 pm3 to nanometers
Programming Problem Design a program in java that can perform three different arithmetic operations based on...
Programming Problem Design a program in java that can perform three different arithmetic operations based on the user’s input. The three operations are 1. Summation of integers from 1 to m 2. Factorial of a given number n (n!) 3. Finding the leftmost digit of a given integer (note: you have to use int) This program should prompt the user a menu including four options, ask the user for one option, and perform the corresponding computation. This process will repeat...
In this project, you are required to write the java program “IO.java” to implement integer operations...
In this project, you are required to write the java program “IO.java” to implement integer operations “+”, “−”, “*”. Specifically, your program reads operands from a file named “input.txt” (which will be manually placed under the directory of the program) as strings. Then your program converts strings to integers, and computes the addition, subtraction, and multiplication of the operands. Finally, your program creates a file named “output.txt” (under the directory of the program) and writes the results to the file....
I am writing a program that will work with two other files to add or subtract...
I am writing a program that will work with two other files to add or subtract fractions for as many fractions that user inputs. I need to overload the + and - and << and >> opperators for the assignment. The two files posted cannot be modified. Can someone correct the Fraction.ccp and Frction.h file that I am working on? I'm really close. // // useFraction.cpp // // DO NOT MODIFY THIS FILE // #include "Fraction.h" #include<iostream> using namespace std;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT