Reputation: 15
I understand this is a simple question for most people here who have been writing Java a long time. I have created a very simple app that combines my methods and will return a sum along with whether the result is odd or even. The math will perform four operations add, subtract, multiply and divide. Add and divide work as expected but subtract and multiply always return a result of 0.0 regardless of the numbers inputted. I have created three classes, MathApp, OddEven and Calculator. Below are the MathApp and Calculator.
import javax.swing.JOptionPane;
public class MathsApp {
public static void main(String[] args){
//declare variables
double num1;
double num2;
String operation;
double sum;
boolean evenCheck;
//objects
Calculator calc;
calc=new Calculator();
OddEven oe;
oe=new OddEven();
//input
num1=Double.parseDouble(JOptionPane.showInputDialog(null,"Enter number 1"));
num2=Double.parseDouble(JOptionPane.showInputDialog(null,"Enter number 2"));
operation=JOptionPane.showInputDialog(null,"Enter a,s,m or d to add, subtract, multiply or divide");
//set, compute, get
sum=calc.doMaths(num1,num2,operation);
//out
JOptionPane.showMessageDialog(null,"Your answer "+sum);
//OddEven
evenCheck=oe.checkIfEven(sum);
JOptionPane.showMessageDialog(null,"Your number is even "+evenCheck);
}
}
public class Calculator {
//variables
private double num1;
private double num2;
//private String operation;
private double sum;
//combined method
public double doMaths(double sum1, double sum2, String operation){
num1=sum1;
num2=sum2;
if(operation.equalsIgnoreCase(("a"))){
sum=num1+num2;
}
else if(operation.equalsIgnoreCase("b")){
sum=num1-num2;
}
else if(operation.equalsIgnoreCase("c")){
sum=num1*num2;
}
else if(operation.equalsIgnoreCase("d")){
sum=num1/num2;
}
return sum;
}
}
Not sure why this is happening. I have searched on Stack to similar questions before posting but have not seen anything as to help me. Thank you in advance.
Upvotes: 0
Views: 538
Reputation: 6256
"... Add and divide work as expected but subtract and multiply always return a result of 0.0 regardless of the numbers inputted ..."
As @Pshemo mentioned, this is because your if-conditional statements, are evaluating the letters, a, b, c, and d.
As opposed to, a, s, m, and d.
if(operation.equalsIgnoreCase(("a"))){
sum=num1+num2;
}
else if(operation.equalsIgnoreCase("b")){
sum=num1-num2;
}
else if(operation.equalsIgnoreCase("c")){
sum=num1*num2;
}
else if(operation.equalsIgnoreCase("d")){
sum=num1/num2;
}
Thus, only a, and d, will work.
The reason it is returning 0 for s, and m, and because an unassigned double value is 0.
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics).
private double sum;
It is worth noting, you can simplify these conditional checks, with a switch statement.
public double doMaths(double sum1, double sum2, String operation){
num1=sum1;
num2=sum2;
return sum = switch (operation.toLowerCase()) {
case "a" -> num1+num2;
case "s" -> num1-num2;
case "m" -> num1*num2;
case "d" -> num1/num2;
};
}
Upvotes: 0