Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public abstract class Shapes{
    public abstract double area();
    public abstract double perimeter ();
}
public class Rectangle extends Shapes{
    double width;
    double length;
    public Rectangle(){
        this(1,1);
    }
    public Rectangle(double width, double length){
        this.width = width;
        this.length = length;
    }
    public double area(){
        return width * length;
    }
    public double perimeter(){
        return 2*(width + length);
    }
}

public class Triangle extends Shapes{
    double a;
    double b;
    double c;
    public Triangle(){
        this(1,1,1);
    }
    public Triangle (double a, double b, double c){
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public double area(){
        double s = (a+b+c)/2;
        return Math.sqrt(s*(s-a)*(s-b)*(s-c));
    }
    public double perimeter(){
        return a+ b + c;
    }
}

public class Test{
    public static void main(String[]args){
        double width = 10;
        double length = 25;
        Shapes Rectangle = new Rectangle (width, length);
        System.out.println("The area of the rectangle is: " + Rectangle.area());
        System.out.println("The perimeter of the rectangle is: " + Rectangle.perimeter());
    }
}

What I have tried:

I'm a little confused as to why the errors below are appearing.

Main.java:1: error: class Shapes is public, should be declared in a file named Shapes.java
public abstract class Shapes{
                ^
Main.java:5: error: class Rectangle is public, should be declared in a file named Rectangle.java
public class Rectangle extends Shapes{
       ^
Main.java:23: error: class Triangle is public, should be declared in a file named Triangle.java
public class Triangle extends Shapes{
       ^
Main.java:44: error: class Test is public, should be declared in a file named Test.java
public class Test{
       ^
4 errors

Would you be able to help me understand what I did wrong and how I can correct it?
Posted
Updated 9-Dec-22 16:55pm
Comments
Graeme_Grant 9-Dec-22 21:53pm    
the error message is quite clear for each: "... should be declared in a file named ..."

1 solution

The errors are telling you exactly what you need to so.

You appear to have defined all your classes in the same file. That's bad practice. Standard practice is to define one class per code file, preferably the filename should match the class name iot defines.

You need the move the code that defines the Shapes class into a file called "Shapes.java". The same goes for all the other classes. The Rectangle class should go into a file named "Rectangle.java", and so on.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900