Search code examples
javajgrasp

Cant compile, class not declared Java


I'm trying to do this following program. I deleted the last part of MyRectangle2D in order to shorten it down a bit. When I try to compile I get 2 errors, i just can't work my way through!

TestExample.java:17: class GeometricObject2 is public, should be declared in a file named GeometricObject2.java
public abstract class GeometricObject2 {

TestExample.java:11: cannot find symbol
symbol  : constructor MyRectangle2D(double,double,double,double)
location: class MyRectangle2D
      GeometricObject2 rectangle1 = new MyRectangle2D(1.0, 2.0, 3.0, 4.0);

2 errors

help is much appreciated !

import java.util.*;

public class TestExample
{
   public static void main(String[] args)
   {
      GeometricObject2 rectangle1 = new MyRectangle2D(1.0, 2.0, 3.0, 4.0);
      System.out.println(rectangle1.getArea());
   }
}


public abstract class GeometricObject2 {
  private String color = "white";
  private boolean filled;


  protected GeometricObject2() {
  }


  protected GeometricObject2(String color, boolean filled) {
    this.color = color;
    this.filled = filled;
  }

  public String getColor() {
    return color;
  }

  public void setColor(String color) {
    this.color = color;
  }

  public boolean isFilled() {
    return filled;
  }

  public void setFilled(boolean filled) {
    this.filled = filled;
  }

  public abstract double getArea();

  public abstract double getPerimeter();
}



class MyRectangle2D extends GeometricObject2 {

}


Solution

  • If you want to implement everything in TestExample.java file, then I would try something like this:

    import java.util.*;
    
    public class TestExample
    {
        public static void main(String[] args)
        {
            GeometricObject2 rectangle1 = new MyRectangle2D(2, 2, 3, 4, "Red", true);
            System.out.println(rectangle1.getArea());
        }
    }
    
    
    abstract class GeometricObject2 {
        private String color = "white";
        private boolean filled;
    
    
        protected GeometricObject2() {
        }
    
    
        protected GeometricObject2(String color, boolean filled) {
            this.color = color;
            this.filled = filled;
        }
    
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    
        public boolean isFilled() {
            return filled;
        }
    
        public void setFilled(boolean filled) {
            this.filled = filled;
        }
    
        public abstract double getArea();
    
        public abstract double getPerimeter();
    }
    
    
    
    class MyRectangle2D extends GeometricObject2
    {
        private double x;
        private double y;
        private double width;
        private double height;
    
        public MyRectangle2D(double x, double y, double width, double height,
                             String color, boolean filled) {
            super(color, filled);
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }
    
        public double getArea() {
            return width * height;
        }
    
        public double getPerimeter() {
            return 2 * (width + height);
        }
    }
    

    And you didn't post the full code of MyRectangle2D class, so I really don't know how you intend to implement it...