0

I have written a code for a calculator but it is not working 100% reliably,yet. Every time I do a calculation, for example: "1+1=2", and I want to do another calculation I have to close the applet and start it again. How can I make it go back at the start.

Here is the code:

package beispiele;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Kalkulator extends Applet {
    String arg1= "", arg2="";
    int ergebnis;
    Button zahl[] =new Button[10];
    Button funktion[] = new Button[4];
    Button ausfuehren;
    Panel zahlPanel,funktionPanel,ergebnisPanel;
    TextField ergebnisFeld;
    int operationArgument;
    char operation;
    public void init () {
    operationArgument= 1; operation =' ';
    setLayout(new BorderLayout());
    zahlPanel = new Panel();
    zahlPanel.setLayout(new GridLayout (4,3));
    for (int i=9; i>=0; i--) {
        zahl[i] = new Button(String.valueOf(i));
        zahl[i].addActionListener(new ButtonZahlen());
        zahlPanel.add(zahl[i]);
    }
    zahlPanel.add(new Button("."));   //leere Taste
    ausfuehren = new Button("=");
    ausfuehren.addActionListener(new ButtonAusfuehren()); //zu dem Listener
    zahlPanel.add(ausfuehren);
    add("Center",zahlPanel);
    funktionPanel = new Panel();
    funktionPanel.setLayout(new GridLayout(4,1));
    funktion[0] = new Button("+");
    funktion[0].addActionListener(new ButtonOperation());
    funktionPanel.add(funktion[0]);
    funktion[1] = new Button("-");
    funktion[1].addActionListener(new ButtonOperation());
    funktionPanel.add(funktion[1]);
    funktion[2] = new Button("*");
    funktion[2].addActionListener (new ButtonOperation());
    funktionPanel.add(funktion[2]);
    funktion[3] = new Button("/");
    funktion[3].addActionListener (new ButtonOperation());
    funktionPanel.add(funktion[3]);

    //funktionPanel.add(new Button("*"));
    //funktionPanel.add(new Button("/"));
    add("East",funktionPanel);
    ergebnisPanel = new Panel();
    ergebnisFeld = new TextField("0",5);
    ergebnisPanel.add(ergebnisFeld);
    add("North",ergebnisPanel);
}
class ButtonZahlen implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        switch (operationArgument)   {
        case 1 :  {
            arg1+=e.getActionCommand();
            ergebnisFeld.setText(arg1);
            break;
        }
        case 2 :   {
            arg2 +=e.getActionCommand();
            ergebnisFeld.setText(arg2);
            break;
        }
        default: { }

        }
        }
    }
class ButtonAusfuehren implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        if(operation =='+')
            if (arg1.equals("")) {
                ergebnis += new Integer(arg2);
            } else {
                ergebnis = new Integer(arg1) + new Integer(arg2);
            }

        else if(operation =='-') {
            if(arg1.equals("")) {
                ergebnis -= new Integer(arg2);
            } else {
                ergebnis = new Integer(arg1) - new Integer(arg2);
            }               
        }
        else if(operation =='*') {
            if(arg1.equals("")) {
                ergebnis *= new Integer(arg2);
            } else {
                ergebnis = new Integer(arg1) * new Integer(arg2);
            }
        }
        else if(operation =='/') {
            if(arg1.equals("")) {
                ergebnis *= new Integer(arg2);
            } else {
                ergebnis = new Integer(arg1) / new Integer(arg2);
            }
        }
    }
}
class ButtonOperation implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("+")) {
            operation = '+'; operationArgument = 2;
            }
        else if(e.getActionCommand().equals("-")) {
                operation = '-'; operationArgument = 2;
                }
        else if(e.getActionCommand().equals("*")) {
                    operation = '*' ; operationArgument =2;
                    }
        else if(e.getActionCommand().equals("/")) {
                        operation = '/' ; operationArgument =2;
                    }

            }
        }
public void paint(Graphics g){   }


}
Tony Andreev
  • 421
  • 1
  • 6
  • 9
  • 1
    actually, there is usually a C button on calculators, that clears all the previous calculations and restarts it all – petajamaja Jan 10 '14 at 22:31
  • 2
    1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Jan 10 '14 at 23:11
  • I have seen such button,but I have no idea how to do it.That was the reason to ask for help. I am using applet and awt,because that is what the teacher wants.Which is a problem,because it is hard to find detailed information about it. – Tony Andreev Jan 11 '14 at 00:02

1 Answers1

1

You are having problems here because you are not careful with your if and else statements and the logic of the program. Firstly, in the actionPerformed method of ButtonOperation, you have an if(e.getActionCommand().equals("-")) inside the if(e.getActionCommand().equals("+")). That doesn't make sense, does it? The commands you are testing for with those ifs are mutually exclusive, so an else if for the - operation is what you want. Then in the ButtonAusfuehren actionPerformed() method: again, think about what you want to be doing. You don't need the last else - you can always set the result field (ergebnisFeld) to the result. Also, the operations themselves wouldn't have worked as the second time an operation was done, one of the arguments (arg1 or arg2) would have been an empty string. If you take this into account, you get something like this, which, with the other changes, makes your calculator work:

        if (operation == '+')
            if (arg1.equals("")) {
                ergebnis += new Integer(arg2);
            } else {
                ergebnis = new Integer(arg1) + new Integer(arg2);
            }

        else if (operation == '-') {
            if (arg1.equals("")) {
                ergebnis -= new Integer(arg2);
            } else {
                ergebnis = new Integer(arg1) - new Integer(arg2);
            }
        }
Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
  • Martin,thank you for the really quick and detailed answer.It really helped clearing some missunderstandings,but still the "=" button doesn't work.I edited my code here with the new changes in ButtonAusfuehren and ButtonOperation – Tony Andreev Jan 11 '14 at 00:57