Erinevus lehekülje "ITI0011-2015:harjutus 08" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
34. rida: 34. rida:
 
* which actually does the calculations.
 
* which actually does the calculations.
 
* <p>
 
* <p>
* The result has to be an integer number.
+
s * Note that abstract method definition doesn't
* Do not use rounding, just cast to int
 
* (which rounds down).
 
* <p>
 
* Note that abstract method definition doesn't
 
 
* have body (no {} part).
 
* have body (no {} part).
* @return Area of the shape cast to int
+
* @return Area of the shape
 
*/
 
*/
public abstract int getArea();
+
public abstract double getArea();
 
 
 
/**
 
/**
48. rida: 44. rida:
 
* For circular shapes, perimeter is also called
 
* For circular shapes, perimeter is also called
 
* circumference.
 
* circumference.
* <p>
+
* @return Perimeter of the shape
* The result has to be an integer using casting not rounding.
 
* @return Perimeter of the shape cast to int
 
 
*/
 
*/
public abstract int getPerimeter();
+
public abstract double getPerimeter();
 
}
 
}
  
59. rida: 53. rida:
 
EX08.java:
 
EX08.java:
 
<source lang="java">
 
<source lang="java">
 +
  
 
import java.util.ArrayList;
 
import java.util.ArrayList;
100. rida: 95. rida:
 
* @return Total area of circular shapes
 
* @return Total area of circular shapes
 
*/
 
*/
public static int getCircularShapesTotalArea(List<Shape> shapes) {
+
public static double getCircularShapesTotalArea(List<Shape> shapes) {
 
// TODO: implement
 
// TODO: implement
return -1;
+
return Double.NaN;
 
}
 
}
 
 
111. rida: 106. rida:
 
* @return Total perimeter of non-circular shapes.
 
* @return Total perimeter of non-circular shapes.
 
*/
 
*/
public static int getNonCircularShapesTotalPerimeter(List<Shape> shapes) {
+
public static double getNonCircularShapesTotalPerimeter(List<Shape> shapes) {
 
// TODO: implement
 
// TODO: implement
return -1;
+
return Double.NaN;
 
}
 
}
 
/**
 
/**
122. rida: 117. rida:
 
* @return Count of unique ellipses (hoe many ellipses are in the memory).
 
* @return Count of unique ellipses (hoe many ellipses are in the memory).
 
*/
 
*/
public static int countUniqueEllipses(List<Shape> shapes) {
+
public static double countUniqueEllipses(List<Shape> shapes) {
 
// TODO: implement
 
// TODO: implement
return -1;
+
return Double.NaN;
 
}
 
}
 
 
134. rida: 129. rida:
 
* @return Count of different rectangless.
 
* @return Count of different rectangless.
 
*/
 
*/
public static int countDifferentRectangles(List<Shape> shapes) {
+
public static double countDifferentRectangles(List<Shape> shapes) {
return -1;
+
// TODO: implement
 +
return Double.NaN;
 
}
 
}
 
}
 
}
  
 
</source>
 
</source>

Redaktsioon: 13. märts 2015, kell 13:09

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>