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
4 changes: 2 additions & 2 deletions task01/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Задание 01 - Точка
``# Задание 01 - Точка

Создайте класс точки на плоскости целых чисел. Каждая точка должна обладать двумя координатами - *x* и *y*

Expand All @@ -10,4 +10,4 @@
Добавьте метод `double distance(Point point)`, который будет принимать в качестве параметра объект точки и должен считать расстояние между двумя точками
(той, для которой вызывается метод, и переданной в качестве аргумента)

Добавьте метод `public String toString()`, который должен возвращать строковое представление точки, например в формате (x, y)
Добавьте метод `public String toString()`, который должен возвращать строковое представление точки, например в формате (x, y)``
20 changes: 20 additions & 0 deletions task01/src/com/example/task01/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,24 @@ 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 p){
return Math.sqrt(Math.pow(p.x-x, 2) + Math.pow(p.y-y, 2));
}

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

}
4 changes: 2 additions & 2 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

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

Expand Down
6 changes: 6 additions & 0 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.example.task02;

public class Task02Main {

public static void main(String[] args) {
TimeSpan timeSpan1 = new TimeSpan(2,30,0);
TimeSpan timeSpan2 = new TimeSpan(1,40,0);

timeSpan1.subtract(timeSpan2);
timeSpan1.getHour();
System.out.println(timeSpan1);
}
}
59 changes: 59 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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;
sortTime();
}

public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
sortTime();
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
sortTime();
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
sortTime();
}

public void add(TimeSpan timeSpan) {
this.second += timeSpan.second;
this.minute += timeSpan.minute;
this.hour += timeSpan.hour;
sortTime();
}
public void subtract(TimeSpan timeSpan) {
this.second -= timeSpan.second;
this.minute -= timeSpan.minute;
this.hour -= timeSpan.hour;
}

public void sortTime(){
minute += second/60;
second %= 60;
hour += minute/60;
minute %= 60;
}

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

class ComplexNumber {
private double real;
private double imag;

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

public ComplexNumber add(ComplexNumber other) {
return new ComplexNumber(this.real + other.real, this.imag + other.imag);
}

public ComplexNumber multiply(ComplexNumber other) {
double newReal = this.real * other.real - this.imag * other.imag;
double newImag = this.real * other.imag + this.imag * other.real;
return new ComplexNumber(newReal, newImag);
}

public double getReal() {
return real;
}

public double getImag() {
return imag;
}

public String toString() {
if (imag >= 0) {
return real + " + " + imag + "i";
} else {
return real + " - " + (-imag) + "i";
}
}
}

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 z1 = new ComplexNumber(2, 3);
ComplexNumber z2 = new ComplexNumber(1, -4);

ComplexNumber sum = z1.add(z2);
ComplexNumber product = z1.multiply(z2);

System.out.println(z1);
System.out.println(z2);
System.out.println(sum);
System.out.println(product);
}
}
26 changes: 26 additions & 0 deletions task04/src/com/example/task04/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 this.p1;
}
public Point getP2(){
return this.p2;
}

public String toString(){
return p1.toString() + "-----" + p2.toString();
}

public boolean isCollinearLine(Point point){
return (int)this.p1.distance(this.p2) == (int)(point.distance(p1) + point.distance(p2));
}
}
25 changes: 25 additions & 0 deletions task04/src/com/example/task04/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.task04;

public class Point {
private final int x;
private final 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 double distance(Point p){
return Math.sqrt(Math.pow(p.x-x, 2) + Math.pow(p.y-y, 2));
}

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

}
7 changes: 7 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,12 @@
public class Task04Main {
public static void main(String[] args) {

Point p1 = new Point(1,1);
Point p2 = new Point(3,3);

Line l1 = new Line(p1,p2);

System.out.println(l1.isCollinearLine(new Point(2,2)));

}
}
39 changes: 7 additions & 32 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,24 @@
package com.example.task05;

/**
* Точка в двумерном пространстве
*/
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;
}

/**
* Возвращает координату точки по оси абсцисс
*
* @return координату точки по оси X
*/
public double getX() {
// TODO: реализовать
throw new AssertionError();
return x;
}

/**
* Возвращает координату точки по оси ординат
*
* @return координату точки по оси Y
*/
public double getY() {
// TODO: реализовать
throw new AssertionError();
return y;
}

/**
* Подсчитывает расстояние от текущей точки до точки, переданной в качестве параметра
*
* @param point вторая точка отрезка
* @return расстояние от текущей точки до переданной
*/
public double getLength(Point point) {
// TODO: реализовать
throw new AssertionError();
return Math.sqrt(Math.pow(point.getX() - x, 2) + Math.pow(point.getY() - y, 2));
}

}
50 changes: 21 additions & 29 deletions task05/src/com/example/task05/PolygonalLine.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,38 @@
package com.example.task05;

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

public class PolygonalLine {

/**
* Устанавливает точки ломаной линии
*
* @param points массив точек, которыми нужно проинициализировать ломаную линию
*/
private List<Point> points = new ArrayList<>();
public void setPoints(Point[] points) {
// TODO: реализовать
this.points = new ArrayList<>();
for (Point p : points) {
this.points.add(new Point(p.getX(), p.getY()));
}
}

/**
* Добавляет точку к ломаной линии
*
* @param point точка, которую нужно добавить к ломаной
*/

public void addPoint(Point point) {
// TODO: реализовать
points.add(new Point(point.getX(), point.getY()));
}

/**
* Добавляет точку к ломаной линии
*
* @param x координата по оси абсцисс
* @param y координата по оси ординат
*/
public void addPoint(double x, double y) {
// TODO: реализовать
points.add(new Point(x, y));
}

/**
* Возвращает длину ломаной линии
*
* @return длину ломаной линии
*/

public double getLength() {
// TODO: реализовать
throw new AssertionError();
double sum = 0.0;
for(int i = 0; i < points.size()-1; i++) {
sum += getLengthBetweenPoints(points.get(i), points.get(i+1));
}
return sum;
}

private double getLengthBetweenPoints(Point point1, Point point2) {
return Math.sqrt(Math.pow(point2.getX() - point1.getX(), 2) + Math.pow(point2.getY() - point1.getY(), 2));
}

}
9 changes: 9 additions & 0 deletions task05/src/com/example/task05/Task05Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

public class Task05Main {
public static void main(String[] args) {
Point p1 = new Point(1, 0);
Point p2 = new Point(3, 0);
Point p3 = new Point(5, 0);

PolygonalLine polygonalLine = new PolygonalLine();

polygonalLine.setPoints(new Point[]{p1, p2, p3});
System.out.println(polygonalLine.getLength());


}
}