I am trying to introduce a Shape class as a parent interface for the classes Circle and Rectangle. I have to implement getName() method that will return Circle for the Circle object and Rectangle for the Rectangle object. Also, I have to override the toString() method in both the Circle and Rectangle classes. The toString methods will call the getName() method and will generate a string representing the object as follows:
Circlewith radius of 2 is represented as"Circle(2)"Rectanglewith width of 2 & height of 10 is represented as"Rectangle(2, 10)".
Also, I cannot modify the classes Shape, Rectangle, Circle or Main, for which you will find the codes below. I'm not sure about how to do this. Can someone please help me?
Here is what I have done so far:
Shape.java
public interface Shape {
String getName();
double getPerimeter();
double getArea();
}
Rectangle.java
public class Rectangle {
private double width, height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getPerimeter() {
return 2 * (this.width + this.height);
}
public double getArea() {
return this.width * this.height;
}
}
Circle.java
public class Circle{
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getPerimeter() {
return 2 * Math.PI * this.radius;
}
public double getArea() {
throw new RuntimeException("Oops, I don't know how to calculate this :(");
}
}
Question.aj
public aspect Question {
declare parents: Rectangle implements Shape;
declare parents: Circle implements Shape;
public String Rectangle.getName(){
return "Rectangle";
}
public String Circle.getName(){
return "Circle";
}
public String Rectangle.toString(){
return Rectangle.getName()+"(" + this.width +", " + this.height +")";
}
public String Circle.toString(){
return Circle.getName() + "(" + this.radius + ")";
}
}
Main.java
public class Main {
public static void main(String[] args) {
try {
Shape s;
s = (Shape) new Rectangle(2, 10);
System.out.println("The area of " + s + " is " + s.getArea());
s = (Shape) new Rectangle(-2, 10);
System.out.println("The perimeter of " + s + " is " + s.getPerimeter());
s = (Shape) new Circle(-2);
System.out.println("The perimeter of " + s + " is " + s.getPerimeter());
s = (Shape) new Circle(2);
System.out.println("The area of " + s + " is " + s.getArea());
}
catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}