1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| abstract class Shape { public int x, y; public int width, height;
public Shape(int x1, int y1, int width1, int height1) { x = x1; y = y1; width = width1; height = height1; }
abstract double getArea();
abstract double getPerimeter(); }
class Square extends Shape { public double getArea() { return (width * height); }
public double getPerimeter() { return (2 * width + 2 * height); }
Square(int x, int y, int width, int height) { super(x, y, width, height); } }
class Circle extends Shape { public double r;
public double getArea() { return (r * r * Math.PI); }
public double getPerimeter() { return (2 * Math.PI * r); }
Circle(int x, int y, int width, int height) { super(x, y, width, height); r = (double) width / 2.0; } }
public class JBT4201 { public static void main(String args[]) { Square box = new Square(5, 15, 20, 20); Circle oval = new Circle(5, 50, 20, 20); System.out.println("Box Area==" + box.getArea()); System.out.println("Oval Area==" + oval.getArea()); } }
|