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
29 changes: 27 additions & 2 deletions task01/src/com/example/task01/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
14 changes: 9 additions & 5 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 8 additions & 0 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}
108 changes: 108 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
38 changes: 38 additions & 0 deletions task03/src/com/example/task03/ComplexNumbers.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
7 changes: 7 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,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));
}
}
55 changes: 55 additions & 0 deletions task04/src/com/example/task04/Line.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
15 changes: 15 additions & 0 deletions task04/src/com/example/task04/Task04Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));

}
}
15 changes: 9 additions & 6 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
* Точка в двумерном пространстве
*/
public class Point {

private final double x;
private final double y;
/**
* Конструктор, инициализирующий координаты точки
*
* @param x координата по оси абсцисс
* @param y координата по оси ординат
*/
public Point(double x, double y) {
throw new AssertionError();
this.x = x;
this.y = y;
}

/**
Expand All @@ -22,7 +24,7 @@ public Point(double x, double y) {
*/
public double getX() {
// TODO: реализовать
throw new AssertionError();
return x;
}

/**
Expand All @@ -32,7 +34,7 @@ public double getX() {
*/
public double getY() {
// TODO: реализовать
throw new AssertionError();
return y;
}

/**
Expand All @@ -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);
}

}
Loading