In Java, Why am I getting this error:
Error: The constructor WeightIn() is undefined
Java Code:
public class WeightIn{
pr
In this you can't do WeightIn weight1 = new WeightIn();
since default constructor is not defined.
So you can add
public WeightIn(){
}
Or you can do this
WeightIn weight1 = new WeightIn(3.65,1.7) // constructor accept two double values
In Java, Why am I getting this error:
Error: The constructor WeightIn() is undefined
It's simply because you didn't have the matching constructor for your class:
public class WeightIn {
...
public WeightIn (double weightIn, double heightIn){
weight = weightIn;
height = heightIn;
}
...
}
you need to add the public WeightIn() {}
.
In Java, the default constructor is automatically generated by the compiler if you didn't defined it. So, when you define the following class:
public class WeightIn {
private double weight;
private double height;
// didn't define a constructor.
public void setWeight(double weightIn){
weight = weightIn;
}
public void setHeight(double heightIn){
height = heightIn;
}
}
compiler will generating a matching default constructor for you. So, your class is implicitly having a default constructor like this:
public class WeightIn {
private double weight;
private double height;
// automatically generated by compiler
public WeightIn() {
// do nothing here.
}
// didn't define a constructor.
public void setWeight(double weightIn){
weight = weightIn;
}
public void setHeight(double heightIn){
height = heightIn;
}
}
when you instantiate the class with:
WeightIn weight = new WeightIn();
everything is alright.
But when you're adding a constructor by yourself, the default constructor will not generated by the compiler. So, when you're creating the class with:
public class WeightIn {
...
// this won't automatically generated by compiler
// public WeightIn() {
// nothing to do here.
//}
public WeightIn (double weightIn, double heightIn){
weight = weightIn;
height = heightIn;
}
...
}
You won't have the default constructor (i.e public WeightIn(){}
). And using the following will raise an error because you have no matching constructor:
WeightIn weight = new WeightIn();
The compiler is encountering a call to a "WeightIn()
" no argument constructor, on this line:
WeightIn weight1 = new WeightIn(); //Error happens here.
The compiler is looking for a matching constructor in the class definition, and its not finding it. That's the error. (You do have a constructor defined: "WeightIn(double,double)
" but that takes two arguments, and is not match.)
Several ways to fix this.
The easiest is to change the code in your main method to pass two arguments.
WeightIn weight1 = new WeightIn( 3.65, 1.7);
//weight1.setWeight(3.65);
//weight2.setHeight(1.7);
The calls to the setWeight
and setHeight
methods are redundant, since the members are already assigned values by the constructor method.
It took me a while but I think I finally figured out a way for this program to work.
I separated the classes into different files and renamed the weight Class to Record just so that it's more obvious instead of everything being named some variation of weight. I also added an Output class to keep the code in the main as clean and minimal as possible.
I hope this is similar to what you were hoping to do.
Original Code: "Constructor"
public class WeightIn{
private double weight;
private double height;
public WeightIn (double weightIn, double heightIn){
weight = weightIn; //needs to be : this.weight = weight
height = heightIn; //needs to be : this.height = height
}
Project: weightInApp
Package: weightInApp
Class: Record.Java
package weightInApp;
class Record
{
private double weight; //declare variables
private double height;
Record (double weight, double height) { //Parameterized Constructor method
this.weight = weight; //this is the correct way
this.height = height;
//if you want to use weightIn and/or heightIn you have to be consistent acrosss
//the whole class. I decided to drop "In" off the variables for brevity.
}
//Getters and setters
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
Project: weightInApp
Package: weightInApp
Class: Output.Java
This class outputs the set values to a table on the console. This can be manually altered to add more data. you could also consider tracking the date of the record and adding that to this output. you would need to add the requisite variables, getters, and setters in the Record Class for that functionality.
package weightInApp;
public class Output {
static void output (Record weightIn1, Record weightIn2)
{
int count = 0;
final Object[][] table = new Object[3][3];
table[0] = new Object[] { "Record", "Weight", "Height"};
table[1] = new Object[] { count +=1, weightIn1.getWeight(), weightIn1.getHeight() };
table[2] = new Object[] { count+=1, weightIn2.getWeight(), weightIn2.getHeight() };
for (final Object[] row : table) {
System.out.format("%15s%15s%15s\n", row);
}
}
}
Project: weightInApp
Package: weightInApp
Class: Main.Java
package weightInApp;
import weightInApp.Record; //imports methods and variables from the other classes
import weightInApp.Output;
public class Main {
public static void main (String [] args){
Record weightIn1 = new Record(0,0);
//previous line of code creates a new Record object named weightIn1
//previous line of code also sets both the values of weight and height to 0.
weightIn1.setWeight(3.65); //sets weight for weightIn1
weightIn1.setHeight(1.70);//sets Height for WeightIn1
Record weightIn2 = new Record(0, 0);
weightIn2.setWeight(3.00);
weightIn2.setHeight(1.75);
//previous 3 lines are the same as above for weightIn1
//except this is for a second object named weightIn2
Output.output(weightIn1, weightIn2); //calls runs passes values to output method
}
}
Sample Output
output sample from console
First of all, you should know that one .java file can have only one public class.
You are getting error because you have written parameterised constructor and accessing a default constructor. To fix this error write:
WeightIn weight1 = new WeightIn(5.2, 52.2);
instead of
WeightIn weight1 = new WeightIn();
Error: The constructor is undefined Possible Reasons:
2.You might have given parameters with wrong data type.