Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- interface Shape{
- double getArea();
- }
- class Circle implements Shape{
- double radius;
- Circle(double radius){
- this.radius = radius;
- }
- public double getArea(){
- return Math.PI*this.radius*this.radius;
- }
- }
- class Square implements Shape{
- double side;
- Square(double side){
- this.side = side;
- }
- public double getArea(){
- return side*side;
- }
- }
- class Rectangle implements Shape{
- double length,width;
- Rectangle(double x, double y){
- this.length = x;
- this.width=y;
- }
- public double getArea(){
- return length*width;
- }
- }
- class CalculateAreaSummation{
- ArrayList<Shape>arr = new ArrayList<>();
- double total = 0;
- void addShape(Shape shape){
- arr.add(shape);
- }
- public double calcArea(){
- this.total = 0;
- for(Shape shape : arr){
- total+=shape.getArea();
- }
- return total;
- }
- void printOutput(){
- System.out.println("total: " + total);
- }
- }
- class OpenClosed{
- public static void main(String[] args){
- Shape circle = new Circle(5);
- Shape square = new Square(10);
- CalculateAreaSummation sum = new CalculateAreaSummation();
- sum.addShape(circle);
- System.out.println(sum.calcArea());
- sum.addShape(square);
- System.out.println(sum.calcArea());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement