Hint
In the loop you only call s.area() – you do not check whether the shape is a circle or a triangle. Java itself chooses the right method.
Solution (Java)
import java.util.Scanner;
abstract class Shape {
abstract double area();
abstract String name();
}
class Circle extends Shape {
double r;
Circle(double r) { this.r = r; }
@Override double area() { return r * r * Math.PI; }
@Override String name() { return "Circle"; }
}
class Rectangle extends Shape {
double a, b;
Rectangle(double a, double b) { this.a = a; this.b = b; }
@Override double area() { return a * b; }
@Override String name() { return "Rectangle"; }
}
class Triangle extends Shape {
double a, h;
Triangle(double a, double h) { this.a = a; this.h = h; }
@Override double area() { return a * h / 2; }
@Override String name() { return "Triangle"; }
}
public class GeometricShapes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in).useLocale(java.util.Locale.US); // a decimal point in the input
int n = sc.nextInt();
Shape[] shapes = new Shape[n];
for (int i = 0; i < n; i++) {
String kind = sc.next();
if (kind.equals("C")) shapes[i] = new Circle(sc.nextDouble());
else if (kind.equals("R")) shapes[i] = new Rectangle(sc.nextDouble(), sc.nextDouble());
else shapes[i] = new Triangle(sc.nextDouble(), sc.nextDouble());
}
double total = 0;
for (Shape s : shapes) { // polymorphism
System.out.printf(java.util.Locale.US, "%s: %.2f%n", s.name(), s.area());
total += s.area();
}
System.out.printf(java.util.Locale.US, "Total: %.2f%n", total);
}
}