Java Error: The constructor is undefined

前端 未结 9 1258
庸人自扰
庸人自扰 2020-11-29 13:31

In Java, Why am I getting this error:

Error: The constructor WeightIn() is undefined

Java Code:

public class WeightIn{
  pr         


        
9条回答
  •  广开言路
    2020-11-29 13:51

    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

提交回复
热议问题