int totalArea = 0; for (Shape shape : shapes) { totalArea += shape.getArea(); }
public static void main(final String[] args){ for(final Shape shape : Shape.values()){ System.out.printf("Shape: %s, area: %1.2f\n", shape, shape.getArea()); } }
@Override public double getArea(SpatialContext ctx) { double MAX_AREA = bbox.getArea(ctx); double sum = 0; for (Shape geom : shapes) { sum += geom.getArea(ctx); if (sum >= MAX_AREA) return MAX_AREA; } return sum; }
public static double area(ArrayList<Shape> shapes){ double sum=0; for(Shape shape: shapes){ sum += shape.getArea(); } return sum; }
@Override public double getArea(SpatialContext ctx) { return s.getArea(ctx); }
public void method(Shape shape) { int area = shape.getArea(); }
Shape shape = new Circle(scan.nextDouble()); // Use variable of type shape here .. // this code is the same for all shapes double boundaryLength = shape.getBoundaryLength(length); double area = shape.getArea(length); System.out.println("Boundary Length = " + Math.round(boundaryLength)); System.out.println("Area = " + Math.round(area));
@Override public double doubleValue() throws IOException { return shapeValues.value().getArea(geoArea ? ctx : null) * multiplier; }
Shape c = new Circle(50); System.out.println(c.getArea());
System.println ("rectangle's area=" + rectangle.getArea ()); ...
if (option==CLOSE_OPTION) end(); Shape shape = getShape(option); if (shape!=null) { double boundaryLength = shape.getBoundaryLength(); double area = shape.getArea(); System.out.println("Boundary Length = " + Math.round(boundaryLength)); System.out.println("Area = " + Math.round(area)); } ... private Shape getShape(int option){ switch(option) { case 1: return new Circle(scan.nextDouble()); case 2: return new Square(scan.nextDouble()); case 3: return new Triangle(scan.nextDouble()); default: System.out.println("Invalid option"); System.out.println("--------------\n"); return null; } }
Shape s = new Rectangle(); // Works fine Rectangle r = (Rectangle)s; // Works fine Rectangle r2 = s; // Needs typecasting, Compile fail Rectangle[] rar = new Rectangle[1]; // Fine, array of length 1 s.setS1(3.0); // Shape doesn't know about setS1(), compile fail s.computeArea(); // Works fine, though abstract method, known that an solid class has to implement rar[0].computeArea(); // Run time NullPointerException, rar[0] not initialized r.s1 = 4.5; // s1 is private, compile error r.setS1(5.0); // works fine r.setS2(3.0); // works fine s.getArea(); // works fine System.out.println(r.computeArea()); // Can't print void method, compile error r = null; // Works fine rar[1] = new Rectangle(); // Runtime ArrayIndexOutOfBoundsException, your array size is 1 System.out.println(Rectangle.name); // Works fine