ITI0011-2015:harjutus 08
Mall
Shape.java <source lang="java">
/**
* General abstract class Shape. All the different * shapes in this exercise have to extend it directly * or through another class.
*
* All the abstract methods have to implemented by the class * which extends this class (and is not abstract itself). */ public abstract class Shape { /** * Calculates the total area for the shape. * As it is an abstract method, every class * which extends Shape has to implement this method, * e.g. override this method and write the body * which actually does the calculations. *
* The result has to be an integer number. * Do not use rounding, just cast to int * (which rounds down). *
* Note that abstract method definition doesn't * have body (no {} part). * @return Area of the shape cast to int */ public abstract int getArea(); /** * Calculates the perimeter of the shape. * For circular shapes, perimeter is also called * circumference. *
* The result has to be an integer using casting not rounding. * @return Perimeter of the shape cast to int */ public abstract int getPerimeter(); } </source> EX08.java: <source lang="java"> import java.util.ArrayList; import java.util.HashSet; import java.util.List; /** * The main class where some static methods * which operate on shapes list are declared. */ public class EX08 { public static void main(String[] args) { // Shape s = new Shape(); // should not be allowed Circle c = new Circle(10); Ellipse e = new Ellipse(10, 5); Rectangle r = new Rectangle(10, 12); Rectangle r2 = new Rectangle(10, 12); Square s = new Square(5); Triangle t = new Triangle(1, 2, 5); List<Shape> shapes = new ArrayList<Shape>(); //shapes.add(c); shapes.add(e); shapes.add(r); shapes.add(r); shapes.add(r2); shapes.add(s); shapes.add(t); System.out.println(getCircularShapesTotalArea(shapes)); System.out.println(getNonCircularShapesTotalPerimeter(shapes)); System.out.println(countUniqueEllipses(shapes)); System.out.println(countDifferentRectangles(shapes)); } /** * Given a list of shapes, sum up the area for all the circular shapes. * @param shapes A list of shapes. * @return Total area of circular shapes */ public static int getCircularShapesTotalArea(List<Shape> shapes) { // TODO: implement return -1; } /** * Given a list of shapes, sum up the perimeter for all the * non-circular shapes. * @param shapes A list of shapes. * @return Total perimeter of non-circular shapes. */ public static int getNonCircularShapesTotalPerimeter(List<Shape> shapes) { // TODO: implement return -1; } /** * Count unique ellipses in the given list of shapes. * Every different instance of Ellipse is a unique ellipse. * The same object instance in the list is considered as a duplicate. * @param shapes A list of shapes * @return Count of unique ellipses (hoe many ellipses are in the memory). */ public static int countUniqueEllipses(List<Shape> shapes) { // TODO: implement return -1; } /** * Count different rectangles in the given list of shapes. * Two rectangles are different if at least one of the sides * (a or b) is not equal for both rectangles. * @param shapes A list of shapes. * @return Count of different rectangless. */ public static int countDifferentRectangles(List<Shape> shapes) { return -1; } } </source>