Skip to content

Instantly share code, notes, and snippets.

@chrisvasqm
Last active April 23, 2024 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisvasqm/c2169bc2e2cd5f42624a156a852de61c to your computer and use it in GitHub Desktop.
Save chrisvasqm/c2169bc2e2cd5f42624a156a852de61c to your computer and use it in GitHub Desktop.
Shapes exercise
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