Hello developers,

Im still practising my programming with coding a calculator after some reading on the basics of Java. Could really use a pointer or two here.

I wrote a calculate function(calculateResult). The error message I got says that the function cannot be applied on the 'number2' argument. (on line 199). I know now I cannot use calculateResult and accept number2 argument in the same subroutine. So what do I do? Any idea?

package assign1;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.awt.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
 
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Frame1 extends JFrame {
    JPanel contentPane;
    XYLayout xYLayout1 = new XYLayout();
    TextField textField1 = new TextField();
    Button button1 = new Button();
    Button button2 = new Button();
    Button button3 = new Button();
    Button button4 = new Button();
    Button button5 = new Button();
    Button button6 = new Button();
    Button button7 = new Button();
    Button button8 = new Button();
    Button button9 = new Button();
    Button button10 = new Button();
    Button button11 = new Button();
    Button button12 = new Button();
    Button button13 = new Button();
    Button button14 = new Button();
    Button button15 = new Button();
    Button button16 = new Button();
    String operator = new String();
  
  
 
    //selfmade attributes
    double number1, number2, result;
 
 
    public Frame1() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
 
    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(xYLayout1);
        setSize(new Dimension(400, 300));
        setTitle("Calculator");
        textField1.setText("");
        button1.setLabel("1");
        button1.addActionListener(new Frame1_button1_actionAdapter(this));
        button2.setLabel("+");
        button2.addActionListener(new Frame1_button2_actionAdapter(this));
        button3.setLabel("=");
        button3.addActionListener(new Frame1_button3_actionAdapter(this));
        button4.setLabel("4");
        button4.addActionListener(new Frame1_button4_actionAdapter(this));
        button5.setLabel("2");
        button5.addActionListener(new Frame1_button5_actionAdapter(this));
        button6.setLabel("3");
        button6.addActionListener(new Frame1_button6_actionAdapter(this));
        button7.setLabel("5");
        button7.addActionListener(new Frame1_button7_actionAdapter(this));
        button8.setLabel("6");
        button8.addActionListener(new Frame1_button8_actionAdapter(this));
        button9.setLabel("7");
        button9.addActionListener(new Frame1_button9_actionAdapter(this));
        button10.setLabel("8");
        button10.addActionListener(new Frame1_button10_actionAdapter(this));
        button11.setLabel("9");
        button11.addActionListener(new Frame1_button11_actionAdapter(this));
        button12.setLabel("/");
        button13.setLabel("*");
        button14.setLabel("-");
        button15.setLabel("0");
        button15.addActionListener(new Frame1_button15_actionAdapter(this));
        button16.setLabel(".");
        button14.addActionListener(new Frame1_button14_actionAdapter(this));
        button13.addActionListener(new Frame1_button13_actionAdapter(this));
        button12.addActionListener(new Frame1_button12_actionAdapter(this));
        contentPane.add(textField1, new XYConstraints(14, 19, 366, 54));
        contentPane.add(button9, new XYConstraints(179, 84, 41, 28));
        contentPane.add(button10, new XYConstraints(227, 85, 47, 28));
        contentPane.add(button8, new XYConstraints(227, 118, 48, 33));
        contentPane.add(button7, new XYConstraints(179, 118, 42, 34));
        contentPane.add(button1, new XYConstraints(179, 157, 46, 33));
        contentPane.add(button14, new XYConstraints(335, 155, 45, 33));
        contentPane.add(button15, new XYConstraints(180, 194, 44, 35));
        contentPane.add(button6, new XYConstraints(279, 156, 49, 34));
        contentPane.add(button4, new XYConstraints(280, 119, 47, 34));
        contentPane.add(button11, new XYConstraints(279, 84, 50, 30));
        contentPane.add(button13, new XYConstraints(335, 119, 44, 31));
        contentPane.add(button12, new XYConstraints(335, 86, 43, 29));
        contentPane.add(button5, new XYConstraints(229, 157, 45, 34));
        contentPane.add(button2, new XYConstraints(279, 194, 47, 33));
        contentPane.add(button3, new XYConstraints(336, 192, 43, 35));
        contentPane.add(button16, new XYConstraints(235, 198, 40, 30));
    }
 
    public void button1_actionPerformed(ActionEvent e) {
        String help;
        help = textField1.getText();
        textField1.setText(help+"1");
    }
 
    public void button5_actionPerformed(ActionEvent e) {
        String help;
        help = textField1.getText();
        textField1.setText(help+"2");
 
    }
 
    public void button6_actionPerformed(ActionEvent e) {
       String help;
       help = textField1.getText();
       textField1.setText(help+"3");
    }
 
    public void button4_actionPerformed(ActionEvent e) {
       String help;
       help = textField1.getText();
       textField1.setText(help+"4");
    }
 
    public void button7_actionPerformed(ActionEvent e) {
       String help;
       help = textField1.getText();
       textField1.setText(help+"5");
    }
 
    public void button8_actionPerformed(ActionEvent e) {
       String help;
       help = textField1.getText();
       textField1.setText(help+"6");
    }
 
    public void button9_actionPerformed(ActionEvent e) {
       String help;
       help = textField1.getText();
       textField1.setText(help+"7");
    }
 
    public void button10_actionPerformed(ActionEvent e) {
       String help;
       help = textField1.getText();
       textField1.setText(help+"8");
    }
 
    public void button11_actionPerformed(ActionEvent e) {
       String help;
       help = textField1.getText();
       textField1.setText(help+"9");
    }
 
    public void button15_actionPerformed(ActionEvent e) {
       String help;
       help = textField1.getText();
       textField1.setText(help+"0");
    }
 
    public void button2_actionPerformed(ActionEvent e) {
       String help;
       help = textField1.getText();
       Double number1 = Double.valueOf(help);
       textField1.setText("");
       operator = "+";
    }
 
    public void button3_actionPerformed(ActionEvent e) {
       String help;
       help = textField1.getText();
       Double number2 = Double.valueOf(help);
       result = calculateResult(operator,number1,number2);      //I must not accept number 2 here so what can i do?
       String si= String.valueOf(result);
       textField1.setText(si);
       operator="=";
    }
 
    public void button14_actionPerformed(ActionEvent e) {
        String help;
        help = textField1.getText();
        Double number1 = Double.valueOf(help);
        textField1.setText("");
        operator = "-";
    }
 
    public void button13_actionPerformed(ActionEvent e) {
        String help;
        help = textField1.getText();
        Double number1 = Double.valueOf(help);
        textField1.setText("");
        operator = "*";
    }
 
    public void button12_actionPerformed(ActionEvent e) {
        String help;
        help = textField1.getText();
        Double number1 = Double.valueOf(help);
        textField1.setText("");
        operator = "/";
    }
 
