public static void main(String[] args) { ConsoleIO con = new ConsoleIO(); Shape tri = new Triangle(); tri.name = "Triangle"; tri.setColor = "Yellow"; tri.getArea(); tri.getPerimeter(); Shape sq = new Square(); System.out.println("square"); sq.setColor = "blue"; sq.getArea(); sq.getPerimeter(); Shape rec = new Rectangle(); System.out.println("rectangle"); rec.setColor = "Green"; rec.getArea(); rec.getPerimeter(); Shape circ = new Circle(); System.out.println("circle"); circ.setColor = "Red"; circ.getArea(); circ.getPerimeter(); }
public static void main(String[] args) { // square Square square = new Square(5.6); System.out.println("area: " + square.getArea()); System.out.println("perimeter: " + square.getPerimeter()); // rectangle Rectangle rectangle = new Rectangle(1.2, 3.4); System.out.println("area: " + rectangle.getArea()); System.out.println("perimeter: " + rectangle.getPerimeter()); // circle Circle circle = new Circle(1.2); System.out.println("area: " + circle.getArea()); System.out.println("perimeter: " + circle.getPerimeter()); // triangle Triangle triangle = new Triangle(1.2, 1.2, 1.2); System.out.println("area: " + triangle.getArea()); System.out.println("perimeter: " + triangle.getPerimeter()); // shape Shape shape = new Circle(1); System.out.println("area: " + shape.getArea()); System.out.println("perimeter: " + shape.getPerimeter()); }
public static void main(String[] args) { List<Shape> shapes = new ArrayList<>(); // Static Factory Method Shape square = Shape.createSquareWithSidesOfLength(5); shapes.add(square); // Abstract Factory Shape circle = new CircleFactory().create(5); shapes.add(circle); for (Shape shape : shapes) { System.out.println( shape.getName() + " has an area of: " + shape.getArea() + " and a perimeter of: " + shape.getPerimeter()); } }