-7

I want to implement this OOP diagram in JAVA.

Is this an Is-a relation in Object-Oriented Programming?

I wrote some code but I have an error.

Is my implementation right? How can I fix this code?

Could you please explain about my problems with this code?

Thanks in advance

Exception in thread "main" java.lang .Error: Unresolved compilation problems:

The public type Circle must be defined in its own file The public type Square must be defined in its own file

enter image description here

import java.util.*;

abstract class Shape
{
    boolean Visit = false;
    abstract void CalculatePerimeter();
    abstract void CalculateArea();
    public Shape(Boolean q) {
        Visit = q;
    }
}
public class Circle extends Shape {
    private int radius = 1;
    private final double PI = 3.14;

    public Circle(final Boolean q, final int r) {
        super(q);
        radius = r;
    }
    @Override
    void CalculateArea() {
        System.out.printf("Area of Circle is %d \n", PI * radius * radius);
    }
    @Override
    void CalculatePerimeter() {
        System.out.printf("Perimeter of Circle is %d \n", 2 * PI * radius);
    }
}
public class Square extends Shape {
    private int side = 1;
    public Square(final Boolean q, final int s) {
        super(q);
        side = s;
    }
    @Override
    void CalculateArea() {
        System.out.printf("Area of Square is %d \n", side * side);
    }
    @Override
    void CalculatePerimeter() {
        System.out.printf("Perimeter of Square is %d \n", side * 4);
    }
}
public class NEW {
    public static void main(final String[] args) {
        Circle cricle = new Circle(true, 10);
         Square square = new Square(true, 10);
        }
    }
Hadi
  • 85
  • 1
  • 7

3 Answers3

1

Separate each class into a new java file. Java will not compile a file containing many public class declarations.

aksappy
  • 3,400
  • 3
  • 23
  • 49
  • That's right, but if this was the issue, then another error message would have been shown ("class definition must be in its own file" or something like that). – Anis R. Nov 15 '19 at 21:38
  • @AnisR. that *is* the issue. Take a look at the edited question. – f1sh Nov 15 '19 at 21:39
1

Any of the following solutions will work:

  1. Remove the keyword, public from the definition of the classes, Circle and Square
  2. Write the classes, Circle and Square as they are (i.e. with public keyword) in separate files.

The reason is that Java supports only one public class in a source file.

A couple of more points:

A. You should instantiate Circle and Square as follows:

Shape circle = new Circle(true, 10);
Shape square = new Square(true, 10);

B. As per your assignment (in the picture), you are supposed to use boolean but you have used Boolean. Note that it doesn't have anything to do with the compilation errors.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Thank you, my problem has been solved and I don't have any error message now. But Is my implementation in accordance with diagram? or Should I use Interface? – Hadi Nov 15 '19 at 21:52
0
Cricle cricle = new Circle(true, 10);

Mind the typo! (you have Cricle instead of Circle in many places in your code).

Edit: After your latest edit to the question, your code runs totally fine (given you have each class in its separate file). Are you sure you saved your files again before re-running your program?

Anis R.
  • 6,656
  • 2
  • 15
  • 37