Skip to content

Instantly share code, notes, and snippets.

View fboeller's full-sized avatar

Fabian Böller fboeller

View GitHub Profile
class Builder {
String firstName;
String lastName;
int age;
public Builder firstName(String firstName) {
this.firstName = firstName;
return this;
}
class Person {
final String firstName;
final String lastName;
final int age;
Person(String firstName, String lastName, int age) {
Objects.requireNonNull(firstName);
Objects.requireNonNull(lastName);
this.firstName = firstName;
this.lastName = lastName;
fun diameter(shape: Shape): Double = when (shape) {
is Rectangle -> Math.sqrt(shape.x * shape.x + shape.y * shape.y)
is Circle -> 2 * shape.radius
is Parallelogram -> /* Math */
}
fun area(shape: Shape): Double = when (shape) {
is Rectangle -> shape.x * shape.y
is Circle -> PI * shape.radius * shape.radius
is Parallelogram -> shape.side * shape.height
}
fun perimeter(shape: Shape): Double = when (shape) {
is Rectangle -> (shape.x + shape.y) * 2
is Circle -> 2 * PI * shape.radius
is Parallelogram -> (shape.base + shape.side) * 2
}
sealed class Shape
class Rectangle(val x: Double, val y: Double) : Shape()
class Circle(val radius: Double) : Shape()
class Parallelogram(val base: Double, val height: Double): Shape()
fun diameter(shape: Shape): Double = when (shape) {
is Rectangle -> Math.sqrt(shape.x * shape.x + shape.y * shape.y)
is Circle -> 2 * shape.radius
}
fun area(shape: Shape): Double = when (shape) {
is Rectangle -> shape.x * shape.y
is Circle -> PI * shape.radius * shape.radius
}
fun perimeter(shape: Shape): Double = when (shape) {
is Rectangle -> (shape.x + shape.y) * 2
is Circle -> 2 * PI * shape.radius
}
interface Shape {}
class Rectangle implements Shape {
double x, y;
}
class Circle implements Shape {
double radius;
}