Commit 4545798a authored by Boris Jaduš's avatar Boris Jaduš
Browse files

Translate iteration-03 README.md

parent 93483c51
## Třetí iterace
Cvičení zaměřené na přetěžování vlastních konstruktorů a metod.
1. Ve třídě `Vertex2D`:
* Udělejte ze třídy `Vertex2D` neměnnou (_immutable_) třídu, tj. odstraňte settery a nastavte všechny atributy jako `final`.
* Přidejte metodu `double distance(Vertex2D vertex)`, která vezme jiný 2D bod jako vstupní parametr a vrátí jeho
eukleidovskou vzdálenost. Vzdálenost bodů se vypočítá jako:
![vzorec](images/03a.png)
* Pokud je vstupní argument `null`, pak metoda vrátí hodnotu `-1.0` jako inditor chyby (vzdálenost je vždy
nezáporná).
2. Vytvořte třídu `Circle`.
* Třída bude mít konstruktor se dvěma parametry (v tomto pořadí): _střed_ (center) typu `Vertex2D`
a _poloměr_ (radius) typu `double`.
Atributy budou neměnné.
* Třída bude mít _bezparametrický konstruktor_, který vytvoří jednotkovou kružnici se středem v počátku
souřadného systému (tj. střed `[0.0, 0.0]`, poloměr `1.0`).
* **Bezparametrický konstruktor bude volat parametrický konstruktor** a předá mu potřebné hodnoty.
* Pro poloměr a střed vytvořte gettery `getRadius()` a `getCenter()`.
* Metoda `toString` bude vracet řetězec ve formátu:
## Third iteration
The exercise focused on overloading custom constructors and methods.
1. In the class `Vertex2D`:
* Make the `Vertex2D` class an _immutable_ class, ie remove the setters and set all attributes as `final`.
* Add the `double distance (Vertex2D vertex)` method, which takes another 2D point as an input parameter and returns its
Euclidean distance. The distance of the points is calculated as:
![formula](images/03a.png)
* If the input argument is `null`, then the method returns` -1.0` as an error indicator (distance is always non-negative).
2. Create a class `Circle`.
* The class will have a constructor with two parameters (in this order): _center_ of type `Vertex2D`
and _radius_ of type `double`.
Attributes will be immutable.
* The class will have a _parametric-free constructor_ that will create a unit circle with the center at the beginning of the
coordinate system (ie center `[0.0, 0.0]`, radius `1.0`).
* **The parametric-free constructor will call the parametric constructor** and will pass the needed values to it.
* For radius and center, create the getters `getRadius()` and `getCenter()`.
* The `toString` method will return a string in the format:
"Circle: center=[<x>, <y>], radius=<radius>"
kde `<x>` a `<y>` jsou hodnoty příslušných souřadnic středu a `<radius>` je hodnota poloměru.
where `<x>` and `<y>` are the values of the respective center coordinates and `<radius>` is the radius value.
3. Upravte třídu `Triangle` následujícím způsobem:
* Odstraňte setter, nastavte atributy jako `final`.
Třída nemůže být neměnná, protože metoda `divide` mění vlastnosti trojúhelníka.
* Přidejte metodu `boolean isEquilateral()`, která vrátí `true`, jestliže je trojúhelník rovnostranný.
Protože pracujeme s reálnými čísly, nelze jednoduše porovnávat délky stran pomocí `d1 == d2`.
Je nutné použít test, který bude považovat dvě reálná čísla za shodná, pokud se liší jen málo:
3. Edit the `Triangle` class as follows:
* Remove the setter, set the attributes to `final`.
A class cannot be immutable because the `divide` method changes the properties of a triangle.
* Add a `boolean isEquilateral()` method that returns `true` if the triangle is equilateral.
Because we work with real numbers, we cannot compare side lengths with `d1 == d2`.
It is necessary to use a test that will consider two real numbers to be identical if they differ only slightly:
Math.abs(d1-d2) < 0.001
kde `0.001` je tolerovaná absolutní odchylka a **bude definována jako privátní konstanta**.
* Vytvořte přetíženou metodu `void divide(int depth)`, která rozdělí trojúhelník na podtrojúhelníky.
Výsledkem bude [_Sierpińského trojúhelník_](http://en.wikipedia.org/wiki/Sierpinski_triangle):
![Sierpińského trojúhelník](images/03b.png)
*Sierpińského trojúhelníky hloubky 0 až 4.*
* Parametr `depth` udává hloubku dělení. Nula značí žádné dělení (jsme na konci rekurze), 1 znamená,
že dojde k jednomu rozdělení původního trojúhelníka, atd.
* Pokud je `depth` menší nebo rovna nule, k dělení nedojde a metoda skončí.
* Jinak metoda rozdělí trojúhelník pomocí metody `divide()` a rekurzivně se pokusí rozdělit vzniklé podtrojúhelníky
až do požadované hloubky.
* Vytvořte konstruktor se 4 parametry, čtvrtý parametr reprezentuje hloubku zanoření.
Konstruktor zavolá předešlý konstruktor a pak rozdělí trojúhelník.
4. Po spuštění třídy `Draw` se na obrazovce [vykreslí _Sierpińského trojúhelníky_ hloubky 4 a červená
kružnice](https://gitlab.fi.muni.cz/pb162/pb162-course-info/wikis/draw-images).
### Hinty
- Metody pro matematické operátory jsou ve třídě `Math`.
Např. odmocnina se vypočítá pomocí statické metody `Math.sqrt()`.
- Volání konstruktoru v konstruktoru se provádí klíčovým slovem `this`.
- Není potřeba volat `toString()`, metoda se zavolá automaticky.
- Konstanta musí být pouze jedna (`static`) a neměnná (`final`).
where `0.001` is the tolerated absolute deviation and **will be defined as the private constant**.
* Create an overloaded `void divide(int depth)` method that divides a triangle into sub-triangles.
The result will be [_Sierpiński triangle_](http://en.wikipedia.org/wiki/Sierpinski_triangle):
![Sierpiński triangle](images/03b.png)
*Sierpiński triangles of depth 0 to 4.*
* The `depth` parameter specifies the depth of the division. Zero means no division (we are at the end of recursion), 1 means
that there will be one division of the original triangle, etc.
* If the `depth` is less than or equal to zero, the division will not occur and the method will end.
* Otherwise, the method splits the triangle using the `divide ()` method and recursively tries to split the resulting sub-triangles
until reaching the requested depth.
* Create a constructor with 4 parameters, the fourth parameter represents the division depth.
The constructor calls the previous constructor and then splits the triangle.
4. After starting the `Draw` class, [_Sierpiński triangles_ of depth 4 and a red circle will be drawn on the screen](https://gitlab.fi.muni.cz/pb162/pb162-course-info/wikis/draw-images).
### Hints
- Methods for mathematical operators are in the `Math` class.
E.g. the square root is calculated using the static method `Math.sqrt()`.
- The constructor inside the constructor is called using `this` keyword.
- No need to call `toString()`, the method is called automatically.
- Constant must be only one (`static`) and immutable (`final`).
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment