-
Java Calculator
Im am doing a java Calculator and I got it working for the first part except when I use it and the first Operator I use doesn't work and the last Operator I use doesn't work, and I'm at a lost on how to code the program to work. To see what I'm talking run the CalculatorEngineCMD
Heres the CalculatorEngine Code
Code:
public class CalculatorEngine {
private double result;
private char operator;
private String buffer;
/**
* implment an empty constructor
*/
public CalculatorEngine() {
result=0;
operator='=';
buffer="";
}
/**
* press a character c. The valid value of c is
* numbers 0, 1, 2, ..., 9
* operators +, -, *, /
* the equality sign =
* R to reset an engine
*/
public void press(char c) {
if(c >= '0' && c<= '9') {
pressNumber(c);
} else if (c == '=') {
calculate();
} else if (c == 'R') {
reset();
} else if (c == 'Q'){
Quit();
} else{
pressOperator(c);
}
}
private void reset() {
result=0;
operator='=';
buffer="";
}
private void pressOperator(char c) {
operator=c;
if (operator=='+'){
result = result + Double.parseDouble(buffer);
buffer="";
}
else if (operator=='-'){
double a=Double.parseDouble(buffer);
result= a - result;
buffer="";
}
else if (operator=='*'){
double a=Double.parseDouble(buffer);
result= a * result;
buffer="";
}
else if (operator=='/'){
double a=Double.parseDouble(buffer);
result= a / result;
buffer="";
}
}
private void pressNumber(char c) {
buffer = buffer + c;
}
private void calculate() {
buffer="";
}
/**
* return the current result of a calculator
*/
public double getResult(){
return result;
}
/**
* return the current buffer number
* for example if users PRESS '1' and PRESS '4', the current buffer is 14
*/
public double getDisplayBuffer() {
double val = 0;
if(buffer.length() > 0) {
val = Double.parseDouble(buffer);
}
else{
val=0;
}
return val;
}
public String Quit(){
String s="Goodbye!";
return s;
}
}
Heres the CalculatorEngineCMD program to run the Calculator
Code:
public class CalculatorCMD {
public static void main(String[] args) {
CalculatorEngine engine = new CalculatorEngine();
engine.press('2');
System.out.println("Buffer:"+engine.getDisplayBuffer());
engine.press('+');
System.out.println("Result:"+engine.getResult());
engine.press('5');
System.out.println("Buffer:"+engine.getDisplayBuffer());
engine.press('*');
System.out.println("Result:"+engine.getResult());
engine.press('2');
System.out.println("Buffer:"+engine.getDisplayBuffer());
engine.press('=');
System.out.println("Result:"+engine.getResult());
}
}
-
Re: Java Calculator
What do you mean by "doesn't work"? There are many ways for code to NOT WORK.
Please explain what happens.
Try debugging the code by adding some println statements to print out the values of variables as they are changed and used and to show execution flow. The print out will show you what the computer sees and does so you can change the code to have it do what you want it to do.
In the pressOperator method there should be and ending else statement after the if/else if statements that prints out a message when none of the above were true.
-
Re: Java Calculator
I'm sorry try running the CalculatorCMD program again. If you run it the calculator should run 2 + 5 * 2 and the output should be result = 14, but the output is result = 10. It totally ignores the 2 + that's in front on the 5.
-
Re: Java Calculator
Have you Tried debugging the code by adding some println statements to print out the values of variables as they are changed and used and to show execution flow. The print out will show you what the computer sees and does so you can change the code to have it do what you want it to do.