Math Calculator
Main.java
public class Main {
public static void main(String[] args) {
SimpleCalculator calculator = new SimpleCalculator();
calculator.setFirstNumber(5.5);
calculator.setSecondNumber(4);
System.out.println();
System.out.println("add= " + calculator.getAdditionResult());
System.out.println("subtract= " + calculator.getSubtractionResult());
System.out.println("Power of= " + calculator.getPowerOfResult());
System.out.println();
calculator.setFirstNumber(5.25);
calculator.setSecondNumber(0);
System.out.println("multiply= " + calculator.getMultiplicationResult());
System.out.println("divide= " + calculator.getDivisionResult());
System.out.println("Power of= " + calculator.getPowerOfResult());
}
}
SimpleCalculator.java
public class SimpleCalculator {
private double firstNumber;
private double secondNumber;
public void setFirstNumber(double firstNumber) {
this.firstNumber = firstNumber;
}
public void setSecondNumber(double secondNumber) {
this.secondNumber = secondNumber;
}
public double getFirstNumber() {
return firstNumber;
}
public double getSecondNumber() {
return secondNumber;
}
public double getAdditionResult() {
return getFirstNumber() + getSecondNumber();
}
public double getSubtractionResult() {
return getFirstNumber() - getSecondNumber();
}
public double getMultiplicationResult() {
return getFirstNumber() * getSecondNumber();
}
public double getPowerOfResult() {
return Math.pow(getFirstNumber(), getSecondNumber());
}
public double getDivisionResult() {
// 1. check if we can divide with secondNumber
if (secondNumber == 0) {
return 0;
}
return getFirstNumber() / getSecondNumber();
}
}
Output
C:\Users\netadmin\.jdks\openjdk-23.0.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.4\lib\idea_rt.jar=61798:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.4\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\netadmin\IdeaProjects\SimpleCalculator\out\production\SimpleCalculator Main
add= 9.5
subtract= 1.5
Power of= 915.0625
multiply= 0.0
divide= 0.0
Power of= 1.0
Process finished with exit code 0