//import API's needed here: import java.util.Scanner; public class CalculatorDriver23 { public static void main(String args[ ]) { Calculator one = new Calculator(); Calculator two = new Calculator('+'); one.calculate(); } //closing main method public static class Calculator { private double a; //operand a private double b; //operand b private double ans; //result of calculation private char op; //operator private double mem; //result of previous calculation private boolean newData;//used if = was found to start new problem otherwise use memory for first operand Calculator() //default constructor with no parameters - sets attributes to 0 { this.a=0; this.b=0; this.ans=0; this.op = ' '; this.mem =0; this.newData=true; } Calculator(char x)//constructor that sets operator only { this.a=0; this.b=0; this.ans=0; this.op = x; this.mem =0; this.newData=true; } void calculate() { Scanner kbd = new Scanner(System.in); //to accept keyboard input int i; //index variable to access string array boolean on = true; //run calculator until user enters lowercase o to turn off calculator String []inp = new String[4]; //4 rows of data: operand a, operator, operand b, = inp[0]="x"; inp[2]="x"; //used to run for loop - that user did not yet enter o to turn off do{ if(newData==true)//full new equation a,operator, b, = { for(i=0;i= '0' && inp[0].charAt(0) <= '9'||inp[0].charAt(0)=='.')) //data was taken in as String this.a = Double.parseDouble(inp[0]); //covert it to the double data type to be able to do math manipulation if (newData==true) setOp(inp[1].charAt(0));//set the operator for the new data if(inp[2].charAt(0) >= '0' && inp[2].charAt(0) <= '9'|| inp[2].charAt(0) == '.') //same for the 2nd operand - pay close attention to the index this.b = Double.parseDouble(inp[2]); if(inp[3].charAt(0) == '=') //if = is found do the math { if(this.op == '+') { add(a,b,inp[3].charAt(0)); this.newData=true; } if(this.op == '-') { subtract(a,b,inp[3].charAt(0)); this.newData=true; } }//end of if found = else if (inp[3].charAt(0) == '+') { if(this.op == '+') { add(a,b,inp[3].charAt(0)); setA(); this.newData =false; } if(this.op == '-') { subtract(a,b,inp[3].charAt(0)); setA(); this.newData = false; } }//end of if found + else if (inp[3].charAt(0) == '-')//subtract { if(this.op == '+') { add(a,b,inp[3].charAt(0)); setA(); this.newData =false; } if(this.op == '-') { subtract(a,b,inp[3].charAt(0)); setA(); this.newData = false; } }//end of if found - }//end of else it was not 'o' }while(on); }//end of calculate void add(double a, double b, char end) { this.ans = a+b; if(end == '=')//display result System.out.println(this.ans); this.mem = this.ans; //return this.ans; } void subtract(double a, double b, char end) { this.ans = a-b; if(end == '=')//display result System.out.println(this.ans); this.mem = this.ans; //return this.ans; } void setA() { this.a = this.mem; } void setOp(char z) { this.op = z; } }//end of Calculator object } //closing class header