-2

I'm writing a program for a calculator and I have added this into my main Java file`

CalculatorEngine calcEngine = new CalculatorEngine(); 

This lnks to my Class file as so:

public class CalculatorEngine implements ActionListener {//code here ;}

Can anyone tell me what I'm doing wrong?

This is the error message: "The constructor CalculatorEngine() is undefined" But I thought that's how you define it?

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

public class CalculatorEngine implements ActionListener {
Calculator parent; //a reference to Calculator window 
char selectedAction = ' '; // +, -, /, or *

double currentResult  =0;

//  Constructor  stores the reference to the Calculator 
//  window in the member variable parent 
CalculatorEngine(Calculator parent){ 

this.parent = parent;

}

public void actionPerformed(ActionEvent  e){

// Get the source of this action

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

//Get the number from the text field // if it’s not empty

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

}
Object src = e.getSource();

//For each action button memorise  selected 
//action +, -, /, or *, store the current value 
//in the currentResult,  and  clean up the display 
//field for entering the next number 

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){ 

//Perform the calculations based on selectedAction 
//update the value of the variable currentResult 
//and display the result 

if (selectedAction=='+'){ 
    currentResult +=displayValue;

//Convert the result to String by concatenating 
//to an empty string and display it 

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{

//  For all numeric buttons append the button's 
//  label to the text field 

String clickedButtonLabel= clickedButton.getText(); 
parent.displayField.setText(dispFieldText + clickedButtonLabel);
}}}

here is the class.

Bartholomew
  • 9
  • 1
  • 1
  • 5
  • 3
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Jan 20 '15 at 22:42
  • Please show all constructors in the `CalculatorEngine` class in your post. – rgettman Jan 20 '15 at 22:43
  • @James Click on [edit](http://stackoverflow.com/posts/28056473/edit) and add the code to your post. After pasting the code in, highlight it and press ctrl-k – Elliott Frisch Jan 20 '15 at 22:45
  • Sorry, added the class code, above. Cheers for the responses – Bartholomew Jan 20 '15 at 22:48
  • Well, your Constructor is: `CalculatorEngine(Calculator)`, but you're calling `new CalculatorEngine()` ... so where exactly is the confusion? – Tom Jan 20 '15 at 22:49
  • In your code you've clearly defined one single constructor - and it takes a `Calculator` as a parameter. No default constructors are generated when there is an explicit constructor defined. – Ordous Jan 20 '15 at 22:49
  • An error appears in my Java file saying "The constructor CalculatorEngine() is undefined" in particular "CalculatorEngine calcEngine = new CalculatorEngine();" Where " = new CalculatorEngine();" is underlined – Bartholomew Jan 20 '15 at 22:51
  • Once you define a constructor of your own, Java compiler will no longer add a default (no argument) constructor for you. If you need it, you need to define it yourself. – PM 77-1 Jan 20 '15 at 22:55

3 Answers3

3

Your code contains a single constructor with package level (default) permissions,

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

So you don't get the default constructor and you can't call the constructor that takes a Calculator anywhere but the same package (or a sub-class). Add an empty public constructor (or remove your existing one and you'll get the default constructor).

public CalculatorEngine(){
    super();
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I'm sorry, I don't understand. Do I need to change the public class? In this case, I tried this but it didn't let me implement the action listener. – Bartholomew Jan 20 '15 at 22:56
  • You could add a public no-arg constructor, you could remove your current constructor, or you could call it (assuming you're in the same package), or you could could modify your current constructor and make it public (but you'll also have to pass in a `Calculator`). I've only posted constructors. – Elliott Frisch Jan 20 '15 at 23:01
0

You are attempting pass in nothing to CalculatorEngine's only constructor. However, that constructor takes one parameter, a Calculator.

Pass in a calculator object to the constructor.

CalculatorEngine calcEngine = new CalculatorEngine(aCalculatorObject);
rgettman
  • 176,041
  • 30
  • 275
  • 357
0

This is the constructor:

CalculatorEngine(Calculator parent)

So you must supply a calculator when constructing. You cannot call new CalculatorEngine() because there is no parameterless constructor.

Note that in the absence of any other constructors a parameterless constructor is made available to you without needing to be defined. See https://softwareengineering.stackexchange.com/questions/257938/why-is-no-default-constructor-generated-if-you-define-an-explicit-constructor

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203