Method Overloading

Lab Java Intermediate 20 points

Write three overloaded methods named area: area(double r) for a circle, area(double w, double h) for a rectangle, and area(double a, double b, double c) for a triangle (Heron's formula). Read shape lines and print each area with 2 decimals.

Input

n, then n lines: 1, 2 or 3 numbers.

Output

n lines.

Example

Input

3
1
3 4
3 4 5

Output

3.14
12.00
6.00
Sample input available Sample output available
Editor
Java

Hints

Overloads share a name but differ in their parameter lists.

Solution explanation

The compiler picks the overload whose parameters match the call.

Show solution
import java.util.Scanner;

public class Main {
    static double area(double r) {
        return Math.PI * r * r;
    }

    static double area(double w, double h) {
        return w * h;
    }

    static double area(double a, double b, double c) {
        double s = (a + b + c) / 2;
        return Math.sqrt(s * (s - a) * (s - b) * (s - c));
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine().trim());
        for (int i = 0; i < n; i++) {
            String[] p = sc.nextLine().trim().split("\\s+");
            double result;
            if (p.length == 1) {
                result = area(Double.parseDouble(p[0]));
            } else if (p.length == 2) {
                result = area(Double.parseDouble(p[0]), Double.parseDouble(p[1]));
            } else {
                result = area(Double.parseDouble(p[0]), Double.parseDouble(p[1]), Double.parseDouble(p[2]));
            }
            System.out.printf("%.2f%n", result);
        }
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD