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
23 changes: 21 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,27 @@
* Класс точки на плоскости
*/
public class Point {
int x;
int y;
private int x;
private int y;

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

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

double distance(Point point){
return Math.sqrt(Math.pow(point.x - x, 2) + Math.pow(point.y - y, 2));
}

public String toString(){
return x + y + "";
}

void print() {
String pointToString = String.format("(%d, %d)", x, y);
Expand Down
10 changes: 4 additions & 6 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

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(5,-7);
Point p2 = new Point(78,12);

System.out.println("Point 1:");
p1.print();
p1.flip();
p1.print();
System.out.println(p1);
System.out.println("Point 2:");
p2.print();
Expand Down
27 changes: 27 additions & 0 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,32 @@
public class Task02Main {
public static void main(String[] args) {

TimeSpan timeSpan = new TimeSpan(0, 0, 0);

System.out.println("Time span 1 is:\n" + timeSpan.toString());

System.out.println("hours before setter is " + timeSpan.getHour());
System.out.println("minutes is " + timeSpan.getMinute());
System.out.println("seconds is " + timeSpan.getSecond());
System.out.println();

timeSpan.setHour(18);
timeSpan.setMinute(80);
timeSpan.setSecond(90);

System.out.println("hours after setter is:" + timeSpan.getHour());
System.out.println("minutes after setter is:" + timeSpan.getMinute());
System.out.println("seconds after setter is:" + timeSpan.getSecond());

TimeSpan timeSpan2 = new TimeSpan(timeSpan.getHour(), timeSpan.getMinute(), timeSpan.getSecond());

timeSpan2.add(timeSpan);
System.out.println("Time span 2 is:\n" + timeSpan2);
timeSpan2.subtract(timeSpan);


System.out.println("Time span 2 is:\n" + timeSpan2);


}
}
94 changes: 94 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.example.task02;

public class TimeSpan {
private int hour;
private int minute;
private int second;

public TimeSpan(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
normalize();
}

public void setSecond(int second) {
if (second < 0) {
throw new IllegalArgumentException("секунды не могут быть отрицательными");
}
this.second = second;
normalize();
}

public void setMinute(int minute) {
if (minute < 0) {
throw new IllegalArgumentException("минуты не могут быть отрицательными");
}
this.minute = minute;
normalize();
}

public void setHour(int hour) {
if (hour >= 0) {
this.hour = hour;
} else {
throw new IllegalArgumentException("часы не могут быть отрицательными");
}
}

public int getHour() {
return hour;
}

public int getMinute() {
return minute;
}

public int getSecond() {
return second;
}

// Метод для нормализации времени
private void normalize() {
if (this.second >= 60) {
this.minute += this.second / 60;
this.second = this.second % 60;
} else if (this.second < 0) {
this.minute -= Math.abs(this.second) / 60 + 1;
this.second = 60 - Math.abs(this.second) % 60;
}

if (this.minute >= 60) {
this.hour += this.minute / 60;
this.minute = this.minute % 60;
} else if (this.minute < 0) {
this.hour -= Math.abs(this.minute) / 60 + 1;
this.minute = 60 - Math.abs(this.minute) % 60;
}
}

public void add(TimeSpan time){
this.hour += time.getHour();
this.minute += time.getMinute();
this.second += time.getSecond();
normalize();
}

public void subtract(TimeSpan time){
this.hour -= time.getHour();
this.minute -= time.getMinute();
this.second -= time.getSecond();

if (this.hour < 0) {
this.hour = 0;
this.minute = 0;
this.second = 0;
} else {
normalize();
}
}

public String toString() {
return String.format("%02d:%02d:%02d", this.hour, this.minute, this.second);
}
}
42 changes: 42 additions & 0 deletions task03/src/com/example/task03/ComplexNum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.task03;

public class ComplexNum {
private double real;
private double imag;

public ComplexNum(double real, double imag) {
this.real = real;
this.imag = imag;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImag() {
return imag;
}
public void setImag(double imag) {
this.imag = imag;
}

public static ComplexNum add(ComplexNum a, ComplexNum b) {
double newReal = a.real + b.real;
double newImag = a.imag + b.imag;
return new ComplexNum(newReal, newImag);
}

public static ComplexNum multiply(ComplexNum a, ComplexNum b) {
double newReal = (a.real * b.real) - (a.imag * b.imag);
double newImag = (a.real * b.imag) + (a.imag * b.real);
return new ComplexNum(newReal, newImag);
}

public String toString() {
return String.format("%.1f + %.1fi", real, imag);
}



}
7 changes: 6 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,11 @@

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

ComplexNum a = new ComplexNum(3, 2);
ComplexNum b = new ComplexNum(1, 7);
ComplexNum c = ComplexNum.add(a, b);
System.out.println("Sum:\n" + a + " + " + b + " = " + c);
c = ComplexNum.multiply(a, b);
System.out.println("Multiply:\n(" + a + ") * (" + b + ") = " + c);
}
}
32 changes: 32 additions & 0 deletions task04/src/com/example/task04/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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;
}

public boolean isCollinearLine(Point p) {
// Проверяем, если три точки (p1, p2 и p) коллинеарны, используя определитель матрицы
double determinant = (p2.getX() - p1.getX()) * (p.getY() - p1.getY()) - (p2.getY() - p1.getY()) * (p.getX() - p1.getX());

// Если определитель равен 0, значит, точки коллинеарны
return Math.abs(determinant) < 1e-9; // учёт погрешности
}

public String toString() {
return String.format("Отрезок от %s до %s", p1.toString(), p2.toString());
}
}

22 changes: 22 additions & 0 deletions task04/src/com/example/task04/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.task04;

public class Point {
private final double x;
private final double y;

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

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

public String toString() {
return String.format("(%.2f, %.2f)", x, y);
}
}
9 changes: 9 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,15 @@

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

Line line = new Line(p1, p2);

boolean isCollinear = line.isCollinearLine(p3);

System.out.println(line);
System.out.println("Точка " + p3 + " лежит на линии: " + isCollinear);
}
}
20 changes: 13 additions & 7 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@
* Точка в двумерном пространстве
*/
public class Point {
private final double x; // Координата X
private final double y; // Координата 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();
return x;
}

/**
Expand All @@ -31,8 +35,7 @@ public double getX() {
* @return координату точки по оси Y
*/
public double getY() {
// TODO: реализовать
throw new AssertionError();
return y;
}

/**
Expand All @@ -42,8 +45,11 @@ 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));
}

public String toString() {
return String.format("(%.2f, %.2f)", x, y);
}

}
Loading