Skip to content
Open
21 changes: 21 additions & 0 deletions task01/src/com/example/task01/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ public class Point {
int x;
int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

void flip() {
int temp = x;
x = y*(-1);
y = temp*(-1);
}

double distance(Point point) {
int a = x - point.x;
int b = y - point.y;
return Math.sqrt(a*a + b*b);
}

public String toString() {
return "(" + x + ", " + y + ")";
}

void print() {
String pointToString = String.format("(%d, %d)", x, y);
System.out.println(pointToString);
Expand Down
8 changes: 2 additions & 6 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

public class Task01Main {
public static void main(String[] args) {
Point p1 = new Point();
p1.x = 10;
p1.y = 45;
Point p2 = new Point();
p2.x = 78;
p2.y = 12;
Point p1 = new Point(10, 45);
Point p2 = new Point(78, 12);

System.out.println("Point 1:");
p1.print();
Expand Down
3 changes: 2 additions & 1 deletion task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public class Task02Main {
public static void main(String[] args) {

System.out.println(Math.floorDiv(25, 60));
System.out.println(Math.floorMod(25, 60));
}
}
69 changes: 69 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.example.task02;

public class TimeSpan {
private int hour;
private int minute;
private int second;

public TimeSpan(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
correct();
}

public void correct() {
minute += Math.floorDiv(second, 60);
second = Math.floorMod(second, 60);

hour += Math.floorDiv(minute, 60);
minute = Math.floorMod(minute, 60);
}


public int getHour() {
return hour;
}

public void setHour(int hour) {
this.hour = hour;
correct();
}

public int getMinute() {
return minute;
}

public void setMinute(int minute) {
this.minute = minute;
correct();
}

public int getSecond() {
return second;
}

public void setSecond(int second) {
this.second = second;
correct();
}

void add(TimeSpan time) {
hour += time.hour;
minute += time.minute;
second += time.second;
correct();
}

void subtract(TimeSpan time) {
hour -= time.hour;
minute -= time.minute;
second -= time.second;
correct();
}

public String toString() {
return hour + ":" + minute + ":" + second;
}
}

25 changes: 25 additions & 0 deletions task03/src/com/example/task03/ComplexNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.task03;

public class ComplexNumber {
private final double real;
private final double imaginary;

public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}

public ComplexNumber add(ComplexNumber addend) {
return new ComplexNumber(real + addend.real, imaginary + addend.imaginary);
}

public ComplexNumber multiply(ComplexNumber multiplicand) {
double real = this.real * multiplicand.real - this.imaginary * multiplicand.imaginary;
double imaginary = this.imaginary * multiplicand.imaginary + this.real * multiplicand.real;
return new ComplexNumber(real, imaginary);
}

public String toString() {
return String.format("%.2f + %.2fi", real, imaginary);
}
}
9 changes: 8 additions & 1 deletion task03/src/com/example/task03/Task03Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

public class Task03Main {
public static void main(String[] args) {
ComplexNumber c1 = new ComplexNumber(3, 4);
ComplexNumber c2 = new ComplexNumber(1, -2);

ComplexNumber sum = c1.add(c2);
ComplexNumber product = c1.multiply(c2);

System.out.println("Сумма: " + sum);
System.out.println("Произведение: " + product);
}
}
}
33 changes: 33 additions & 0 deletions task04/src/com/example/task04/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.task04;

public class Line {
private Point p1;
private Point p2;

public Line(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}

public Point getP1() {
return p1;
}

public Point getP2() {
return p2;
}

public String toString() {
return "Line [p1=" + p1 + ", p2=" + p2 + "]";
}

public boolean isCollinearLine(Point p) {
double area = 0.5 * Math.abs(
p1.x * (p2.y - p.y) +
p2.x * (p.y - p1.y) +
p.x * (p1.y - p2.y)
);

return area == 0;
}
}
29 changes: 29 additions & 0 deletions task04/src/com/example/task04/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.task04;

/**
* Класс точки на плоскости
*/
public class Point {
final int x;
final int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

double distance(Point point) {
int a = x - point.x;
int b = y - point.y;
return Math.sqrt(a*a + b*b);
}

public String toString() {
return "(" + x + ", " + y + ")";
}

void print() {
String pointToString = String.format("(%d, %d)", x, y);
System.out.println(pointToString);
}
}
10 changes: 9 additions & 1 deletion task04/src/com/example/task04/Task04Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

public class Task04Main {
public static void main(String[] args) {

Line line1 = new Line(new Point(0,0), new Point(5,5));
Point[] points = new Point[7];
for (int i = 0; i < 6; i++) {
points[i] = new Point(i,i);
}
points[6] = new Point(1,2);
for (Point point: points) {
System.out.println(line1+" | isCollinearLine("+point+"): "+line1.isCollinearLine(point));
}
}
}
15 changes: 8 additions & 7 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ public class Point {
* @param x координата по оси абсцисс
* @param y координата по оси ординат
*/
private final double x;
private final double y;

public Point(double x, double y) {
throw new AssertionError();
this.x = x;
this.y = y;
}

/**
Expand All @@ -21,8 +25,7 @@ public Point(double x, double y) {
* @return координату точки по оси X
*/
public double getX() {
// TODO: реализовать
throw new AssertionError();
return x;
}

/**
Expand All @@ -31,8 +34,7 @@ public double getX() {
* @return координату точки по оси Y
*/
public double getY() {
// TODO: реализовать
throw new AssertionError();
return y;
}

/**
Expand All @@ -42,8 +44,7 @@ public double getY() {
* @return расстояние от текущей точки до переданной
*/
public double getLength(Point point) {
// TODO: реализовать
throw new AssertionError();
return Math.pow(Math.pow(point.x - x, 2) + Math.pow(point.y -y, 2), 0.5);
}

}
54 changes: 49 additions & 5 deletions task05/src/com/example/task05/PolygonalLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,26 @@ public class PolygonalLine {
*
* @param points массив точек, которыми нужно проинициализировать ломаную линию
*/
PolygonalLinePart firstLinePart;
PolygonalLinePart lastLinePart;
private Point tempPoint;

public void setPoints(Point[] points) {
// TODO: реализовать
for (int i = 0; i < points.length; i++){
if (i == points.length - 1)
continue;
Point firstPoint = new Point(points[i].getX(), points[i].getY());
Point secondPoint = new Point(points[i+1].getX(), points[i+1].getY());

PolygonalLinePart iterationLinePart = new PolygonalLinePart(firstPoint, secondPoint);
if (firstLinePart == null){
firstLinePart = iterationLinePart;
lastLinePart = iterationLinePart;
continue;
}
lastLinePart.linkToNextLine(iterationLinePart);
lastLinePart = iterationLinePart;
}
}

/**
Expand All @@ -20,7 +38,27 @@ public void setPoints(Point[] points) {
* @param point точка, которую нужно добавить к ломаной
*/
public void addPoint(Point point) {
// TODO: реализовать
point = new Point(point.getX(), point.getY());

Point firstPoint;
if (lastLinePart != null)
firstPoint = lastLinePart.getP2();
else if (tempPoint == null) {
tempPoint = point;
return;
} else {
firstPoint = tempPoint;
}

PolygonalLinePart newLinePart = new PolygonalLinePart(firstPoint, point);
if (firstLinePart == null) {
firstLinePart = newLinePart;
lastLinePart = newLinePart;
}
else {
lastLinePart.linkToNextLine(newLinePart);
lastLinePart = newLinePart;
}
}

/**
Expand All @@ -30,7 +68,8 @@ public void addPoint(Point point) {
* @param y координата по оси ординат
*/
public void addPoint(double x, double y) {
// TODO: реализовать
Point point = new Point(x, y);
addPoint(point);
}

/**
Expand All @@ -39,8 +78,13 @@ public void addPoint(double x, double y) {
* @return длину ломаной линии
*/
public double getLength() {
// TODO: реализовать
throw new AssertionError();
double result = 0;
PolygonalLinePart tempLine = firstLinePart;
while (tempLine != null) {
result += tempLine.getP1().getLength(tempLine.getP2());
tempLine = tempLine.nextLinePart;
}
return result;
}

}
Loading