Skip to content

Instantly share code, notes, and snippets.

@karnzx
Last active February 13, 2021 07:43
Show Gist options
  • Save karnzx/8fd7e1f10423c14dc18f517acb49965b to your computer and use it in GitHub Desktop.
Save karnzx/8fd7e1f10423c14dc18f517acb49965b to your computer and use it in GitHub Desktop.
import java.util.*;
class Shape {
private String name;
public Shape(String name) {
this.name = name;
}
public String getName() {
return name;
}
public double getArea() {
return 0;
}
}
class Rectangle extends Shape {
private double area;
private double width;
private double height;
public Rectangle(String name, double width, double height) {
super(name);
this.area = width * height;
this.width = width;
this.height = height;
}
public double getArea() {
return area;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
}
class Triangle extends Shape {
private double base, height, area;
public Triangle(String name, double base, double height) {
super(name);
this.base = base;
this.height = height;
this.area = 0.5 * base * height;
}
public double getArea() {
return area;
}
public double getBase() {
return base;
}
public double getHeight() {
return height;
}
}
public class Main {
public static double findTotalArea(ArrayList<Shape> shapes) {
double total = 0;
for (Shape shape : shapes) {
total += shape.getArea();
}
return total;
}
public static void main(String[] args) {
ArrayList<Shape> shapes = new ArrayList<Shape>();
shapes.add(new Rectangle("r1", 3, 8));
shapes.add(new Triangle("t1", 3, 2));
System.out.println(findTotalArea(shapes));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment