Back to all lessons


Lesson 10

Structured programming

class Point2D {
    double x;
    double y;
}
public class Main {
    public static void main(String[] args) {
        Point2D p = new Point2D();
        p.x = 10;
        p.y = 20;
        System.out.println("[" + p.x + ", " + p.y + "]");
    }
}
Task 0

Create a new package called "geometry". Create two classess: Path2D and Point2D.

Task 1

Point2D should have 2 doubles: x and y. Create a static method, that takes two Point2Ds as argument and returns a distance between them.

Task 2

Create a static method returning a new random Point2D.

Task 3

Path2D should store array of Point2D. Create a method, that creates new Path2D. It should take length of path as a argument. Don't put values into array.

Task 4

Create a static method returning a new random Path2D.

Task 5

Create a method, that adds new point to path. Look up for empty slot in array (value==null).

Task 6

Create a static method, that prints all points located in Path2D.

Task 7

Run following scenario in main method, check if it is working properly.


public class Main {
    public static void main(String[] args) {
        Path2D path = Path2D.createPath();
        Path2D.addPointTo(path);
        Path2D.addPointTo(path);
        Path2D.addPointTo(path);
        Path2D.print(path);
    }
}
Task 8

Create a static method, that sorts points in path. Points should keep minimal distance between them.

Task 9

Add a static method, that translates each point in path by x and y parameters.

Task 10

Add a static method, that allows to scale a path (multiply x and y).

Task 11

Create a static method, that splits path into two. It should return 2-elements array of containing two paths.

Task 12

Create a static method, that rotates a point by angle. Hint: https://academo.org/demos/rotation-about-point/

Task 13

Add a variable in Point2D called: index. Modify all methods to keep index of a point in an array inside the point.

Task 14

Write a scenario in main, that contains two Path2D. Create a new Point2D and put it into two paths. Modify the point using Path2D code (i.e. translate path). Check of values of point in paths. What happend? Why?