commit
2db76ce98e
12 changed files with 87 additions and 0 deletions
Binary file not shown.
@ -0,0 +1,17 @@ |
|||||
|
public class Circle extends Shape { |
||||
|
private double radius; |
||||
|
|
||||
|
public Circle(double radius) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return Math.PI * radius * radius; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void draw() { |
||||
|
System.out.println("Drawing a circle with radius " + radius); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,19 @@ |
|||||
|
public class Main { |
||||
|
public static void main(String[] args) { |
||||
|
Shape circle = new Circle(5); |
||||
|
Shape rectangle = new Rectangle(4, 6); |
||||
|
Shape triangle = new Triangle(3, 8); |
||||
|
|
||||
|
System.out.println("Circle:"); |
||||
|
ShapeUtil.printArea(circle); |
||||
|
ShapeUtil.drawShape(circle); |
||||
|
|
||||
|
System.out.println("Rectangle:"); |
||||
|
ShapeUtil.printArea(rectangle); |
||||
|
ShapeUtil.drawShape(rectangle); |
||||
|
|
||||
|
System.out.println("Triangle:"); |
||||
|
ShapeUtil.printArea(triangle); |
||||
|
ShapeUtil.drawShape(triangle); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,19 @@ |
|||||
|
public class Rectangle extends Shape { |
||||
|
private double width; |
||||
|
private double height; |
||||
|
|
||||
|
public Rectangle(double width, double height) { |
||||
|
this.width = width; |
||||
|
this.height = height; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return width * height; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void draw() { |
||||
|
System.out.println("Drawing a rectangle with width " + width + " and height " + height); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,4 @@ |
|||||
|
public abstract class Shape { |
||||
|
public abstract double getArea(); |
||||
|
public abstract void draw(); |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,9 @@ |
|||||
|
public class ShapeUtil { |
||||
|
public static void printArea(Shape shape) { |
||||
|
System.out.println("Area: " + shape.getArea()); |
||||
|
} |
||||
|
|
||||
|
public static void drawShape(Shape s) { |
||||
|
s.draw(); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,19 @@ |
|||||
|
public class Triangle extends Shape { |
||||
|
private double base; |
||||
|
private double height; |
||||
|
|
||||
|
public Triangle(double base, double height) { |
||||
|
this.base = base; |
||||
|
this.height = height; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return 0.5 * base * height; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void draw() { |
||||
|
System.out.println("Drawing a triangle with base " + base + " and height " + height); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue