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

I need to find out how to make this calculator repeat itself after it finds the

ID: 3713216 • Letter: I

Question

I need to find out how to make this calculator repeat itself after it finds the answer to the problems inserted. Can you give me the code on how to make that happen?

Also, when I insert +,-,/,or* it does not do the function it needs to do. Ex. when I put 4 and 6 as my numbers, and then I choose the operation "+", it gives me the answer as -2. This occurs with the other operations as well. Please help!

Here is the code compiled below:

Welcome to Calculator Calculations!

Enter two numbers below

Please enter the first number:

4

Please enter the second number:

6

What operation would you like to choose for these numbers?

Your options are: +, -, /, *

Please type one of the options here:

+

-2

MY CODE BELOW:

package calculator;

public class MyCalculator {

   public static void main(String args[]) {

       OtherOps op = new OtherOps();

       while(op.op != '+' && op.op != '-'&& op.op != '*' && op.op !='/') {

           System.out.println("Enter a valid operator");

           op.set_op();

       }

           switch (op.op) {

          

           case '*': op.result = op.multiply();

           case '/': op.result = op.divide();

           case '+': op.result = op.add();

           case '-': op.result = op.subtract();

           default: break;

           }

       System.out.println(op.result);

      

   }

  

  

}

package calculator;

import java.util.Scanner;

public class Operators {

   private int num1;

   private int num2;

   char op;

   private final Scanner sca;

   public int result;

   char command;

   public Operators() {

       sca = new Scanner(System.in);

       System.out.println("Welcome to Calculator Calculations!");

       System.out.println("Enter two numbers below");

       System.out.println("Please enter the first number:");

       num1 = sca.nextInt();

       System.out.println("Please enter the second number:");

       num2 = sca.nextInt();

       System.out.println("What operation would you like to choose for these numbers?");

       System.out.println("Your options are: "+ "+, " + "-, " + "/, " + "*");

       System.out.println("Please type one of the options here:");

       op = sca.next().charAt(0);

       if(op == '+' ||op == '-' || op == '/' || op == '*') {

           return;

       }

   }

   public char set_op(){

       op = (char)sca.nextShort();

       return op;

   }

   public int add() {

       return this.num1 + this.num2;

   }

   public int subtract() {

       return this.num1 - this.num2;

   }

   public int getX() {

       return this.num1;

   }

   public int getY() {

       return this.num2;

   }

   public void setX(int x) {

       this.num1 = x;

   }

   public void setY(int y) {

       this.num2 = y;

   }

}

   /*public int Sin(int x) {

       int z;

       z = Sin(x);

       return z;

   }

}

*/

package calculator;

public class OtherOps extends Operators {

  

   public OtherOps() {

       super();

   }

   public int multiply() {

       return this.getX() * this.getY();

   }

   public int divide() {

       return this.getX() * this.getY();

   }

}

Explanation / Answer

//Modifications are in bold letters
//MyCalculator.java
package calculator;
public class MyCalculator
{
   public static void main(String args[])
   {
       OtherOps op =null;
       //add while loop to repeatedly prompt for values
       while(true)
       {
           op = new OtherOps();
           while(op.op != '+' && op.op != '-'&& op.op != '*' && op.op !='/')
           {
               System.out.println("Enter a valid operator");
               op.set_op();
           }

           switch (op.op)
           {

           case '*':
               op.result = op.multiply();
               //add break statement
               break;
           case '/':
               op.result = op.divide();
               //add break statement
               break;
           case '+':
               op.result = op.add();
               //add break statement
               break;
           case '-':
               op.result = op.subtract();
               //add break statement
               break;
           default: break;
           }

           System.out.println(op.result);
       }
   }
}

----------------------------------------------------------------------


//Operators.java
package calculator;
import java.util.Scanner;
public class Operators
{
  
   private int num1;
   private int num2;
   char op;
   private final Scanner sca;
   public int result;
   char command;
  
   public Operators()
   {
       sca = new Scanner(System.in);
       System.out.println("Welcome to Calculator Calculations!");
       System.out.println("Enter two numbers below");
       System.out.println("Please enter the first number:");
       num1 = sca.nextInt();
       System.out.println("Please enter the second number:");
       num2 = sca.nextInt();
       System.out.println("What operation would you like to choose for these numbers?");
       System.out.println("Your options are: "+ "+, " + "-, " + "/, " + "*");
       System.out.println("Please type one of the options here:");
       op = sca.next().charAt(0);
      
       if(op == '+' ||op == '-' || op == '/' || op == '*')
       {
           return;
       }
   }
  
   public char set_op()
   {
       op = (char)sca.nextShort();
       return op;
   }
  
   public int add()
   {
       return this.num1 + this.num2;
   }
  
   public int subtract()
   {
       return this.num1 - this.num2;
   }
   public int getX()
   {
       return this.num1;
   }
  
   public int getY()
   {
       return this.num2;
   }
  
   public void setX(int x)
   {
       this.num1 = x;
   }
  
   public void setY(int y)
   {
       this.num2 = y;
   }
}

----------------------------------------------------------------------


//OtherOps.java
package calculator;
import calculator.Operators;
public class OtherOps extends Operators
{

   public OtherOps() {
       super();
   }
   public int multiply() {
       return this.getX() * this.getY();
   }
   public int divide() {
       return this.getX() / this.getY();
   }

}

----------------------------------------------------------------------

Sample Output:

Welcome to Calculator Calculations!
Enter two numbers below
Please enter the first number:
1
Please enter the second number:
2
What operation would you like to choose for these numbers?
Your options are: +, -, /, *
Please type one of the options here:
+
3
Welcome to Calculator Calculations!
Enter two numbers below
Please enter the first number:
1
Please enter the second number:
2
What operation would you like to choose for these numbers?
Your options are: +, -, /, *
Please type one of the options here:
-
-1
Welcome to Calculator Calculations!
Enter two numbers below
Please enter the first number:
1
Please enter the second number:
2
What operation would you like to choose for these numbers?
Your options are: +, -, /, *
Please type one of the options here:
*
2
Welcome to Calculator Calculations!
Enter two numbers below
Please enter the first number:
12
Please enter the second number:
6
What operation would you like to choose for these numbers?
Your options are: +, -, /, *
Please type one of the options here:
/
2
Welcome to Calculator Calculations!
Enter two numbers below
Please enter the first number: