简体   繁体   中英

Invalid method declaration, return type required

I am getting an error at public Rectangle(double width, double height){ saying that it's an invalid method declaration, return type required. I'm not sure how to fix it. These are also my instructions for my assignment: Write a super class encapsulating a rectangle. A rectangle has two attributes representing the width and the height of the rectangle. It has methods returning the perimeter and the area of the rectangle. This class has a subclass, encapsulating a parallelepiped, or box. A parallelepiped has a rectangle as its base, and another attribute, its length. It has two methods that calculate and return its area and volume.

`public class Rectangle1
{

private double width;
private double height;

public Rectangle1(){
}

public Rectangle(double width, double height){
this.width = width;
this.height = height;

}

public double getWidth(){
return width;
}

public void setWidth(double width) {
this.width = width;

}


public double getHeight(){
return height;

}

public void setHeight(double height){
this.height = height;

}

public double getArea(){
return width * height;
}

public double getPerimeter(){
return 2 * (width + height);

}

}



public class TestRectangle {

public static void main(String[] args) {

Rectangle1 rectangle = new Rectangle1(2,4);

System.out.println("\nA rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +
rectangle.getPerimeter());
}
}`

Constructor name should be same as your class name . your class name is Rectangle1 thus your Constructor name should be the same as well, currently java compiler this it as an method without a return type , thus it complains .

public Rectangle(double width, double height){

should be

public Rectangle1(double width, double height){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM