Skip to content

Instantly share code, notes, and snippets.

@Krisztiaan
Created December 5, 2017 18:42
Show Gist options
  • Save Krisztiaan/21a25ce5e645e29a49d42676e17a7e1b to your computer and use it in GitHub Desktop.
Save Krisztiaan/21a25ce5e645e29a49d42676e17a7e1b to your computer and use it in GitHub Desktop.
public class Rectangle {
public static final DEFAULT_EDGE_LENGTH = 1;
public final double a;
public final double b;
public final double c;
public Rectangle() {
this(DEFAULT_EDGE_LENGTH, DEFAULT_EDGE_LENGTH, DEFAULT_EDGE_LENGTH);
}
public Rectangle(double a) {
this(a, DEFAULT_EDGE_LENGTH, DEFAULT_EDGE_LENGTH);
}
public Rectangle(double a, double b) {
this(a, b, DEFAULT_EDGE_LENGTH);
}
public Rectangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double volume() {
return a * b * c;
}
public double area() {
return 2 * (a * b + a * c + b * c);
}
public double edge() {
return 4 * (a + b + c);
}
public static void main(String[] args) {
double a = 2;
double b = 3;
double c = 4;
Rectangle r1 = new Rectangle();
assertEdges(r1, DEFAULT_EDGE_LENGTH, DEFAULT_EDGE_LENGTH, DEFAULT_EDGE_LENGTH);
Rectangle r2 = new Rectangle(a);
assertEdges(r1, a, DEFAULT_EDGE_LENGTH, DEFAULT_EDGE_LENGTH);
Rectangle r3 = new Rectangle(a, b);
assertEdges(r1, a, b, DEFAULT_EDGE_LENGTH);
Rectangle r4 = new Rectangle(a, b, c);
assertEdges(r1, a, b, c);
assert r4.volume() == a * b * c;
assert r4.area() == 2 * (a * b + a * c + b * c);
assert r4.edge() == 4 * (a + b + c);
}
public static void assertEdges(Rectangle r, double a, double b, double c) {
assert r.a == a && r.b == b && r.c == c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment