Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions task01/src/com/example/task01/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,34 @@ public class Point {
int x;
int y;

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

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

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

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

@Override
public String toString() {
return String.format("(%d, %d)", x, y);
}

void print() {
String pointToString = String.format("(%d, %d)", x, y);
System.out.println(pointToString);
System.out.println(toString());
}
}
}
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 hours;
private int minutes;
private int seconds;

public TimeSpan(int hours, int minutes, int seconds) {
setTime(hours, minutes, seconds);
}

private void setTime(int hours, int minutes, int seconds) {
long totalSeconds = hours * 3600L + minutes * 60L + seconds;

if (totalSeconds < 0) {
totalSeconds = 0;
}

this.hours = (int) (totalSeconds / 3600);
totalSeconds %= 3600;
this.minutes = (int) (totalSeconds / 60);
this.seconds = (int) (totalSeconds % 60);
}

public int getHours() {
return hours;
}

public void setHours(int hours) {
setTime(hours, this.minutes, this.seconds);
}

public int getMinutes() {
return minutes;
}

public void setMinutes(int minutes) {
setTime(this.hours, minutes, this.seconds);
}

public int getSeconds() {
return seconds;
}

public void setSeconds(int seconds) {
setTime(this.hours, this.minutes, seconds);
}

public void add(TimeSpan time) {
setTime(
this.hours + time.hours,
this.minutes + time.minutes,
this.seconds + time.seconds
);
}

public void subtract(TimeSpan time) {
long thisTotal = this.hours * 3600L + this.minutes * 60L + this.seconds;
long otherTotal = time.hours * 3600L + time.minutes * 60L + time.seconds;
long resultTotal = thisTotal - otherTotal;

setTime(0, 0, (int)resultTotal);
}

@Override
public String toString() {
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
}
33 changes: 33 additions & 0 deletions task03/src/com/example/task03/ComplexNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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 other) {
return new ComplexNumber(
this.real + other.real,
this.imaginary + other.imaginary
);
}

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

@Override
public String toString() {
if (imaginary >= 0) {
return String.format("%.2f + %.2fi", real, imaginary);
} else {
return String.format("%.2f - %.2fi", real, Math.abs(imaginary));
}
}
}
32 changes: 32 additions & 0 deletions task04/src/com/example/task04/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.task04;

public class Line {
private final Point p1;
private final 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 boolean isCollinearLine(Point p) {
int determinant = p1.getX() * (p2.getY() - p.getY()) +
p2.getX() * (p.getY() - p1.getY()) +
p.getX() * (p1.getY() - p2.getY());

return determinant == 0;
}

@Override
public String toString() {
return String.format("Line[%s, %s]", p1, p2);
}
}
24 changes: 24 additions & 0 deletions task04/src/com/example/task04/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.task04;

public class Point {
private final int x;
private final int y;

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

public int getX() {
return x;
}

public int getY() {
return y;
}

@Override
public String toString() {
return String.format("(%d, %d)", x, y);
}
}
11 changes: 10 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,15 @@

public class Task04Main {
public static void main(String[] args) {
Point p1 = new Point(0, 0);
Point p2 = new Point(4, 4);
Point p3 = new Point(2, 2);
Point p4 = new Point(1, 2);

Line line = new Line(p1, p2);

System.out.println("Line: " + line);
System.out.println("Point p3 " + p3 + " is collinear: " + line.isCollinearLine(p3));
System.out.println("Point p4 " + p4 + " is collinear: " + line.isCollinearLine(p4));
}
}
}
19 changes: 10 additions & 9 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Точка в двумерном пространстве
*/
public class Point {
private final double x;
private final double y;

/**
* Конструктор, инициализирующий координаты точки
Expand All @@ -12,7 +14,8 @@ public class Point {
* @param y координата по оси ординат
*/
public Point(double x, double y) {
throw new AssertionError();
this.x = x;
this.y = y;
}

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

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

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

}
}
25 changes: 18 additions & 7 deletions task05/src/com/example/task05/PolygonalLine.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.example.task05;

import java.util.ArrayList;
import java.util.List;

/**
* Ломаная линия
*/
public class PolygonalLine {
private List<Point> points = new ArrayList<>();

/**
* Устанавливает точки ломаной линии
*
* @param points массив точек, которыми нужно проинициализировать ломаную линию
*/
public void setPoints(Point[] points) {
// TODO: реализовать
this.points = new ArrayList<>();
for (Point point : points) {
// Создаем копию каждой точки
addPoint(point.getX(), point.getY());
}
}

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

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

/**
Expand All @@ -39,8 +48,10 @@ public void addPoint(double x, double y) {
* @return длину ломаной линии
*/
public double getLength() {
// TODO: реализовать
throw new AssertionError();
double length = 0;
for (int i = 0; i < points.size() - 1; i++) {
length += points.get(i).getLength(points.get(i + 1));
}
return length;
}

}
}
16 changes: 15 additions & 1 deletion task05/src/com/example/task05/Task05Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

public class Task05Main {
public static void main(String[] args) {
PolygonalLine line = new PolygonalLine();

line.addPoint(new Point(1, 2));
line.addPoint(4, 6);
line.addPoint(new Point(8, 9));

System.out.println("Длина ломаной: " + line.getLength());

Point[] points = {
new Point(0, 0),
new Point(3, 4),
new Point(6, 8)
};
line.setPoints(points);
System.out.println("Новая длина ломаной: " + line.getLength());
}
}
}