From aeb3a7487431d29b1f116d9cb2b332448483c96f Mon Sep 17 00:00:00 2001 From: Maria Vasiljewa Date: Tue, 7 Oct 2025 05:45:41 +0500 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=B0=D1=81=D0=B8=D0=BB=D1=8C=D0=B5?= =?UTF-8?q?=D0=B2=D0=B0=20=D0=9C=D0=B0=D1=80=D0=B8=D1=8F=20=D0=9F=D1=80?= =?UTF-8?q?=D0=98-201.=20=D0=9C=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task01/src/com/example/task01/Point.java | 29 +++++++- task02/src/com/example/task02/Task02Main.java | 6 ++ task02/src/com/example/task02/TimeSpan.java | 73 +++++++++++++++++++ .../src/com/example/task03/ComplexNumber.java | 40 ++++++++++ task03/src/com/example/task03/Task03Main.java | 6 ++ task04/src/com/example/task04/Line.java | 29 ++++++++ task04/src/com/example/task04/Point.java | 30 ++++++++ task04/src/com/example/task04/Task04Main.java | 5 ++ task05/src/com/example/task05/Point.java | 17 +++-- .../src/com/example/task05/PolygonalLine.java | 20 +++-- task05/src/com/example/task05/Task05Main.java | 6 ++ 11 files changed, 248 insertions(+), 13 deletions(-) create mode 100644 task02/src/com/example/task02/TimeSpan.java create mode 100644 task03/src/com/example/task03/ComplexNumber.java create mode 100644 task04/src/com/example/task04/Line.java create mode 100644 task04/src/com/example/task04/Point.java diff --git a/task01/src/com/example/task01/Point.java b/task01/src/com/example/task01/Point.java index ec5c69e8..11a11de4 100644 --- a/task01/src/com/example/task01/Point.java +++ b/task01/src/com/example/task01/Point.java @@ -7,8 +7,35 @@ public class Point { int x; int y; + public Point(int x, int y) { + this.x = x; + this.y = y; + } + + public Point() { + x = 0; + y = 0; + } + + void flip(){ + int temp = x; + x = -y; + y = -temp; + } + double distance(Point point){ + double dX = Math.pow(x-point.x,2); + double dY = Math.pow(y-point.y,2); + return Math.sqrt(dX + dY); + + } + + public String toString(){ + return String.format("(%d, %d)", x, y); + } + + void print() { - String pointToString = String.format("(%d, %d)", x, y); + String pointToString = toString(); System.out.println(pointToString); } } diff --git a/task02/src/com/example/task02/Task02Main.java b/task02/src/com/example/task02/Task02Main.java index 644a0eba..5f74d5be 100644 --- a/task02/src/com/example/task02/Task02Main.java +++ b/task02/src/com/example/task02/Task02Main.java @@ -2,6 +2,12 @@ public class Task02Main { public static void main(String[] args) { + TimeSpan time1 = new TimeSpan(3,34,57); + TimeSpan time2 = new TimeSpan(0,30,15); + time1.add(time2); + System.out.println(time1.toString()); + time1.setSeconds(90); + System.out.println(time1.toString()); } } diff --git a/task02/src/com/example/task02/TimeSpan.java b/task02/src/com/example/task02/TimeSpan.java new file mode 100644 index 00000000..c0da1cc0 --- /dev/null +++ b/task02/src/com/example/task02/TimeSpan.java @@ -0,0 +1,73 @@ +package com.example.task02; + +public class TimeSpan { + private int hours; + private int minutes; + private int seconds; + + public TimeSpan(int hours, int minutes, int seconds) { + this.hours = hours; + this.minutes = minutes; + this.seconds = seconds; + normalizeTime(); + } + + private void normalizeTime() { + int totalSeconds = hours * 3600 + minutes * 60 + seconds; + this.hours = totalSeconds / 3600; + this.minutes = (totalSeconds % 3600) / 60; + this.seconds = (totalSeconds % 3600) % 60; + } + + public int getHours() { + return hours; + } + + public int getMinutes() { + return minutes; + + } + + public int getSeconds() { + return seconds; + } + + public void setHours(int hours) { + this.hours = hours; + normalizeTime(); + + } + + public void setMinutes(int minutes) { + this.minutes = minutes; + normalizeTime(); + + } + + public void setSeconds(int seconds) { + this.seconds = seconds; + normalizeTime(); + + } + + void add(TimeSpan time) { + this.hours += time.hours; + this.minutes += time.minutes; + this.seconds += time.seconds; + normalizeTime(); + + } + + void subtract(TimeSpan time) { + this.hours -= time.hours; + this.minutes -= time.minutes; + this.seconds -= time.seconds; + normalizeTime(); + + } + + public String toString() { + return hours + ":" + minutes + ":" + seconds; + } + +} diff --git a/task03/src/com/example/task03/ComplexNumber.java b/task03/src/com/example/task03/ComplexNumber.java new file mode 100644 index 00000000..238d1fb1 --- /dev/null +++ b/task03/src/com/example/task03/ComplexNumber.java @@ -0,0 +1,40 @@ +package com.example.task03; + +public class ComplexNumber { + private final double realPart; + private final double imaginaryPart; + + public ComplexNumber(double realPart, double imaginaryPart){ + this.realPart = realPart; + this.imaginaryPart = imaginaryPart; + } + + public double getRealPart() { + return realPart; + } + + public double getImaginaryPart() { + return imaginaryPart; + } + + public ComplexNumber add(ComplexNumber complexNumber){ + double newReal = this.realPart + complexNumber.realPart; + double newImaginary = this.imaginaryPart + complexNumber.imaginaryPart; + return new ComplexNumber(newReal, newImaginary); + } + + public ComplexNumber multiply(ComplexNumber complexNumber) { + double newReal = this.realPart * complexNumber.realPart - this.imaginaryPart * complexNumber.imaginaryPart; + double newImaginary = this.realPart * complexNumber.imaginaryPart + this.imaginaryPart * complexNumber.realPart; + return new ComplexNumber(newReal, newImaginary); + } + + @Override + public String toString() { + if (imaginaryPart >= 0) { + return realPart + "+" + imaginaryPart + "i"; + } + return realPart + "-" + (-imaginaryPart) + "i"; + } + +} diff --git a/task03/src/com/example/task03/Task03Main.java b/task03/src/com/example/task03/Task03Main.java index ae40e6f2..5facd5c1 100644 --- a/task03/src/com/example/task03/Task03Main.java +++ b/task03/src/com/example/task03/Task03Main.java @@ -2,6 +2,12 @@ public class Task03Main { public static void main(String[] args) { + ComplexNumber c1 = new ComplexNumber(2, 3); + ComplexNumber c2 = new ComplexNumber(1, -1); + System.out.println(c1); + System.out.println(c2); + System.out.println(c1.add(c2)); + System.out.println(c1.multiply(c1)); } } diff --git a/task04/src/com/example/task04/Line.java b/task04/src/com/example/task04/Line.java new file mode 100644 index 00000000..6a2686ce --- /dev/null +++ b/task04/src/com/example/task04/Line.java @@ -0,0 +1,29 @@ +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; + } + + @Override + public String toString() { + return String.format(" from (%d, %d) to (%d, %d)", p1.x, p1.y, p2.x, p2.y); + } + + public boolean isCollinearLine(Point p) { + return (p2.x - p1.x) * (p.y - p1.y) - (p.x - p1.x) * (p2.y - p1.y) == 0; + } + +} diff --git a/task04/src/com/example/task04/Point.java b/task04/src/com/example/task04/Point.java new file mode 100644 index 00000000..0ed810b4 --- /dev/null +++ b/task04/src/com/example/task04/Point.java @@ -0,0 +1,30 @@ +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) { + double dX = Math.pow(x - point.x, 2); + double dY = Math.pow(y - point.y, 2); + return Math.sqrt(dX + dY); + + } + + public String toString() { + return String.format("(%d, %d)", x, y); + } + + + void print() { + String pointToString = toString(); + System.out.println(pointToString); + } + +} diff --git a/task04/src/com/example/task04/Task04Main.java b/task04/src/com/example/task04/Task04Main.java index 55917a30..16472acc 100644 --- a/task04/src/com/example/task04/Task04Main.java +++ b/task04/src/com/example/task04/Task04Main.java @@ -2,6 +2,11 @@ public class Task04Main { public static void main(String[] args) { + Point p1 = new Point(2,5); + Point p2 = new Point(3,9); + Line l = new Line(p1,p2); + System.out.println(l); + System.out.println(l.isCollinearLine(new Point(-4,3))); } } diff --git a/task05/src/com/example/task05/Point.java b/task05/src/com/example/task05/Point.java index 968ea652..06603d55 100644 --- a/task05/src/com/example/task05/Point.java +++ b/task05/src/com/example/task05/Point.java @@ -4,6 +4,8 @@ * Точка в двумерном пространстве */ public class Point { + final double x; + final double y; /** * Конструктор, инициализирующий координаты точки @@ -12,7 +14,9 @@ public class Point { * @param y координата по оси ординат */ public Point(double x, double y) { - throw new AssertionError(); + this.x = x; + this.y = y; + } /** @@ -21,8 +25,7 @@ public Point(double x, double y) { * @return координату точки по оси X */ public double getX() { - // TODO: реализовать - throw new AssertionError(); + return x; } /** @@ -31,8 +34,7 @@ public double getX() { * @return координату точки по оси Y */ public double getY() { - // TODO: реализовать - throw new AssertionError(); + return y; } /** @@ -42,8 +44,9 @@ public double getY() { * @return расстояние от текущей точки до переданной */ public double getLength(Point point) { - // TODO: реализовать - throw new AssertionError(); + double newX = Math.pow(x - point.x, 2); + double newY = Math.pow(y - point.y, 2); + return Math.sqrt(newX + newY); } } diff --git a/task05/src/com/example/task05/PolygonalLine.java b/task05/src/com/example/task05/PolygonalLine.java index b534bfd5..ccf0d9b1 100644 --- a/task05/src/com/example/task05/PolygonalLine.java +++ b/task05/src/com/example/task05/PolygonalLine.java @@ -1,9 +1,12 @@ package com.example.task05; +import java.util.ArrayList; + /** * Ломаная линия */ public class PolygonalLine { + private ArrayList line = new ArrayList(); /** * Устанавливает точки ломаной линии @@ -11,7 +14,9 @@ public class PolygonalLine { * @param points массив точек, которыми нужно проинициализировать ломаную линию */ public void setPoints(Point[] points) { - // TODO: реализовать + for (Point point : points) { + addPoint(point); + } } /** @@ -20,7 +25,7 @@ public void setPoints(Point[] points) { * @param point точка, которую нужно добавить к ломаной */ public void addPoint(Point point) { - // TODO: реализовать + line.add(new Point(point.getX(), point.getY())); } /** @@ -30,7 +35,7 @@ public void addPoint(Point point) { * @param y координата по оси ординат */ public void addPoint(double x, double y) { - // TODO: реализовать + line.add(new Point(x, y)); } /** @@ -39,8 +44,13 @@ public void addPoint(double x, double y) { * @return длину ломаной линии */ public double getLength() { - // TODO: реализовать - throw new AssertionError(); + double length = 0; + for (int i = 0; i < line.size() - 1; i++) { + Point p1 = line.get(i); + Point p2 = line.get(i + 1); + length += p1.getLength(p2); + } + return length; } } diff --git a/task05/src/com/example/task05/Task05Main.java b/task05/src/com/example/task05/Task05Main.java index 4f745fb6..4c3a7aea 100644 --- a/task05/src/com/example/task05/Task05Main.java +++ b/task05/src/com/example/task05/Task05Main.java @@ -2,6 +2,12 @@ public class Task05Main { public static void main(String[] args) { + Point p1 = new Point(2, 4); + Point p2 = new Point(5, 1); + Point p3 = new Point(1, 3); + PolygonalLine polygonalLine = new PolygonalLine(); + polygonalLine.setPoints(new Point[]{p1, p2, p3}); + System.out.println(polygonalLine.getLength()); } }