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
29 changes: 27 additions & 2 deletions task01/src/com/example/task01/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,33 @@
* Класс точки на плоскости
*/
public class Point {
int x;
int y;
public int x;
public int y;

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

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

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

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

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

void print() {
String pointToString = String.format("(%d, %d)", x, y);
Expand Down
12 changes: 6 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,12 @@

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(25, 8);

Point p2 = new Point(5, 7);

System.out.println(p1.distance(p2));


System.out.println("Point 1:");
p1.print();
Expand Down
5 changes: 4 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,9 @@

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

TimeSpan t1 = new TimeSpan(0, 50, 0);
TimeSpan t2 = new TimeSpan(0, 10, 0);
t1.add(t2);
System.out.println(t1.toString());
}
}
48 changes: 48 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.task02;

public class TimeSpan {
private int hours = 0;
private int minutes = 0;
private int seconds = 0;

public int getHours() { return hours; }

public void setHours(int hours) { this.hours = hours; }

public int getMinutes() { return minutes; }

public void setMinutes(int minutes) { this.minutes = minutes; }

public int getSeconds() { return seconds; }

public void setSeconds(int seconds) { this.seconds = seconds; }

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

private void setNormalizedTime(int hours, int minutes, int seconds) {
if (hours < 0 || minutes < 0 || seconds < 0)
throw new IllegalArgumentException("Время не может быть отрицательным!");

int totalSeconds = hours * 3600 + minutes * 60 + seconds;

this.hours = totalSeconds / 3600;
this.minutes = (totalSeconds % 3600) / 60;
this.seconds = totalSeconds % 60;
}
public void add(TimeSpan time) {
setNormalizedTime(hours + time.hours, minutes + time.minutes, seconds + time.seconds);
}

public void subtract(TimeSpan time) {
if ((time.hours * 3600 + time.minutes * 60 + time.seconds) > (hours * 3600 + minutes * 60 + seconds))
throw new IllegalArgumentException("Время не может быть отрицательным!");

setNormalizedTime(hours - time.hours, minutes - time.minutes, seconds - time.seconds);
}

public String toString() {
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
}
38 changes: 38 additions & 0 deletions task03/src/com/example/task03/Complex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.task03;

public class Complex {
final private double real;
final private double imag;

public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
};

public double getReal() {
return real;
};
public double getImag() {
return imag;
};

public Complex add(Complex other) {
return new Complex(this.real + other.real, this.imag + other.imag);
};

public Complex multiply(Complex other) {
double real = (this.real * other.real) - (this.imag * other.imag);
double imag = (this.real * other.imag) + (other.real * this.imag);

return new Complex(real, imag);
};

@Override
public String toString() {
String sign = "+";
if (imag < 0)
sign = "-";

return String.format("%.2f%s%.2fi", real, sign, Math.abs(imag));
};
}
4 changes: 3 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,8 @@

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

Complex z1 = new Complex(1, 3);
Complex z2 = new Complex(4, -5);
System.out.println(z1.add(z2).toString());
}
}
30 changes: 30 additions & 0 deletions task04/src/com/example/task04/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.task04;


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

public Point getP1() { return p1; }
public Point getP2() { return p2; }

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

public double length() {
return p1.distance(p2);
}

private double distance(Point p) {
return Math.abs((p2.getY() - p1.getY()) * p.getX() - (p2.getX() - p1.getX()) * p.getY() + p2.getX() * p1.getY() - p2.getY() * p1.getX()) / this.length();
}

public boolean isCollinearLine(Point p) { return distance(p) == 0; }

@Override
public String toString() {
return String.format("(%d, %d)-(%d, %d)", p1.getX(), p1.getY(), p2.getX(), p2.getY());
}
}
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 private int x;
final private int y;

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

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

public int getX() { return x; }
public int getY() { return y; }

public double distance(Point point) {
return Math.sqrt((point.x - this.x) * (point.x - this.x) + (point.y - this.y) * (point.y - this.y));
}

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

}
8 changes: 8 additions & 0 deletions 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) {
Point p1 = new Point(1, 2);
Point p2 = new Point(3, 4);
Point p3 = new Point(2, 3);

Line line = new Line(p1, p2);

System.out.println(line.isCollinearLine(p3));

//System.out.println(line.length());
}
}
23 changes: 12 additions & 11 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,33 @@
*/
public class Point {

final private double x;
final private double y;

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

/**
* Возвращает координату точки по оси абсцисс
*
* @return координату точки по оси X
*/
public double getX() {
// TODO: реализовать
throw new AssertionError();
}
public double getX() { return x; }

/**
* Возвращает координату точки по оси ординат
*
* @return координату точки по оси Y
*/
public double getY() {
// TODO: реализовать
throw new AssertionError();
}
public double getY() { return y; }

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

@Override
public String toString() {
return String.format("(%.2f, %.2f)", x ,y);
}
}
40 changes: 34 additions & 6 deletions task05/src/com/example/task05/PolygonalLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@
*/
public class PolygonalLine {

private Point[] points;

/**
* Устанавливает точки ломаной линии
*
* @param points массив точек, которыми нужно проинициализировать ломаную линию
*/
public void setPoints(Point[] points) {
// TODO: реализовать
if (points == null) { throw new NullPointerException("Массив точек не должен быть null"); }
if (points.length == 0) { throw new IllegalArgumentException("Массив точек не должен быть пустым!"); }

Point[] values = new Point[points.length];
for (int i = 0; i < points.length; i++) {
double x = points[i].getX();
double y = points[i].getY();
values[i] = new Point(x, y);
}

this.points = values;
}

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

Point[] pointsWithAddedPoint = new Point[points.length + 1];
for (int i = 0; i < points.length; i++) {
double x = points[i].getX();
double y = points[i].getY();
pointsWithAddedPoint[i] = new Point(x, y);
}
pointsWithAddedPoint[pointsWithAddedPoint.length - 1] = new Point(point.getX(), point.getY());
points = pointsWithAddedPoint;
}

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

/**
Expand All @@ -39,8 +63,12 @@ public void addPoint(double x, double y) {
* @return длину ломаной линии
*/
public double getLength() {
// TODO: реализовать
throw new AssertionError();
}
double length = 0.0;

for (int i = 1; i < points.length; i++) {
length += points[i].getLength(points[i-1]);
}

return length;
}
}
13 changes: 12 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,17 @@

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

Point[] points = {
new Point(1, 2),
new Point(4, 6),
new Point(8, 9),
new Point(12, 12),
new Point(15, 16),
};
PolygonalLine line = new PolygonalLine();
for (Point p : points) {
line.addPoint(p);
}
System.out.println(line.getLength());
}
}