Commit ae61165b authored by Radek Ošlejšek's avatar Radek Ošlejšek
Browse files

Adjusted assignment

parent 07210f8d
......@@ -3,19 +3,19 @@
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:
* Make the `Vertex2D` class _immutable_, i.e., remove the setters and set all attributes as `final`.
* Add the `distance` method, which takes another 2D point as an input parameter and returns its
Euclidean distance as `double`. 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).
>> Methods for mathematical operators are in the `Math` class.
>> For instance, the square root is calculated using the static method `Math.sqrt()`.
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`.
2. Create a class `Circle` in the package `cz.muni.fi.pb162.project.geometry`.
* 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 class will have a _parametric-free constructor_ that will create a unit circle with the center at the beginning of the coordinate system (i.e., center `[0.0, 0.0]` and 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:
......@@ -26,32 +26,19 @@ The exercise focused on overloading custom constructors and methods.
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:
The class cannot be immutable because the `divide` method changes the properties of a triangle (the state of a triangle object).
* Add a `boolean isEquilateral()` method that returns `true` if the triangle is equilateral. Because we work with real numbers computed by math expressions, it is not practical to compare side lengths with `d1 == d2`. Even e very small difference makes the lengths different. Therefore, it is more suitable to use a test that considers two real numbers to be identical if they differ only slightly:
Math.abs(d1-d2) < 0.001
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):
* 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.
* 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.
* 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