hi. i got some problems with the dot and the C in th calculator i'm making.
1. when i press the C button it clears the screen but the next number i enter is also ignored even though it appears on the screen.
2. the dot is not working as it should for example if i multiply 0.1 * 0.1 = 0.010000001 or if i add multiple times 0.1 at the 8 time i get 0.70000005 or if substracting i get -0.70000005
i would appreciat some help.
thanks in advance.
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AppletCalculadora extends Applet implements ActionListener {
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, c1, c2, c3, c4, c5, c6, c7,
c8, c9;
Panel p1, p2;
Label l1;
TextArea t1;
boolean start = true;
boolean firstDigit = true;
boolean pressed = false;
float previousValue = 0;
String operator = "=";
public AppletCalculadora() {
setLayout(new BorderLayout());
p1 = new Panel(new GridLayout(6, 3));
p2 = new Panel(new BorderLayout());
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b0 = new Button("0");
c1 = new Button(".");
c2 = new Button("CE");
c3 = new Button("+");
c4 = new Button("一");
c5 = new Button("=");
c6 = new Button("*");
c7 = new Button("/");
c8 = new Button("C");
// l1 = new Label("");
Dimension d = p2.getSize();
t1 = new TextArea("0", d.width, d.height, TextArea.SCROLLBARS_NONE);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(c1);
p1.add(b0);
p1.add(c2);
p1.add(c3);
p1.add(c4);
p1.add(c5);
p1.add(c6);
p1.add(c7);
p1.add(c8);
p2.add(t1, BorderLayout.CENTER);
p2.setBackground(Color.white);
p2.setFont(new Font("Serif", Font.BOLD, 16));
add(p1, BorderLayout.SOUTH);
add(p2, BorderLayout.NORTH);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
c1.addActionListener(this);
c2.addActionListener(this);
c3.addActionListener(this);
c4.addActionListener(this);
c5.addActionListener(this);
c6.addActionListener(this);
c7.addActionListener(this);
c8.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
String input = ae.getActionCommand();
if (input.charAt(0) >= '0' && input.charAt(0) <= '9') {
if (start) {
t1.setText(input);
start = false;
} else {
t1.setText(t1.getText() + input);
}
} else if (input.equals(".")) { // got problem here with the dot
if (!start && !pressed) {
t1.setText(t1.getText() + input);
pressed = true;
} else if (start && !pressed) {
t1.setText("0.");
start = false;
pressed = true;
showStatus("pressed start" + t1.getText() + pressed);
}
} else if (input.equals("CE")) {
t1.setText("0");
start = true;
} else if (input.equals("C")) { // also got problems here or in the switch case "C"
// t1.setText("0");
operator = "C";
calculate("0");
start = true;
}
else {
if (!start) {
calculate(t1.getText());
start = true;
pressed = false;
}
operator = input;
}
}
public void calculate(String input) {
float val = Float.parseFloat(input);
switch (operator) {
case "=":
previousValue = val;
break;
case "+":
previousValue += val;
break;
case "一":
previousValue -= val;
break;
case "*":
previousValue *= val;
break;
case "/":
previousValue /= val;
break;
case "C": // may be the problem is with "C" is here
previousValue = val = 0;
break;
}
t1.setText("" + previousValue);
showStatus("" + operator + pressed + previousValue + start);
}
public void init() {
setBackground(Color.cyan);
setForeground(Color.blue);
setFont(new Font(null, Font.BOLD, 11));
}
}