1

I'm very new to Java, and I keep getting this compile error when trying to compile my code:

"MyRectangle.java:3: error: constructor Rectangle in class Rectangle cannot be applied to given types; Rectangle rectangle1 = new Rectangle(5.0, 10.0, "red"); ^ required: no arguments found: double,double,String reason: actual and formal argument lists differ in length

MyRectangle.java:8: error: constructor Rectangle in class Rectangle cannot be applied to given types; Rectangle rectangle2 = new Rectangle(3.5, 4.3, "yellow"); ^ required: no arguments found: double,double,String reason: actual and formal argument lists differ in length"

Here is my code:

public class MyRectangle {
  public static void main (String[] args) {
    Rectangle rectangle1 = new Rectangle(5.0, 10.0, "red");
      System.out.println("The width of the first rectangle is " + 
          rectangle1.getWidth() + 
         " the height is " + rectangle1.getHeight() + " the area is " + 
           rectangle1.findArea() + 
           " and the color is " + rectangle1.getColor());

    Rectangle rectangle2 = new Rectangle(3.5, 4.3, "yellow");
      System.out.println("The width of the second rectangle is " + 
        rectangle2.getWidth() +
        " the height is " + rectangle2.getHeight() + " the area is " + 
          rectangle2.findArea() +
         " and the color is " + rectangle2.getColor());
  }
}

class Rectangle {
  private double width = 1.0;
  private double height = 1.0;
  private static String color = "white";

  public double MyRectangle(){ 
  }

  public double MyRectangle(double widthParam, double heightParam, String 
   colorParam){ 
  }

  public double getWidth(){
    return width;     
    }

  public void setWidth(double widthParam){
    width = widthParam;   
    }

  public double getHeight(){ 
    return height;
    }

  public void setHeight(double heightParam){
    height = heightParam;     
    }

  public String getColor(){
    return color;     
    }  

  public static void setColor(String colorParam){ 
    color = colorParam;
    }

  public double findArea(){ 
   return width * height;
   }
 }
L.Largent
  • 11
  • 2
  • 3
    Remove the `double` return type from the "constructors". When you give it a return type, it is just a method with the same name as the class – GBlodgett Mar 30 '19 at 03:46
  • Possible duplicate of [java "void" and "non void" constructor](https://stackoverflow.com/questions/24963718/java-void-and-non-void-constructor) – GBlodgett Mar 30 '19 at 03:46
  • 2
    You also need to rename the "constructors" to `Rectangle` for the class `Rectangle`. And **don't** make `color` `static`! – Elliott Frisch Mar 30 '19 at 03:51

1 Answers1

0

Your error is that you're trying to assign a data type (double, int, float, etc) to your constructor. In Java the constructor is defined just by the name of the class and the parameters. Example.

public class MyClass(){
   //My constructor
   public MyClass(){
     //My code
   }
}