ITI0011-2015:harjutus 08
Kirjeldus
Luua klassid, millega saab defineerida erinevaid kujundi-objekte. Kõik nimetatud klassid peavad kas otse või läbi mõne teise klassi pärinema Shape klassist (vt allpool malli). Klassid, mis tuleks kirjeldada:
- Triangle (kolmnurk). Kolmnurga objektil on konstruktor, kuhu saab ette anda kolm täisarvu: kolme külje pikkused.
- Ellipse (ellips). Ellipsil objektil on konstruktor, kuhu saab ette anda kaks täisarvu: lühim kaugus keskpunktist jooneni ja pikim kaugus keskpunktist jooneni.
- Rectangle (ristkülik). Ristkülikul on konstruktor, kuhu saab ette anda kahe kõrvuti oleva külje pikkused (a ja b).
- Circle (ring). See pärineb Ellipse klassist. Kirjeldab ära konstruktori, mis võtab vastu ühe täisarvu - raadiuse (kaugus keskpunktist jooneni).
- Square (ruut). Pärineb Rectangle klassist. Kirjeldab ära konstruktori, mis võtab vastu ühe täisarvu - ruutu külje pikkuse.
Kuna Shape klassis on kirjeldatud ära kaks abstraktset meetodit, peavad kõik Shape klassi laiendavad klassid ära kirjeldama nende meetodite sisu. Circle ja Square võiksid ära kasutada oma vahetu ülemklassi funktsionaalsust. Nendes klassides tuleb eraldi konstruktor luua, kuna see on erinev ülemklassi omast.
Allpool on toodud mall, mida peaks kasutama ülesande teksti juurde.
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. *
s * Note that abstract method definition doesn't * have body (no {} part). * @return Area of the shape */ public abstract double getArea(); /** * Calculates the perimeter of the shape. * For circular shapes, perimeter is also called * circumference. * @return Perimeter of the shape */ public abstract double 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 double getCircularShapesTotalArea(List<Shape> shapes) { // TODO: implement return Double.NaN; } /** * 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 double getNonCircularShapesTotalPerimeter(List<Shape> shapes) { // TODO: implement return Double.NaN; } /** * 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 double countUniqueEllipses(List<Shape> shapes) { // TODO: implement return Double.NaN; } /** * 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 double countDifferentRectangles(List<Shape> shapes) { // TODO: implement return Double.NaN; } } </source>