Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

java - 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("
A rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +
rectangle.getPerimeter());
}
}`
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...