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
19 changes: 19 additions & 0 deletions task01/src/com/example/task01/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,27 @@ public class Point {
int x;
int y;

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

void flip() {
int x1 = x;
x = -1 * y;
y = -1 * x1;
}

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

void print() {
String pointToString = String.format("(%d, %d)", x, y);
System.out.println(pointToString);
}

public String toString() {
return String.format("(%d, %d)", x, y);
}
}
8 changes: 2 additions & 6 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

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

System.out.println("Point 1:");
p1.print();
Expand Down
5 changes: 3 additions & 2 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

TimeSpan time01 = new TimeSpan(2, 40, 15);
System.out.println(time01);
}
}
}
63 changes: 63 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.example.task02;

public class TimeSpan {
private int hour;
private int min;
private int sec;

public int getHour() {
return this.hour;
}
public void setHour(int hour) {
if (hour >= 0) {
this.hour = hour;
}
}

public int getMin() {
return this.min;
}
public void setMin(int min) {
if (min >= 0) {
this.hour += min / 60;
this.min = min % 60;
}
}

public int getSec() {
return this.sec;
}
public void setSec(int sec) {
if (sec >= 0) {
this.hour += (sec / 3600) + ((this.min + (sec % 3600) / 60) / 60);
this.min = (this.min + (sec % 3600) / 60) % 60;
this.sec = sec % 60;
}
}

public TimeSpan(int hour, int min, int sec) {
if (hour >= 0 && min >= 0 && sec >= 0) {
setHour(hour);
setMin(min);
setSec(sec);
}

}

void add(TimeSpan time) {
setHour(hour + time.hour);
setMin(min + time.min);
setSec(sec + time.sec);
}

void subtract(TimeSpan time) {
int allTimeInSec = (sec + min * 60 + hour * 3600) - (time.sec + time.min * 60 + time.hour * 3600);
setHour(0);
setMin(0);
setSec(Math.max(0, allTimeInSec));
}

public String toString() {
return String.format("%d:%d:%d", hour, min, sec);
}
}
23 changes: 23 additions & 0 deletions task03/src/com/example/task03/ComplexNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 sum(ComplexNumber number) {
return new ComplexNumber(real + number.real, imaginary + number.imaginary);
}

public ComplexNumber product(ComplexNumber number) {
return new ComplexNumber(real * number.real - imaginary * number.imaginary, real * number.imaginary + number.real * imaginary);
}

public String toString() {
return String.format("%.1f + %.1fi", real, imaginary);
}
}
6 changes: 5 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,10 @@

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

ComplexNumber number1 = new ComplexNumber(2, 3);
ComplexNumber number2 = new ComplexNumber(4, 5);
System.out.println("Number 1: " + number1 + "\nNumber 2: " + number2);
System.out.println("\nSum: " + number1.sum(number2));
System.out.println("Product: " + number1.product(number2));
}
}
27 changes: 27 additions & 0 deletions task04/src/com/example/task04/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.task04;

public class Line {
private Point p1;
private Point p2;

public Point getP1() {
return p1;
}

public Point getP2() {
return p2;
}

public Line(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}

public boolean isCollinearLine(Point p) {
return p1.distance(p2) == p1.distance(p) + p2.distance(p);
}

public String toString() {
return String.format("[(%d,%d), (%d,%d)]", p1.x, p1.y, p2.x, p2.y);
}
}
27 changes: 27 additions & 0 deletions task04/src/com/example/task04/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.task04;

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

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

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

void print() {
String pointToString = String.format("(%d, %d)", x, y);
System.out.println(pointToString);
}

public String toString() {
return String.format("(%d, %d)", x, y);
}
}
7 changes: 6 additions & 1 deletion task04/src/com/example/task04/Task04Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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

Point p1 = new Point(2, 1);
Point p2 = new Point(6, 3);
Line line = new Line(p1, p2);
System.out.println("Line: " + line);
Point p = new Point(4, 2);
System.out.println(line.isCollinearLine(p));
}
}
36 changes: 7 additions & 29 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,24 @@
* Точка в двумерном пространстве
*/
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(x - point.x, 2) + Math.pow(y - point.y, 2));
}

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

import java.util.ArrayList;
import java.util.List;

/**
* Ломаная линия
*/
public class PolygonalLine {
private List<Point> points;

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

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

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

/**
Expand All @@ -30,7 +41,7 @@ public void addPoint(Point point) {
* @param y координата по оси ординат
*/
public void addPoint(double x, double y) {
// TODO: реализовать
this.points.add(new Point(x, y));
}

/**
Expand All @@ -39,8 +50,10 @@ public void addPoint(double x, double y) {
* @return длину ломаной линии
*/
public double getLength() {
// TODO: реализовать
throw new AssertionError();
double length = 0.0;
for (int i = 1; i < points.size(); i++) {
length += points.get(i - 1).getLength(points.get(i));
}
return length;
}

}
}