Question
package calculator; public class MyCalculator { public static void main(String args[]) { OtherOps op = new OtherOps(); do { System.out.println(Enter a valid operator); op.set_op(); }while(op.op
package calculator;
public class MyCalculator {
public static void main(String args[]) {
OtherOps op = new OtherOps();
do {
System.out.println("Enter a valid operator");
op.set_op();
}while(op.op != '+' && op.op != '-'&& op.op != '*' && op.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 Scanner sca;
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 = (char)sca.nextShort();
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() {
int z;
z = this.getY();
if(z==0) {
return 0;
}
return this.getX() / z;
}
}
Every time I run this program, I get this 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:
+
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:860)
at java.base/java.util.Scanner.next(Scanner.java:1497)
at java.base/java.util.Scanner.nextShort(Scanner.java:2021)
at java.base/java.util.Scanner.nextShort(Scanner.java:1975)
at calculator.Operators.
at calculator.OtherOps.
at calculator.MyCalculator.main(MyCalculator.java:5)
PLEASE HELP ME ASAP. I don't know exactly what to do and what is wrong.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started