I am trying to come up with a simple basic calculator but am supposed to work with the following constructors and methods:
simpleCalc();
simpleCalc(double op1, double op2);
double add();
double add(double op1, double op2);
double mult();
double mult(double op1, double op2);
double exp();
double exp(double op1, double op2);
void show();
void show(int dp);
I have written down some working code and I just want to know if am doing the right thing.
MyClass.java
package basicjavacalculator;
public class MyClass {
public static void main(String[] args) {
// TODO code application logic here
simpleCalc sc = new simpleCalc();
sc.show();
}
}
simpleCalc.java
package basicjavacalculator;
import java.util.Scanner;
public class simpleCalc {
double add(){
return 0;
}
double mult(){
return 0;
}
double exp(){
return 0;
}
simpleCalc(){
}
void show(){
Scanner userInput = new Scanner(System.in);
System.out.println("=================Welcome to SimpleCalcDemo=================");
System.out.println("\n");
System.out.println("Enter first number: ");
double op1 = userInput.nextInt();
System.out.println("Enter second number: ");
double op2 = userInput.nextInt();
System.out.print("Enter operation to perform (+,x,^):");
String operation= userInput.next();
if(operation.equals("+")){
System.out.println(add(op1, op2));
}
else if (operation.equals("x")){
System.out.println(mult(op1, op2));
}
else if (operation.equals("^")){
System.out.println(exp(op1, op2));
}
else{
System.out.println("The operation is not valid.");
}
}
double add(double op1, double op2){
return op1 + op2;
}
double mult(double op1, double op2){
return op1 * op2;
}
double exp(double op1, double op2){
double result = 1;
while (op2 > 0){
result = result * op1;
op2--;
}
return result;
}
}