Last active
April 23, 2024 00:14
-
-
Save chrisvasqm/c2169bc2e2cd5f42624a156a852de61c to your computer and use it in GitHub Desktop.
Shapes exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Main { | |
public static void main(String[] args) { | |
var shapes = Arrays.asList( | |
new Circle(3), | |
new Rectangle(5, 10), | |
new Square(5) | |
); | |
for (var shape : shapes) | |
System.out.println(shape.getArea()); | |
} | |
} | |
interface Shape { | |
double getArea(); | |
} | |
class Circle implements Shape { | |
private final double radius; | |
public Circle(double radius) { | |
this.radius = radius; | |
} | |
@Override | |
public double getArea() { | |
return Math.PI * Math.pow(this.radius, 2); | |
} | |
} | |
class Square implements Shape { | |
private final double width; | |
public Square(double width) { | |
this.width = width; | |
} | |
@Override | |
public double getArea() { | |
return this.width * this.width; | |
} | |
} | |
class Rectangle implements Shape { | |
private final double width; | |
private final double height; | |
public Rectangle(double width, double height) { | |
this.width = width; | |
this.height = height; | |
} | |
@Override | |
public double getArea() { | |
return this.width * this.height; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment