Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work #610

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Work #610

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
21 changes: 17 additions & 4 deletions task01/src/com/example/task01/Point.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package com.example.task01;

/**
* Класс точки на плоскости
*/
public class Point {
int x;
int y;

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

public void print() {
String pointToString = String.format("(%d, %d)", x, y);
System.out.println(pointToString);
}
public void flip(){
int temp = this.x;
this.x = -this.y;
this.y = -temp;
}
public double distance(Point point){
return Math.sqrt(Math.pow(point.x - this.x, 2) + Math.pow(point.y - this.y, 2));
}
public String toString(){
return String.format("(%d, %d)", x, y);
}
}
8 changes: 5 additions & 3 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

public class Task01Main {
public static void main(String[] args) {
Point p1 = new Point();
p1.x = 10;
Point p1 = new Point(-20,0);
p1.x = -10;
p1.y = 45;
Point p2 = new Point();
Point p2 = new Point(0,0);
p2.x = 78;
p2.y = 12;

p1.flip();
System.out.println(p1.x);
System.out.println("Point 1:");
p1.print();
System.out.println(p1);
Expand Down
4 changes: 3 additions & 1 deletion task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.task02;



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

TimeSpan T1 = new TimeSpan(1,20,0);
}
}
95 changes: 95 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.example.task02;

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

public TimeSpan(int hours, int minutes, int seconds) {
setHours(hours);
setMinutes(minutes);
setSeconds(seconds);
normalize();
}

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;
normalize();
}

public int getSeconds() {
return seconds;
}

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

private void normalize() {
if (seconds >= 60) {
minutes += seconds / 60;
seconds %= 60;
} else if (seconds < 0) {
minutes -= (-seconds + 59) / 60;
seconds = (seconds % 60 + 60) % 60;
}

if (minutes >= 60) {
hours += minutes / 60;
minutes %= 60;
} else if (minutes < 0) {
hours -= (-minutes + 59) / 60;
minutes = (minutes % 60 + 60) % 60;
}

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

public void add(TimeSpan time) {
this.hours += time.getHours();
this.minutes += time.getMinutes();
this.seconds += time.getSeconds();
normalize();
}

public void subtract(TimeSpan time) {
this.hours -= time.getHours();
this.minutes -= time.getMinutes();
this.seconds -= time.getSeconds();
normalize();
}

@Override
public String toString() {
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}

public static void main(String[] args) {
TimeSpan t1 = new TimeSpan(1, 70, 120);
TimeSpan t2 = new TimeSpan(0, 30, 50);

System.out.println("Первый интервал: " + t1);
System.out.println("Второй интервал: " + t2);

t1.add(t2);
System.out.println("После сложения: " + t1);

t1.subtract(new TimeSpan(2, 15, 30));
System.out.println("После вычитания: " + t1);
}
}
28 changes: 28 additions & 0 deletions task03/src/com/example/task03/ComplexNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.task03;

public class ComplexNumber {
private double real;
private double imaginary;

public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}

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

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() {
return String.format("%.2f + %.2fi", real, imaginary);
}
}
9 changes: 9 additions & 0 deletions task03/src/com/example/task03/Task03Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

public class Task03Main {
public static void main(String[] args) {
ComplexNumber num1 = new ComplexNumber(3, 4);
ComplexNumber num2 = new ComplexNumber(1, 2);

// Вычисляем сумму
ComplexNumber sum = num1.add(num2);
System.out.println("Сумма: " + sum);

// Вычисляем произведение
ComplexNumber product = num1.multiply(num2);
System.out.println("Произведение: " + product);
}
}
29 changes: 29 additions & 0 deletions task04/src/com/example/task04/Line.java
Original file line number Diff line number Diff line change
@@ -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("Line from %s to %s", p1, p2);
}

public boolean isCollinearLine(Point p) {
return (p2.getY() - p1.getY()) * (p.getX() - p1.getX()) ==
(p.getY() - p1.getY()) * (p2.getX() - p1.getX());
}
}
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: 11 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,17 @@

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

Line line = new Line(p1, p2);

System.out.println(line);

Point testPoint = new Point(2, 2);
System.out.println(line.isCollinearLine(testPoint)); // Вывод: true

Point nonColPoint = new Point(2, 3);
System.out.println(line.isCollinearLine(nonColPoint)); // Вывод: false
}
}
15 changes: 7 additions & 8 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 double x;
private 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,6 @@ public double getY() {
* @return расстояние от текущей точки до переданной
*/
public double getLength(Point point) {
// TODO: реализовать
throw new AssertionError();
return Math.sqrt(Math.pow(point.getX() - this.x, 2) + Math.pow(point.getY() - this.y, 2));
}

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

/**
* Ломаная линия
*/
import java.util.ArrayList;
import java.util.List;


public class PolygonalLine {
private List<Point> points;

public PolygonalLine() {
points = new ArrayList<>();
}

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

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

/**
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.0;
for (int i = 0; i < points.size() - 1; i++) {
length += points.get(i).getLength(points.get(i + 1));
}
return length;
}

}
Loading