An interface can carry shared behaviour in default methods, including the order objects sort in.
What to build
A program that reads shapes, sorts them by area and summarises them.
Requirements
- Write
interface Shape extends Comparable<Shape>witharea(),perimeter(),name()and default methodscompareTo(by area) andsummary()returningNAME: area A, perimeter P(two decimals each); and recordsCircle(double r),Rect(double w, double h)andTriangle(double a, double b, double c)implementing it (a triangle's area by Heron's formula). - Lines are
circle R,rect W Hortriangle A B C. Printinvalid trianglefor a triangle that breaks the triangle inequality (it is left out). Then print the valid shapes' summaries sorted by area, smallest first, andtotal area: T.
Input
Line 1: n. Then n lines.
Output
The invalid lines first, then one line per valid shape, then the total.
Constraints
1 ≤ n ≤ 50; sizes are positive numbers up to 1000.
Example
Input:
3
rect 2 3
circle 1
triangle 3 4 5
Output:
circle: area 3.14, perimeter 6.28
rect: area 6.00, perimeter 10.00
triangle: area 6.00, perimeter 12.00
total area: 15.14
Why it works
Every shape fulfils one contract, so sorting, summarising and totalling work on the interface alone, and the default methods are written once for all of them.
Examples
Example 1 · Input
3 rect 2 3 circle 1 triangle 3 4 5
Expected output
circle: area 3.14, perimeter 6.28 rect: area 6.00, perimeter 10.00 triangle: area 6.00, perimeter 12.00 total area: 15.14
Example 2 · Input
2 triangle 1 2 3 rect 1 1
Expected output
invalid triangle rect: area 1.00, perimeter 4.00 total area: 1.00
Hints
Hint 1
interface Shape extends Comparable<Shape> with area(), perimeter(), name(), and a default compareTo comparing areas.
Hint 2
Check the triangle inequality before making a triangle: each side shorter than the other two together.
Solution
Show one correct solution (try it yourself first)
import java.util.Arrays;
import java.util.Scanner;
interface Shape extends Comparable<Shape> {
double area();
double perimeter();
String name();
default int compareTo(Shape o) {
return Double.compare(area(), o.area());
}
default String summary() {
return String.format("%s: area %.2f, perimeter %.2f", name(), area(), perimeter());
}
}
record Circle(double r) implements Shape {
public double area() {
return Math.PI * r * r;
}
public double perimeter() {
return 2 * Math.PI * r;
}
public String name() {
return "circle";
}
}
record Rect(double w, double h) implements Shape {
public double area() {
return w * h;
}
public double perimeter() {
return 2 * (w + h);
}
public String name() {
return "rect";
}
}
record Triangle(double a, double b, double c) implements Shape {
public double area() {
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
public double perimeter() {
return a + b + c;
}
public String name() {
return "triangle";
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Shape[] shapes = new Shape[n];
int size = 0;
for (int i = 0; i < n; i++) {
String kind = in.next();
if (kind.equals("circle")) shapes[size++] = new Circle(in.nextDouble());
else if (kind.equals("rect")) shapes[size++] = new Rect(in.nextDouble(), in.nextDouble());
else {
double a = in.nextDouble(), b = in.nextDouble(), c = in.nextDouble();
if (a < b + c && b < a + c && c < a + b) shapes[size++] = new Triangle(a, b, c);
else System.out.println("invalid triangle");
}
}
Shape[] valid = Arrays.copyOf(shapes, size);
Arrays.sort(valid);
double total = 0;
for (Shape s : valid) {
System.out.println(s.summary());
total += s.area();
}
System.out.printf("total area: %.2f%n", total);
}
}