diff --git a/task01/src/com/example/task01/Point.java b/task01/src/com/example/task01/Point.java index ec5c69e8..44666728 100644 --- a/task01/src/com/example/task01/Point.java +++ b/task01/src/com/example/task01/Point.java @@ -7,8 +7,33 @@ public class Point { int x; int y; + public Point(int x, int y){ + this.x = x; + this.y = y; + } + + public Point(){ + this(0,0); + } + + void flip(){ + int item = x; + x = - y; + y = - item; + } + + double distance(Point point){ + double distanceX = point.x - this.x; + double distanceY = point.y - this.y; + double result = Math.sqrt((distanceX * distanceX) + (distanceY * distanceY)); + return result; + } + + 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()); } } diff --git a/task01/src/com/example/task01/Task01Main.java b/task01/src/com/example/task01/Task01Main.java index 7d71173a..137e7cd3 100644 --- a/task01/src/com/example/task01/Task01Main.java +++ b/task01/src/com/example/task01/Task01Main.java @@ -2,18 +2,22 @@ public class Task01Main { public static void main(String[] args) { - Point p1 = new Point(); - p1.x = 10; - p1.y = 45; + Point p1 = new Point(10,45); + p1.x = 11; + p1.y = 46; Point p2 = new Point(); p2.x = 78; p2.y = 12; + p1.flip(); + System.out.println(p1); + + System.out.println(p1.distance(p2)); + + System.out.println("Point 1:"); - p1.print(); System.out.println(p1); System.out.println("Point 2:"); - p2.print(); System.out.println(p2); } } diff --git a/task02/src/com/example/task02/Task02Main.java b/task02/src/com/example/task02/Task02Main.java index 644a0eba..d6faccf0 100644 --- a/task02/src/com/example/task02/Task02Main.java +++ b/task02/src/com/example/task02/Task02Main.java @@ -2,6 +2,14 @@ public class Task02Main { public static void main(String[] args) { + TimeSpan time1 = new TimeSpan(1,100,130); + System.out.println("Время 1 до "); + System.out.println(time1); + time1.setSeconds(3000); + System.out.println("Время 1 после"); + System.out.println(time1); + + time1.setSeconds(-1); } } diff --git a/task02/src/com/example/task02/TimeSpan.java b/task02/src/com/example/task02/TimeSpan.java new file mode 100644 index 00000000..058bd58b --- /dev/null +++ b/task02/src/com/example/task02/TimeSpan.java @@ -0,0 +1,108 @@ +package com.example.task02; + +public class TimeSpan { + + private int inHours; + private int inMinutes; + private int inSeconds; + + public TimeSpan(int hours, int minutes, int seconds) { + if (hours < 0 || minutes < 0 || seconds < 0) { + throw new IllegalArgumentException("Все компоненты временного интервала должны быть неотрицательными"); + } + this.inHours = hours; + this.inMinutes = minutes; + this.inSeconds = seconds; + transformTime(); // на случай, если минут/секунд ≥ 60 + } + + public int getHours() { + return inHours; + } + + public void setHours(int hours) { + if (hours < 0) { + throw new IllegalArgumentException("Часы не могут быть отрицательными"); + } + this.inHours = hours; + transformTime(); // на случай нормализации полей + } + + public int getMinutes() { + return inMinutes; + } + + public void setMinutes(int minutes) { + if (minutes < 0) { + throw new IllegalArgumentException("Минуты не могут быть отрицательными"); + } + this.inMinutes = minutes; + transformTime(); + } + + public int getSeconds() { + return inSeconds; + } + + public void setSeconds(int seconds) { + if (seconds < 0) { + throw new IllegalArgumentException("Секунды не могут быть отрицательными"); + } + this.inSeconds = seconds; + transformTime(); + } + + private void transformTime() { + // Обработка секунд + if (inSeconds >= 60) { + inMinutes += inSeconds / 60; + inSeconds %= 60; + } else if (inSeconds < 0) { + int borrow = (-inSeconds + 59) / 60; // сколько минут занять + inMinutes -= borrow; + inSeconds += borrow * 60; + } + + // Обработка минут + if (inMinutes >= 60) { + inHours += inMinutes / 60; + inMinutes %= 60; + } else if (inMinutes < 0) { + int borrow = (-inMinutes + 59) / 60; // сколько часов занять + inHours -= borrow; + inMinutes += borrow * 60; + } + + if (inHours < 0) { + throw new IllegalStateException("Нельзя получить отрицательный временной интервал: результат вычитания недопустим"); + } + + // на случай переносов после заимствований + if (inMinutes >= 60) { + inHours += inMinutes / 60; + inMinutes %= 60; + } + if (inSeconds >= 60) { + inMinutes += inSeconds / 60; + inSeconds %= 60; + } + } + + public void add(TimeSpan time) { + this.inHours += time.inHours; + this.inMinutes += time.inMinutes; + this.inSeconds += time.inSeconds; + transformTime(); + } + + public void subtract(TimeSpan time) { + this.inHours -= time.inHours; + this.inMinutes -= time.inMinutes; + this.inSeconds -= time.inSeconds; + transformTime(); + } + + public String toString() { + return String.format("Временной интервал: %d ч %d мин %d сек", inHours, inMinutes, inSeconds); + } +} diff --git a/task03/src/com/example/task03/ComplexNumbers.java b/task03/src/com/example/task03/ComplexNumbers.java new file mode 100644 index 00000000..97497643 --- /dev/null +++ b/task03/src/com/example/task03/ComplexNumbers.java @@ -0,0 +1,38 @@ +package com.example.task03; + +public class ComplexNumbers { + + private final double realPart; + private final double imaginaryPart; + + public ComplexNumbers( double realPart, double imaginaryPart){ + this.realPart = realPart; + this.imaginaryPart = imaginaryPart; + } + + public ComplexNumbers add(ComplexNumbers comples){ + return new ComplexNumbers( + this.realPart + comples.realPart, + this.imaginaryPart + comples.imaginaryPart); + } + + public ComplexNumbers product(ComplexNumbers complex){ + double newReal = (this.realPart * complex.realPart) - (this.imaginaryPart * complex.imaginaryPart); + double newImaginary = this.realPart * complex.imaginaryPart + this.imaginaryPart * complex.realPart; + return new ComplexNumbers(newReal, newImaginary); + } + + public String toString() { + if (realPart == 0 && imaginaryPart == 0) { + return "Комплексное число: 0"; + } else if (realPart == 0) { + return String.format("Комплексное число: %.2fi", imaginaryPart); + } else if (imaginaryPart == 0) { + return String.format("Комплексное число: %.2f", realPart); + } else if (imaginaryPart > 0) { + return String.format("Комплексное число: %.2f + %.2fi", realPart, imaginaryPart); + } else { + return String.format("Комплексное число: %.2f - %.2fi", realPart, -imaginaryPart); + } + } +} diff --git a/task03/src/com/example/task03/Task03Main.java b/task03/src/com/example/task03/Task03Main.java index ae40e6f2..fb49b20c 100644 --- a/task03/src/com/example/task03/Task03Main.java +++ b/task03/src/com/example/task03/Task03Main.java @@ -2,6 +2,13 @@ public class Task03Main { public static void main(String[] args) { + ComplexNumbers complex1 = new ComplexNumbers(0,2); + ComplexNumbers complex2 = new ComplexNumbers(1,-4); + System.out.println(complex1); + System.out.println(complex2); + + System.out.println(complex1.add(complex2)); + System.out.println(complex1.product(complex2)); } } diff --git a/task04/src/com/example/task04/Line.java b/task04/src/com/example/task04/Line.java new file mode 100644 index 00000000..9758333c --- /dev/null +++ b/task04/src/com/example/task04/Line.java @@ -0,0 +1,55 @@ +package com.example.task04; + +import java.awt.*; + +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; } + + public String toString() { + return "(" + x + ", " + y + ")"; + } +} + +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 String toString(){ + return String.format("Отрезок с началом в (%d, %d) и с концом в (%d, %d)", + p1.getX(), p1.getY(), p2.getX(), p2.getY()); + } + + public boolean isCollinearLine(Point p){ + + int x = p.getX(); + int y = p.getY(); + int area = (x - p1.getX()) * (p2.getY() - p1.getY()) - (y - p1.getY()) * (p2.getX() - p1.getX()); + return area == 0; + } + +} diff --git a/task04/src/com/example/task04/Task04Main.java b/task04/src/com/example/task04/Task04Main.java index 55917a30..a728c33e 100644 --- a/task04/src/com/example/task04/Task04Main.java +++ b/task04/src/com/example/task04/Task04Main.java @@ -3,5 +3,20 @@ public class Task04Main { public static void main(String[] args) { + Point p1 = new Point(1,1); + Point p2 = new Point(4,5); + Line section = new Line(p1,p2); + + Point startingPoint = section.getP1(); + Point enddingPoint = section.getP2(); + + System.out.println("Начало отрезка - " + startingPoint); + System.out.println("Конец отрезка - " + enddingPoint); + + System.out.println(section); + + Point p3 = new Point(2,3); + System.out.println("Точка" + p3 + " на прямой: " + section.isCollinearLine(p3)); + } } diff --git a/task05/src/com/example/task05/Point.java b/task05/src/com/example/task05/Point.java index 968ea652..1cb4d94a 100644 --- a/task05/src/com/example/task05/Point.java +++ b/task05/src/com/example/task05/Point.java @@ -4,7 +4,8 @@ * Точка в двумерном пространстве */ public class Point { - + private final double x; + private final double y; /** * Конструктор, инициализирующий координаты точки * @@ -12,7 +13,8 @@ public class Point { * @param y координата по оси ординат */ public Point(double x, double y) { - throw new AssertionError(); + this.x = x; + this.y = y; } /** @@ -22,7 +24,7 @@ public Point(double x, double y) { */ public double getX() { // TODO: реализовать - throw new AssertionError(); + return x; } /** @@ -32,7 +34,7 @@ public double getX() { */ public double getY() { // TODO: реализовать - throw new AssertionError(); + return y; } /** @@ -43,7 +45,8 @@ public double getY() { */ public double getLength(Point point) { // TODO: реализовать - throw new AssertionError(); + double deltaX = this.x - point.x; + double deltaY = this.y - point.y; + return Math.sqrt(deltaX * deltaX + deltaY * deltaY); } - } diff --git a/task05/src/com/example/task05/PolygonalLine.java b/task05/src/com/example/task05/PolygonalLine.java index b534bfd5..d5077dee 100644 --- a/task05/src/com/example/task05/PolygonalLine.java +++ b/task05/src/com/example/task05/PolygonalLine.java @@ -1,10 +1,33 @@ package com.example.task05; +import sun.security.util.Length; +import java.util.ArrayList; +import java.util.List; /** * Ломаная линия */ public class PolygonalLine { + private Point[] points; + + public PolygonalLine() { + this.points = new Point[0]; // Создаем пустой массив точек + } + + public PolygonalLine(Point[] points) { + if (points == null) { + this.points = new Point[0]; + } else { + this.points = new Point[points.length]; + + for (int i = 0; i < points.length; i++){ + if(points[i] != null){ + this.points[i] = new Point(points[i].getX(), points[i].getY()); + } + } + } + } + /** * Устанавливает точки ломаной линии * @@ -12,6 +35,16 @@ public class PolygonalLine { */ public void setPoints(Point[] points) { // TODO: реализовать + if (points == null) { + this.points = new Point[0]; + } else { + this.points = new Point[points.length]; + for (int i = 0; i < points.length; i++) { + if (points[i] != null) { + this.points[i] = new Point(points[i].getX(), points[i].getY()); + } + } + } } /** @@ -21,6 +54,17 @@ public void setPoints(Point[] points) { */ public void addPoint(Point point) { // TODO: реализовать + if (point == null) return; + + Point newPoin = new Point(point.getX(), point.getY()); + + Point[] newPoint = new Point[points.length+1]; + + for (int i = 0; i < points.length; i++){ + newPoint[i] = points[i]; + } + newPoint[points.length] = newPoin; + points = newPoint; } /** @@ -31,6 +75,8 @@ public void addPoint(Point point) { */ public void addPoint(double x, double y) { // TODO: реализовать + Point point = new Point(x, y); + addPoint(point); } /** @@ -40,7 +86,14 @@ public void addPoint(double x, double y) { */ public double getLength() { // TODO: реализовать - throw new AssertionError(); + if (points.length < 2) return 0.0; + + double lineLength = 0.0; + + for (int i = 0; i < points.length - 1; i++){ + lineLength += points[i].getLength(points[i + 1]); + } + return lineLength; } } diff --git a/task05/src/com/example/task05/Task05Main.java b/task05/src/com/example/task05/Task05Main.java index 4f745fb6..ee2b4e8c 100644 --- a/task05/src/com/example/task05/Task05Main.java +++ b/task05/src/com/example/task05/Task05Main.java @@ -3,5 +3,15 @@ public class Task05Main { public static void main(String[] args) { + PolygonalLine line = new PolygonalLine(); + line.setPoints(new Point[]{ + new Point(2,3), + new Point(4,4), + new Point(1,3), + new Point(7,2) + }); + line.addPoint(0,5); + System.out.println("Длина ломаной: " + line.getLength()); + } }