public double calculateResult(String operator, double number1, double number2)
    {
        if(operator=="+")
            result = number1 + number2;
        else if(operator=="-")
            result = number1 - number2;
        else if(operator=="*")
            result = number1 * number2;
        else if(operator=="/")
            result = number1 / number2;
        return result;
    }
 
 
    class Frame1_button3_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button3_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button3_actionPerformed(e);
    }
}
 
 
class Frame1_button2_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button2_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button2_actionPerformed(e);
    }
}
 
 
class Frame1_button15_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button15_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button15_actionPerformed(e);
    }
}
 
 
class Frame1_button11_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button11_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button11_actionPerformed(e);
    }
}
 
 
class Frame1_button10_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button10_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button10_actionPerformed(e);
    }
}
 
 
class Frame1_button9_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button9_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button9_actionPerformed(e);
    }
}
 
 
class Frame1_button8_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button8_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button8_actionPerformed(e);
    }
}
 
 
class Frame1_button7_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button7_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button7_actionPerformed(e);
    }
}
 
 
class Frame1_button4_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button4_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button4_actionPerformed(e);
    }
}
 
 
class Frame1_button6_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button6_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button6_actionPerformed(e);
    }
}
 
 
class Frame1_button5_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button5_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button5_actionPerformed(e);
    }
}
 
 
class Frame1_button1_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button1_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button1_actionPerformed(e);
    }
}
}
 
 
class Frame1_button12_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button12_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button12_actionPerformed(e);
    }
}
 
 
class Frame1_button13_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button13_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button13_actionPerformed(e);
    }
}
 
 
class Frame1_button14_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_button14_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }
 
    public void actionPerformed(ActionEvent e) {
        adaptee.button14_actionPerformed(e);
    }
}

You create a double number2 at the beginning of your program, but
in the function: button3_actionPerformed you create another Double number2. This is not wrong, but the calculateResult function takes as arguments: 1 String and 2 double. The number2 you are passing is not a double primitive type but a Double object.

double calculateResult(String operator, double number1, double number2)

Use something like this:

Double number2 = Double.valueOf(help);
double d=number2.doubleValue();
result = calculateResult(operator,number1,d);

You must be using a JDK prior to 1.5, because 1.5 and 1.6 will allow you to pass that Double in place of a double parameter (autoboxing).

You can use Double.parseDouble(String) to convert direct from a String to a double primitive value without the need to first create a Double.

Aside from that, do not use "==" to compare Strings in your calculate function. Use the equals() method.

You are just creating a lot of extra unnecessary code by declaring an inner class for each button listener and then just calling back into a function that performs the action. You can simplify the whole thing by having a single listener, either as an inner class or by implementing ActionListener() directly withing the class, and handling the various functions by source like so

if (e.getSource()==button3){
  // do button3 stuff
}

A better organization would be to have one listener class for the number buttons and one for the operator buttons, since the behavior for each of those groups of buttons only differs by the value of the button that was pressed.

Finally, use variable names that actually mean something. Button3 doesn't describe what it's for at all. "btnEquals" or "btnPerformEquals" would be a lot clearer and having meaningful variable names will help you keep better track of what your code is supposed to be doing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.