I used Float.parseFloat and all int variable with float then backspace won't work properly
// packets that are required
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
//NewCalculator class
public class NewCalculator extends Applet implements ActionListener{
String cmd[]={"+","-","*","/","=","C","%","<-","1/x","."};
String cmd1[] = {"MR","MS","EXIT"};
int pv=0,r=0;
String op="";
Button b[]=new Button[23];
TextField t1=new TextField(10);
// applet initialization
public void init(){
setLayout(new BorderLayout());
add(t1,"North");
t1.setText("0");
Panel p=new Panel();
p.setLayout(new GridLayout(4,4));
for(int i=0;i<20;i++){
if(i<10)
b[i]=new Button(String.valueOf(i));
else
b[i]=new Button(cmd[i%10]);
b[i].setFont(new Font("Arial",Font.BOLD,25));
p.add(b[i]);
add(p,"Center");
b[i].addActionListener(this);
}
b[20] = new Button("MR");
b[20].setFont(new Font("Arial",Font.BOLD,25));
p.add(b[20]);
b[20].addActionListener(this);
b[21] = new Button("MS");
b[21].setFont(new Font("Arial",Font.BOLD,25));
p.add(b[21]);
b[21].addActionListener(this);
b[22] = new Button("EXIT");
b[22].setFont(new Font("Arial",Font.BOLD,25));
p.add(b[22]);
b[22].addActionListener(this);
}
// actionperformer for the button and textfield
public void actionPerformed(ActionEvent ae){
int res=0;
if(op == "EXIT"){ // exit is not working
System.out.println("1");
System.exit(0);
System.out.println("2");
}
else{
String cap=ae.getActionCommand();
int cv=Integer.parseInt(t1.getText());
if(cap.equals("C")){
t1.setText("0");
pv=0;
cv=0;
res=0;
op="";
}
else if(cap.equals("=")){
res=0;
if(op=="+")
res=pv+cv;
else if(op=="-")
res=pv-cv;
else if(op=="*")
res=pv*cv;
else if(op=="/")
res=pv/cv;
else if(op=="%")
res=pv%cv;
t1.setText(String.valueOf(res));
}
else if(cap.equals("+")||cap.equals("-")||cap.equals("*")||cap.equals("/")||cap.equals("%")||cap.equals("<-")||cap.equals("1/x")||cap.equals(".")||cap.equals("MS")||cap.equals("MR")){
pv=cv;
op=cap;
t1.setText("0");
if(op=="<-"){ // doesn't works with float
if(pv !=0){
pv = pv /10;
t1.setText("");
}
else{
cv = cv /10;
t1.setText(""+cv);
}
}
if(op == "1/x"){ // not able to do with floatnumber
pv =1/pv;
t1.setText(""+pv);
}
if(op == "MS"){
r = cv ;
}
if(op == "MR"){
t1.setText(""+r);
}
}
else{
int v=cv*10+Integer.parseInt(cap);
t1.setText(String.valueOf(v));
}
}
}
}