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
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...
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;...
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a...
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a postfix form, and then computes its value (by using stack-based algorithms). Assume that all the numbers in the arithmetic expression are one-digit numbers, i.e., each of these numbers is either 0, or 1, or 2, ..., or 9. For example, your program should correctly process expressions like 2+3*4, but there is no need to process expressions like 11+22.
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT