Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.3 kB
3
Indexable
Never
abstract class Shape {

  public abstract double calculateArea();

  public abstract double calculatePerimeter();
}


//Rectangle subclass
public class Rectangle extends Shape {
  private double width;
  private double height;
  

  public Rectangle(double width, double height){
      this.width=width;
      this.height=height;
  }



  public double calculateArea() {
    return width * height;
  }

  public double calculatePerimeter() {
    return 2.0 * (width + height);
  }
}


//Circle subclass
public class Circle extends Shape {
    private double radius;
    
    public Circle(double radius){
        this.radius=radius;
    }

  public double calculateArea() {
    return Math.PI * radius * radius;
  }
  
  public double calculatePerimeter() {
    return 2.0 * Math.PI * radius;
  }
}


public class Test{

    public static void main(String[] args) {
        Rectangle r= new Rectangle(2.00,5.00);
        Circle cr=new Circle(5);
        
        System.out.println("Rectangle: "+'\n'+"Area: "+r.calculateArea());
        System.out.println("Perimeter: "+r.calculatePerimeter());
        
        System.out.println("\nCircle: "+'\n'+"Area: "+cr.calculateArea());
        System.out.println ("Perimeter: "+cr.calculatePerimeter());
    }
}