Commit fa8b20df authored by Vladimír Ulman's avatar Vladimír Ulman
Browse files

Merge branch 'iteration-01' into 'master'

Iteration 01

See merge request !1
parents 1274f2ed 04ee37c4
Pipeline #70673 passed with stage
in 10 seconds
PB162 Java ## First iteration
=============
At the left upper part you can see the list of branches with the assignments. Iteration for getting acquainted with objects and encapsulation.
You can find all necessary information [here](https://gitlab.fi.muni.cz/pb162/pb162-course-info/wikis/home). 1. Create a `Vertex2D` class representing a point in 2D space with *X* and *Y* coordinates.
* The class will be in the package `cz.muni.fi.pb162.project.geometry` (you must create the appropriate package).
* The class will have two attributes of type `double`, in which it will store the values of the coordinates *X* and *Y*.
Their default value will be 0.
> Never use `float` type. this type is obsolete.
**Private information for teachers is [here](https://gitlab.fi.muni.cz/pb162/pb162-teachers-info/wikis/home)**. > Attributes should have accurate descriptive names because they can appear anywhere in the class. Therefore, single-letter names are not recommended. However, the letters *X* and *Y* for coordinates are very common, so we can accept these single-letter attributes in this special case.
# Current branch `master` * **Do not** create constructors yet. We will focus on constructors on the next iteration.
* Add the so-called _getters_ and _setters_ to the class.
> Choose appropriate names that reflect the names of the attributes you have selected!
This branch consist of working _Hello world_ project with the passing tests. Try it! * Method `String getInfo()` returns a formatted coordinate description according to the following example:
\ No newline at end of file For a point at the coordinates x=2.0, y=3.0 returns **10 characters** (space included):
`[2.0, 3.0]`.
* Method `double sumCoordinates()` returns the sum of the coordinates *X* and *Y*.
* Method `void move(Vertex2D vertex)` takes another 2D point as the input parameter and shifts the vertex by the `vertex` coordinates. For example, calling the `move` mwthod on vertex with coordinates `[2, 3]` and input parameter `[1, 1]` shifts the vertex `[2, 3]` into `[3, 4]`.
* Adjust the visibility of attributes and methods to meet encapsulation conditions.
2. Edit the executable class `Demo`.
* Leave the class in the package `cz.muni.fi.pb162.project`.
* Fill in the author's name (`@author`).
* Remove the print of `Hello world!`.
* The class creates 2 vertices with coordinates `[2, 3]` and `[1, 1]`.
> Hint: The `Vertex2D` class must be **imported** into the `Demo` class because it is located in another package.
* Then moves the first vertex by the coordinates of the second vertex.
* Then prints information about both vertices.
* The program prints to standard output:
~~~~
[3.0, 4.0]
[1.0, 1.0]
~~~~
3. Document both classes using [_JavaDoc_](https://en.wikipedia.org/wiki/Javadoc).
> At least the author, the purpose of public methods, their input parameters, and return value have documented.
> The presence of mandatory javadoc items is checked by the *checkstyle* plugin. However, the content is up to you. Be consise but thorough.
> For getters and setters, javadoc does not have to be written, because their purpose and use are obvious.
4. Test your code using unit tests in the package **src/test/java**. After successful testing, submit to a homework vault or git and have it checked by the tutor. Submitted iteration must pass *tests* and *checkstyle*!
> Unit tests and style checking are always evaluated whenever you rebuild the project in IDE.
> If unsure, run `mvn clean install` on the command line.
> Tests are also invoked when you submit (push) your code into git. If they fail, then you get an email. Failures during the development and continuous submission are OK. Failures after the final submission are not OK ;-)
> Code of unit tests is weird and messy. To implement test cases, it is oftten necessary to violate encapsulation and other principles of production code. Therefore, don't take the inspiration from this code (it is better to not look inside at all ;-)
package cz.muni.fi.pb162.project; package cz.muni.fi.pb162.project;
import cz.muni.fi.pb162.project.geometry.Vertex2D;
/** /**
* Class for running main method. * Class for running main method.
* *
* @author TODO: put your name here * @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/ */
public class Demo { public class Demo {
/** /**
* Runs the code. * prints info for two vertices of fixed coordinates.
* *
* @param args command line arguments, will be ignored * @param args command line arguments, will be ignored
*/ */
public static void main(String[] args) { public static void main(String[] args) {
Vertex2D v1 = newVertex(2, 3);
Vertex2D v2 = newVertex(1, 1);
v1.move(v2);
System.out.println("Hello world!"); System.out.println(v1.getInfo());
System.out.println(v2.getInfo());
}
/**
* Creates new instance of Vertex2D.
*
* @param x coordinate for new vector
* @param y coordinate for new vector
* @return new Vertex2D.
*/
private static Vertex2D newVertex(double x, double y) {
Vertex2D vertex = new Vertex2D();
vertex.setX(x);
vertex.setY(y);
return vertex;
} }
} }
package cz.muni.fi.pb162.project.geometry;
/**
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
public class Vertex2D {
private double x = 0;
private double y = 0;
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
/**
* To get string representation.
*
* @return string representation.
*/
public String getInfo() {
return String.format("[%.1f, %.1f]", x, y);
}
/**
* To get sum of coordinates.
*
* @return sum of coordinates.
*/
public double sumCoordinates() {
return x + y;
}
/**
* Moves the vertex.
*
* @param vertex is added to this vertex.
*/
public void move(Vertex2D vertex) {
x += vertex.x;
y += vertex.y;
}
}
...@@ -13,7 +13,8 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -13,7 +13,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class DemoTest { public class DemoTest {
private static final String EXPECTED_OUTPUT = "Hello world!" + System.lineSeparator(); private static final String EXPECTED_OUTPUT = "[3.0, 4.0]" + System.lineSeparator()
+ "[1.0, 1.0]" + System.lineSeparator();
@Test @Test
public void testMainOutput() { public void testMainOutput() {
......
package cz.muni.fi.pb162.project.geometry;
import cz.muni.fi.pb162.project.helper.BasicRulesTester;
import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.within;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
/**
* Simple Vertex2D tests.
* @author Marek Sabo
*/
public class Vertex2DTest {
private Vertex2D vertex2D;
private static final double X = -1.2;
private static final double Y = 2.4;
private static final double DELTA = 0.001;
@Before
public void setUp() {
vertex2D = createVertex(X, Y);
}
private Vertex2D createVertex(double x, double y) {
Vertex2D v = new Vertex2D();
v.setX(x);
v.setY(y);
return v;
}
@Test
public void attributesAndMethodsAmount() {
BasicRulesTester.attributesAmount(Vertex2D.class, 2);
BasicRulesTester.methodsAmount(Vertex2D.class, 7);
}
@Test
public void gettersAndSetters() {
assertThat(vertex2D.getX()).isEqualTo(X);
assertThat(vertex2D.getY()).isEqualTo(Y);
}
@Test
public void attributesAre0() {
Vertex2D v = new Vertex2D();
assertVertex(v, 0.0, 0.0);
}
@Test
public void getInfo() {
assertThat(vertex2D.getInfo()).isEqualTo("[" + X + ", " + Y + "]");
}
@Test
public void sumCoordinates() {
assertThat(vertex2D.sumCoordinates()).isCloseTo(X + Y, within(DELTA));
}
@Test
public void moveVertex() {
final double XX = -3.3;
final double YY = -5.5;
Vertex2D negativeVertex = createVertex(XX, YY);
vertex2D.move(negativeVertex);
assertVertex(vertex2D, XX + X, YY + Y);
assertVertex(negativeVertex, XX, YY);
}
private void assertVertex(Vertex2D v, double x, double y) {
assertThat(v.getX()).isCloseTo(x, within(DELTA));
assertThat(v.getY()).isCloseTo(y, within(DELTA));
}
}
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