0

Can someone kindly tell me what is missing to make this calculator add, sub,divide, and multiply.

If you could kindly fix it, I would really appreciate. At the stage of the lesson from http://www.myflex.org/books/JavaKid8x11.pdf, it more important for someone at my level to understand basic events.

The PDF is for kids to learn Java, but is seems to me the author might not have tested the code in a Java environment because I have typed it verbatim, and it is not working.

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;


public class  CalculatorEngine implements ActionListener {
Calculator parent;
char selectedAction = ' ';

double currentResult =0;

CalculatorEngine(Calculator parent){
    this.parent = parent;
}


    @Override
    public void actionPerformed(ActionEvent e) {

    JButton clickedButton =(JButton) e.getSource();
    String displayFieldText = parent.displayField.getText();
    double displayValue =0;


    if(!" ".equals(displayFieldText)){
        displayValue = Double.parseDouble(displayFieldText);


    }

    Object src = e.getSource();

    if(src == parent.buttonPlus){
        selectedAction = '+';
        currentResult = displayValue;
        parent.displayField.setText("");


    }else if (src == parent.buttonMinus){
        selectedAction = '-';
        currentResult = displayValue;
        parent.displayField.setText("");

    }else if(src == parent.buttonDivide){
        selectedAction = '/';
        currentResult = displayValue;
        parent.displayField.setText("");

    }else if(src == parent.buttonMultiply){
        selectedAction = '*';
        currentResult = displayValue;
        parent.displayField.setText("");

    }else if (src == parent.buttonEqual){

        if(selectedAction == '+' ){
            currentResult += displayValue;
            parent.displayField.setText("" + currentResult);
        }else if(selectedAction == '-'){
            currentResult -= displayValue;
            parent.displayField.setText("" + currentResult);

        }else if(selectedAction == '/'){
            currentResult /= displayValue;
            parent.displayField.setText("" + currentResult);

        }else if(selectedAction == '*'){
            currentResult *= displayValue;
            parent.displayField.setText("" + currentResult);

        }

    }else{

        String clickedButtonLabel = clickedButton.getText();
        parent.displayField.setText(displayFieldText + clickedButtonLabel );
    }
        // TODO Auto-generated method stub

    }





    public static void main(String[] args) {
        // TODO Auto-generated method stub


    }

}

import javax.swing.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;

public class Calculator{

private static final Calculator Calculator = null;
JButton button0 = new JButton("0");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton buttonPoint = new JButton(".");
JButton buttonEqual = new JButton("=");
JButton buttonMinus = new JButton("-");
JButton buttonPlus = new JButton("+");
JButton buttonDivide = new JButton("/");
JButton buttonMultiply = new JButton("*");
JPanel windowContent = new JPanel(); 
JTextField displayField = new JTextField(30);

Calculator(){

    BorderLayout bl = new BorderLayout();
    windowContent.setLayout(bl);
    windowContent.add("North", displayField);

    JPanel p1 = new JPanel(); 
    GridLayout gl =new GridLayout(4,3); 

    p1.setLayout(gl); 
    p1.add(button1); 
    p1.add(button2); 
    p1.add(button3); 
    p1.add(button4); 
    p1.add(button5);
    p1.add(button6); 
    p1.add(button7); 
    p1.add(button8);
    p1.add(button9);
    p1.add(button0);
    p1.add(button9);
    p1.add(buttonPoint);
    p1.add(buttonEqual);

    windowContent.add("Center",p1);

    JPanel p2 = new JPanel();
    GridLayout gl2 =new GridLayout(4,1);
    p2.setLayout(gl2); 
    p2.add(buttonPlus);
    p2.add(buttonMinus); 
    p2.add(buttonMultiply);
    p2.add(buttonDivide);

    windowContent.add("East",p2);

    JFrame frame = new JFrame ("Calculator");
    frame.setContentPane(windowContent);
    frame.pack();
    frame.setVisible(true);

    CalculatorEngine calcEngine = new CalculatorEngine (this);
    button0.addActionListener(calcEngine);
    button1.addActionListener(calcEngine);
    button2.addActionListener(calcEngine);
    button3.addActionListener(calcEngine);
    button4.addActionListener(calcEngine);
    button5.addActionListener(calcEngine);

    button6.addActionListener(calcEngine);
    button7.addActionListener(calcEngine);
    button8.addActionListener(calcEngine);
    button9.addActionListener(calcEngine);
    buttonPoint.addActionListener(calcEngine);
    buttonPlus.addActionListener(calcEngine);
    buttonMinus.addActionListener(calcEngine);
    buttonDivide.addActionListener(calcEngine);
    buttonMultiply.addActionListener(calcEngine);
    buttonEqual.addActionListener(calcEngine);

}
public static void main(String[] args) {    
    Calculator calc = new Calculator ();



}



}
appletree
  • 57
  • 6
  • Hello Yshavit, This is a new question. The system kept failing me for how the code was displayed that I decided to leave it as such so that I can get my question posted. If these 2 questions have been answered please, send me the link. Thank you so much for the help – appletree Sep 02 '17 at 14:39
  • Can someone kindly run the above code on Eclipse and help me figure how to make it add, sub, divide, and multiply. the code is there, it is simply missing something that the book does not mention to make it work – appletree Sep 03 '17 at 14:10

0 Answers0