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

How do I make my calculator program ask the user after each calculation to eithe

ID: 3713922 • Letter: H

Question

How do I make my calculator program ask the user after each calculation to either end the program or quit or to continue to use the calculator before repeating the "Welcome to Calculator Calculations" thing? Its not working for me. What code do I need to add? Also, how do I add the functions for sin cos and tan in this calculator? I need the code to see how it would work for trigonometric functions both degrees and radians if possible. Also, I want my code to reject any input other than a number and repeat itself to ask the user to enter numbers again. Also, I want the calculator to be able to calculate values like 5/9 would be 0.55, I think I would need to add a double but i dont know where to add.Thank you. Please get this problem done ASAP!

This is the output:

Welcome to Calculator Calculations!

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

Enter two numbers below:

Please enter the first number:

23

Please enter the second number:

39

What operation would you like to choose for these numbers?

Your options are: +, -, /, *

Please type one of the options here:

Type Q to quit

+

62

Would you like to calculate more or quit?

Welcome to Calculator Calculations!

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

Enter two numbers below:

Please enter the first number:

THIS IS MY CODE BELOW:

package calculator;

public abstract class MyCalculator {

   public static void main(String args[]) {

       OtherOps op = null;

       while(true) {

           op = new OtherOps();

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

               op.set_op();

           }

           switch (op.op) {

          

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

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

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

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

           case 'Q': System.out.println("Exiting program"); break;

           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("--------------------------------------");

       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:");

       System.out.println("Type Q to quit");

       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 (int) (this.getX() * this.getY());

   }

   public int divide() {

       return (int) (this.getX() / this.getY());

   }

}

Explanation / Answer


package splitcon;
import java.util.Scanner;
public class MyCalculator {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
for(;;){
double output = 0;
System.out.println("Welcome to Calculator Calculations!");
System.out.println("Enter two numbers below:");
System.out.println("Please enter the first number OR Degress:");
double firstNumber=sc.nextInt();
System.out.println("Please enter the second number OR Radians: ");
double secondNumber=sc.nextInt();
System.out.println("What operation would you like to choose for these numbers?");
System.out.println("Your options are: +, -, /, *,SIN, COS, TAN");
System.out.println("Type Q to quit");
System.out.println("Please type one of the options here:");
String operand= sc.next();
switch (operand) {
case "+": output = sum(firstNumber, secondNumber);
break;
case "-": output = subtract(firstNumber, secondNumber);
break;
case "/": output = division(firstNumber, secondNumber);
break;
case "*": output = mulplication(firstNumber, secondNumber);
break;
case "SIN": output = sin(firstNumber, secondNumber);
break;
case "COS": output = cos(firstNumber, secondNumber);
break;
case "TAN": output = tan(firstNumber, secondNumber);
break;
case "Q":
break;
default: operand = "Invalid operand ";
break;
}
System.out.println(output);
}
}

public static double sum(double a, double b){
return a+b;
   }
public static double subtract(double a, double b){
return a-b;
   }
public static double division(double a, double b){
return a/b;
   }
public static double mulplication(double a, double b){
return a*b;
   }
public static double sin(double a, double b){
int k=-1,f=1,i,ch,t=0;
double p,s=0,x=a,rad,r=0,n=b;
rad=3.14/180*b;
s=rad;
for(i=3;i<n;i+=2)
{
p=Math.pow(rad,i);
f=f*(i-1)*i;
r=p/f;
s=s+k*r;
k=k*(-1);
}
return s;
   }
public static double cos(double a, double b){
double n=b,k=-1,f=1,i,ch,t=0;
double p,s=0,x=a,rad,r=0;
rad=3.14/180*b;
s=1.0;
for(i=2;i<n;i+=2)
{
   p=Math.pow(rad,i);
f=f*(i-1)*i;
r=p/f;
s=s+k*r;
k=k*(-1);
}
return s;
   }
public static double tan(double a, double b){
return sin(a, b)/cos(a, b);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote