From 996c2d59688fd586c7b296b3d352d41ebb3fd37b Mon Sep 17 00:00:00 2001 From: Anna Date: Fri, 14 Mar 2025 20:37:37 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BD=D0=BE=D0=BF=D0=BB=D1=91?= =?UTF-8?q?=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task01/src/com/example/task01/Point.java | 27 ++++++-- task01/src/com/example/task01/Task01Main.java | 12 ++-- task02/src/com/example/task02/Task02Main.java | 2 + task02/src/com/example/task02/TimeSpan.java | 62 +++++++++++++++++++ .../src/com/example/task03/ComplexNumber.java | 28 +++++++++ task03/src/com/example/task03/Task03Main.java | 6 ++ task04/src/com/example/task04/Line.java | 28 +++++++++ task04/src/com/example/task04/Point.java | 22 +++++++ task04/src/com/example/task04/Task04Main.java | 7 +++ task05/src/com/example/task05/Point.java | 35 +++++------ .../src/com/example/task05/PolygonalLine.java | 38 ++++++------ 11 files changed, 219 insertions(+), 48 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..16344b89 100644 --- a/task01/src/com/example/task01/Point.java +++ b/task01/src/com/example/task01/Point.java @@ -7,8 +7,27 @@ public class Point { int x; int y; - void print() { - String pointToString = String.format("(%d, %d)", x, y); - System.out.println(pointToString); + public Point(int x, int y) { + this.x = x; + this.y = y; + } + + + public void flip() { + int temp = x; + + x = -y; + y = -temp; + } + + public double distance(Point point) { + double a = Math.pow(x - point.x, 2); + double b = Math.pow(y - point.y, 2); + + return Math.sqrt(a + b); + } + + public String toString() { + return String.format("(%d, %d)", x, y); } -} +} \ No newline at end of file diff --git a/task01/src/com/example/task01/Task01Main.java b/task01/src/com/example/task01/Task01Main.java index 7d71173a..a48b4cbb 100644 --- a/task01/src/com/example/task01/Task01Main.java +++ b/task01/src/com/example/task01/Task01Main.java @@ -2,18 +2,14 @@ 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(); + p1.toString(); System.out.println(p1); System.out.println("Point 2:"); - p2.print(); + p2.toString(); System.out.println(p2); } } diff --git a/task02/src/com/example/task02/Task02Main.java b/task02/src/com/example/task02/Task02Main.java index 644a0eba..21cca4c1 100644 --- a/task02/src/com/example/task02/Task02Main.java +++ b/task02/src/com/example/task02/Task02Main.java @@ -3,5 +3,7 @@ public class Task02Main { public static void main(String[] args) { + TimeSpan time = new TimeSpan(1, 100, 65); + System.out.println(time); } } diff --git a/task02/src/com/example/task02/TimeSpan.java b/task02/src/com/example/task02/TimeSpan.java new file mode 100644 index 00000000..1e6a3b1b --- /dev/null +++ b/task02/src/com/example/task02/TimeSpan.java @@ -0,0 +1,62 @@ +package com.example.task02; + +public class TimeSpan { + private int Hours; + private int Minutes; + private int Seconds; + + public TimeSpan(int hours, int minutes, int seconds) { + correctTimeFormat(hours, minutes, seconds); + } + + private void correctTimeFormat(int hours, int minutes, int seconds) { + if (seconds > 59) { + minutes += seconds / 60; + seconds %= 60; + } + if (minutes > 59) { + hours += minutes / 60; + minutes %= 60; + } + + Hours = hours; + Minutes = minutes; + Seconds = seconds; + } + + public int getHours() { + return Hours; + } + + public void setHours(int valueOfHours) { + Hours = valueOfHours; + } + + public int getMinutes() { + return Minutes; + } + + public void setMinutes(int valueOfMinutes) { + Minutes = valueOfMinutes; + } + + public int getSeconds() { + return Seconds; + } + + public void setSeconds(int valueOfSeconds) { + Seconds = valueOfSeconds; + } + + public void add(TimeSpan time) { + correctTimeFormat(Hours + time.Hours, Minutes + time.Minutes, Seconds + time.Seconds); + } + + public void subtract(TimeSpan time) { + correctTimeFormat(Math.abs(Hours - time.Hours), Math.abs(Minutes - time.Minutes), Math.abs(Seconds - time.Seconds)); + } + + public String toString() { + return String.format("%d:%d:%d", Hours, Minutes, Seconds); + } +} \ No newline at end of file diff --git a/task03/src/com/example/task03/ComplexNumber.java b/task03/src/com/example/task03/ComplexNumber.java new file mode 100644 index 00000000..a38a8046 --- /dev/null +++ b/task03/src/com/example/task03/ComplexNumber.java @@ -0,0 +1,28 @@ +package com.example.task03; + +public class ComplexNumber { + private final double RealPart; + private final double ImaginaryPart; + + public ComplexNumber(double realPart, double imaginaryPart) { + RealPart = realPart; + ImaginaryPart = imaginaryPart; + } + + public ComplexNumber summarizeComplexNumbers(ComplexNumber number) { + double realParts = RealPart + number.RealPart; + double imaginaryParts = ImaginaryPart + number.ImaginaryPart; + return new ComplexNumber(realParts, imaginaryParts); + } + + public ComplexNumber multiplyComplexNumbers(ComplexNumber number) { + double a = RealPart * number.RealPart - ImaginaryPart * number.ImaginaryPart; + double b = RealPart * number.ImaginaryPart + ImaginaryPart * number.RealPart; + return new ComplexNumber(a, b); + } + + public String toString() { + String complexOneUnit = "i"; + return (ImaginaryPart > 0) ? RealPart + "+" + ImaginaryPart + complexOneUnit : RealPart + "" + ImaginaryPart + complexOneUnit; + } +} \ No newline at end of file diff --git a/task03/src/com/example/task03/Task03Main.java b/task03/src/com/example/task03/Task03Main.java index ae40e6f2..a4e2fd71 100644 --- a/task03/src/com/example/task03/Task03Main.java +++ b/task03/src/com/example/task03/Task03Main.java @@ -3,5 +3,11 @@ public class Task03Main { public static void main(String[] args) { + ComplexNumber num1 = new ComplexNumber(5, 4); + ComplexNumber num2 = new ComplexNumber(10, 6); + ComplexNumber sum = num1.summarizeComplexNumbers(num2); + ComplexNumber mul = num1.multiplyComplexNumbers(num2); + System.out.println(sum); + System.out.println(mul); } } diff --git a/task04/src/com/example/task04/Line.java b/task04/src/com/example/task04/Line.java new file mode 100644 index 00000000..eb217230 --- /dev/null +++ b/task04/src/com/example/task04/Line.java @@ -0,0 +1,28 @@ +package com.example.task04; + +public class Line { + private final Point Start; + private final Point End; + + public Line(Point start, Point end) { + Start = start; + End = end; + } + + public Point getP1() { + return Start; + } + + public Point getP2() { + return End; + } + + public boolean isCollinearLine(Point p) { + return (End.x - Start.x) * (p.y - Start.y) - (p.x - Start.x) * (End.y - Start.y) == 0; + } + + @Override + public String toString() { + return String.format("(%d, %d), (%d, %d)", Start.x, Start.y, End.x, End.y); + } +} \ No newline at end of file diff --git a/task04/src/com/example/task04/Point.java b/task04/src/com/example/task04/Point.java new file mode 100644 index 00000000..e2ce85d2 --- /dev/null +++ b/task04/src/com/example/task04/Point.java @@ -0,0 +1,22 @@ +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; + } + + public double distance(Point point) { + double a = Math.pow(x - point.x, 2); + double b = Math.pow(y - point.y, 2); + + return Math.sqrt(a + b); + } + + public String toString() { + return String.format("(%d, %d)", x, y); + } +} \ No newline at end of file diff --git a/task04/src/com/example/task04/Task04Main.java b/task04/src/com/example/task04/Task04Main.java index 55917a30..998b1763 100644 --- a/task04/src/com/example/task04/Task04Main.java +++ b/task04/src/com/example/task04/Task04Main.java @@ -2,6 +2,13 @@ public class Task04Main { public static void main(String[] args) { + Point start = new Point(1, 1); + Point end = new Point(3, 5); + Point point = new Point(4, 7); + Line line = new Line(start, end); + boolean result = line.isCollinearLine(point); + + System.out.println(result); } } diff --git a/task05/src/com/example/task05/Point.java b/task05/src/com/example/task05/Point.java index 968ea652..644765b2 100644 --- a/task05/src/com/example/task05/Point.java +++ b/task05/src/com/example/task05/Point.java @@ -5,24 +5,18 @@ */ public class Point { - /** - * Конструктор, инициализирующий координаты точки - * - * @param x координата по оси абсцисс - * @param y координата по оси ординат - */ - public Point(double x, double y) { - throw new AssertionError(); - } + public class Point { + private final double X; + private final double Y; - /** - * Возвращает координату точки по оси абсцисс - * - * @return координату точки по оси X - */ + public Point(double x, double y){ + + X =x; + Y =y; + } public double getX() { - // TODO: реализовать - throw new AssertionError(); + + return X; } /** @@ -32,7 +26,8 @@ public double getX() { */ public double getY() { // TODO: реализовать - throw new AssertionError(); + + return Y; } /** @@ -43,7 +38,11 @@ public double getY() { */ public double getLength(Point point) { // TODO: реализовать - throw new AssertionError(); + + double a = Math.pow(X - point.X, 2); + double b = Math.pow(Y - point.Y, 2); + + return Math.sqrt(a + b); } } diff --git a/task05/src/com/example/task05/PolygonalLine.java b/task05/src/com/example/task05/PolygonalLine.java index b534bfd5..bb869dbf 100644 --- a/task05/src/com/example/task05/PolygonalLine.java +++ b/task05/src/com/example/task05/PolygonalLine.java @@ -1,26 +1,21 @@ package com.example.task05; -/** - * Ломаная линия - */ +import java.util.ArrayList; + public class PolygonalLine { + private final ArrayList Line; - /** - * Устанавливает точки ломаной линии - * - * @param points массив точек, которыми нужно проинициализировать ломаную линию - */ + public PolygonalLine() { + Line = new ArrayList<>(); + } public void setPoints(Point[] points) { - // TODO: реализовать + for (Point p : points) { + addPoint(p); } - /** - * Добавляет точку к ломаной линии - * - * @param point точка, которую нужно добавить к ломаной - */ + public void addPoint(Point point) { - // TODO: реализовать + Line.add(new Point(point.getX(), point.getY())); } /** @@ -30,7 +25,7 @@ public void addPoint(Point point) { * @param y координата по оси ординат */ public void addPoint(double x, double y) { - // TODO: реализовать + Line.add(new Point(x, y)); } /** @@ -39,8 +34,15 @@ public void addPoint(double x, double y) { * @return длину ломаной линии */ public double getLength() { - // TODO: реализовать - throw new AssertionError(); + + double lenght = 0; + for (int i = 0; i < Line.size() - 1; i++) { + Point first = Line.get(i); + Point second = Line.get(i + 1); + lenght += first.getLength(second); + } + + return lenght; } }