Question

I am not sure what I am doing wrong in this coding. I keep getting this...

I am not sure what I am doing wrong in this coding. I keep getting this error. I tried to change it to Circle2D.java but it still comes an error:

1. Compiler error: class Circle2D is public, should be declared in a file named Circle2D.java
2. public class Circle2D {

public class Exercise10_11 {
public static void main(String[] args) {
Circle2D c1 = new Circle2D(2, 2, 5.5);
System.out.println("area: " + c1.getArea());
System.out.println("perimeter: " + c1.getPerimeter());
System.out.println("contains(3, 3): " + c1.contains(3, 3));
System.out.println("contains(new Circle2D(4, 5, 10.5)): " +
c1.contains(new Circle2D(4, 5, 10.5)));
System.out.println("overlaps(new Circle2D(3, 5, 2.3)): " +
c1.overlaps(new Circle2D(3, 5, 2.3)));
}
}

public class Circle2D {
private double x;
private double y;

public Circle2D() {
x = 0;
y = 0;
}

public Circle2D(double x, double y, double radius) {
this.x = x;
this.y = y;
}

public double getArea() {
}

public double getPerimeter() {
return 2 * Math.PI * radius;
}

public boolean contains(double x, double y) {
return distance(this.x, this.y, x, y) <= radius;
}

public boolean contains(Circle2D circle) {
double x1 = getX();
double y1 = getY();
double x2 = circle.getX();
double y2 = circle.getY();
}

public boolean overlaps(Circle2D circle) {
double x1 = getX();
double y1 = getY();
double x2 = circle.getX();
double y2 = circle.getY();
}

private static double distance(double x1, double y1, double x2, double y2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}

public double getX() {
return x;
}

public double getY() {
return y;
}

}
}

In Java, every class should have a main method because when you compile your program, the execution always starts from main method (I assume there are no static blocks)
So it's mandatory to have a main method in your program unless it's a JavaFX program.

Just make sure that your class has a main method in it and the definition should follow this:

public static void main(String[] args){ //code }

• Compiler error: class Circle2D is public, should be declared in a file named Circle2D.java

This error comes when you compile a program that has two public classes in same file. One file cannot have more than one public class, in case if you need multiple public classes then it's better to split up your program into different files.

Make only that class as public that has main method in it.