Advertisement
AS8

openclose.java

AS8
Sep 22nd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. interface Shape{
  4.     double getArea();
  5. }
  6.  
  7. class Circle implements Shape{
  8.     double radius;
  9.     Circle(double radius){
  10.         this.radius = radius;
  11.     }
  12.     public double getArea(){
  13.         return Math.PI*this.radius*this.radius;
  14.     }
  15. }
  16.  
  17. class Square implements Shape{
  18.     double side;
  19.     Square(double side){
  20.         this.side = side;
  21.     }
  22.     public double getArea(){
  23.         return side*side;
  24.     }
  25. }
  26. class Rectangle implements Shape{
  27.     double length,width;
  28.     Rectangle(double x,  double y){
  29.         this.length = x;
  30.         this.width=y;
  31.     }
  32.     public double getArea(){
  33.         return length*width;
  34.     }
  35. }
  36. class CalculateAreaSummation{
  37.     ArrayList<Shape>arr = new ArrayList<>();
  38.     double total = 0;
  39.  
  40.     void addShape(Shape shape){
  41.         arr.add(shape);
  42.     }
  43.  
  44.     public double calcArea(){
  45.         this.total = 0;
  46.         for(Shape shape : arr){
  47.          total+=shape.getArea();
  48.         }
  49.         return total;
  50.     }
  51.  
  52.     void printOutput(){
  53.         System.out.println("total: " + total);
  54.     }
  55. }
  56.  
  57.  
  58. class OpenClosed{
  59.     public static void main(String[] args){
  60.         Shape circle = new Circle(5);
  61.         Shape square = new Square(10);
  62.  
  63.         CalculateAreaSummation sum = new CalculateAreaSummation();
  64.         sum.addShape(circle);
  65.         System.out.println(sum.calcArea());
  66.  
  67.         sum.addShape(square);
  68.         System.out.println(sum.calcArea());
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement