简体   繁体   中英

Assist me in finding the error: invalid method declaration; return type required

This is my last homework for my java class and I have been trying to run this through a compiler but I don't understand whats wrong with the code.

I tried using void after reading about how that would fix the return type issue but that just made it worse, maybe I was putting void in the wrong place.

public class Exercise09_01 {
    private double width = 1;
    private double height = 1;

    public Rectangle() {
    }

    public Rectangle(double newWidth, double newHeight) {
        width = newWidth;
        height = newHeight;
    }

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

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

    public static void main(String[] args) {
        Rectangle rectangle1 = new Rectangle(4, 40);
        System.out.println("The area of a 4.0 x 40.0 Rectangle is " + 
        rectangle1.getArea());
        System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " + 
        rectangle1.getPerimeter());
        Rectangle rectangle2 = new Rectangle(3.5, 35.9);
        System.out.println("The area of a 3.5 x 35.9 Rectangle is " + 
        rectangle2.getArea());
        System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " + 
        rectangle2.getPerimeter());
    }
}

This is my last homework for this class I just want this over with any help is appreciated.

Whats wrong in your code is your class name and constructor names are different.

You have two options, one is to rename the constructor to Exercise01_01 or meke the return type of the Rectangle as void.

public class Exercise01_01 {

    private double width = 1;
    private double height = 1;

    public Exercise01_01() {
    }

    public Exercise01_01(double newWidth, double newHeight) {
        width = newWidth;
        height = newHeight;
    }

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

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

    public static void main(String[] args) {
        Exercise01_01 rectangle1 = new Exercise01_01(4, 40);
        System.out.println("The area of a 4.0 x 40.0 Rectangle is " + rectangle1.getArea());
        System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " + rectangle1.getPerimeter());
        Exercise01_01 rectangle2 = new Exercise01_01(3.5, 35.9);
        System.out.println("The area of a 3.5 x 35.9 Rectangle is " + rectangle2.getArea());
        System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " + rectangle2.getPerimeter());
    }

}
public Rectangle() {
}

This is a method as far as Java is concerned. And all methods have to have a return type.

public Rectangle(double newWidth, double newHeight) {
width = newWidth;
  height = newHeight;
  }

The same here.

You don't need the 1st one really unless you really need to be able to make one without those values set.

You can just rename them, but you probably just want to rename the class Rectangle

The constructor name should have the same name as the class name

    public class Exercise09_01 {
        private double width = 1;
        private double height = 1;

        public Exercise09_01() {
        }

        public Exercise09_01(double newWidth, double newHeight) {
            width = newWidth;
            height = newHeight;
        }
   }
public class Exercise09_01 {
private double width = 1;
private double height = 1;

public Exercise09_01() {
}

public Exercise09_01(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}

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

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

public static void main(String[] args) {
Exercise09_01 rectangle1 = new Exercise09_01(4, 40);
System.out.println("The area of a 4.0 x 40.0 Rectangle is " + rectangle1.getArea());
System.out.println("The perimeter of a 4.0 x 40.0 Rectangle is " + 
rectangle1.getPerimeter());
Exercise09_01 rectangle2 = new Exercise09_01(3.5, 35.9);
System.out.println("The area of a 3.5 x 35.9 Rectangle is " + rectangle2.getArea());
System.out.println("The perimeter of a 3.5 x 35.9 Rectangle is " + 
rectangle2.getPerimeter());
}
}

Thanks for all the help and this is the code that passes through the compiler. Just leaving it here for future visitors.

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