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
22 changes: 20 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,26 @@ public class Point {
int x;
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((point.x - x) * (point.x - x) + (point.y - y) * (point.y - y));
}

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(this.toString());
}
}
16 changes: 10 additions & 6 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 p2 = new Point();
p2.x = 78;
p2.y = 12;
Point p1 = new Point(0, 3);
Point p2 = new Point(4, 0);


System.out.println("Point 1:");
p1.print();
System.out.println(p1);
System.out.println("Point 2:");
p2.print();
System.out.println(p2);

System.out.println(p1.distance(p2));

p1.flip();
p1.print();

System.out.println(p1.toString());
}
}
4 changes: 4 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,10 @@

public class Task02Main {
public static void main(String[] args) {
TimeSpan test = new TimeSpan(0, 100, 0);

test.subtract(new TimeSpan(2, 0, 0));

System.out.println(test.toString());
}
}
71 changes: 71 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.example.task02;

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

public TimeSpan(int hours, int minutes, int seconds) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
timeFormat();
}

private void timeFormat() {
if (seconds >= 60) {
minutes += seconds / 60;
seconds %= 60;
}

if (minutes >= 60) {
hours += minutes / 60;
minutes %= 60;
}
}

public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
timeFormat();
}

public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
timeFormat();
}

public int getSeconds() {
return seconds;
}
public void setSeconds(int seconds) {
this.seconds = seconds;
timeFormat();
}

public void add(TimeSpan time) {
seconds += time.seconds;
minutes += time.minutes;
hours += time.hours;
timeFormat();
}

public void subtract(TimeSpan time) {
int prog_data = hours * 3600 + minutes * 60 + seconds;
int user_data = time.hours * 3600 + time.minutes * 60 + time.seconds;
int new_time = Math.abs(user_data - prog_data);

hours = new_time / 3600;
minutes = (new_time % 3600) / 60;
seconds = new_time % 60;
}

public String toString() {
return String.format("%d hours, %d minutes, %d seconds", hours, minutes, seconds);
}
}
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;

public class ComplexNumber {
private final double realPart;
private final double imgPart;

public ComplexNumber(double realPart, double imgPart){
this.realPart = realPart;
this.imgPart = imgPart;
}

public double getRealPart(){
return realPart;
}

public double getImgPart(){
return imgPart;
}

public ComplexNumber sumComplexNumber(ComplexNumber complexNumber){
double newRealPart = realPart + complexNumber.realPart;
double newImaginaryPart = imgPart + complexNumber.imgPart;
return new ComplexNumber(newRealPart, newImaginaryPart);
}

public ComplexNumber multiplyComplexNumber(ComplexNumber complexNumber){
double newRealPart = realPart * complexNumber.realPart - imgPart * complexNumber.imgPart;
double newImaginaryPart = realPart * complexNumber.imgPart + complexNumber.realPart * imgPart;
return new ComplexNumber(newRealPart, newImaginaryPart);
}

public String toString(){
if (imgPart < 0){
return realPart + " - " + Math.abs(imgPart) + "i";
}
return realPart + " + " + imgPart + "i";
}
}
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) {
ComplexNumber complexNum1 = new ComplexNumber(2, 3);
ComplexNumber complexNum2 = new ComplexNumber(2, -4);

System.out.println(complexNum1);
System.out.println(complexNum2);

System.out.println(complexNum1.sumComplexNumber(complexNum2));
System.out.println(complexNum1.multiplyComplexNumber(complexNum2));
}
}
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 p1;
}
public Point getP2() {
return p2;
}

public String toString() {
return String.format("p1:(%d, %d) p2:(%d, %d)", p1.x, p1.y, p2.x, p2.y);
}

public boolean isCollinearLine(Point p) {
return (p2.x - p1.x) * (p.y - p1.y) - (p.x - p1.x) * (p2.y - p1.y) == 0;
}
}
26 changes: 26 additions & 0 deletions task04/src/com/example/task04/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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((point.x - x) * (point.x - x) + (point.y - y) * (point.y - y));
}

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

void print() {
System.out.println(this.toString());
}
}
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 @@ -2,6 +2,13 @@

public class Task04Main {
public static void main(String[] args) {
Point p1 = new Point(3,2);
Point p2 = new Point(5,10);
Point p = new Point(4,6);

Line line = new Line(p1, p2);

System.out.println(line);
System.out.println(line.isCollinearLine(p));
}
}
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 {
final double x;
final 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((point.x - x) * (point.x - x) + (point.y - y) * (point.y - y));
}

}
19 changes: 14 additions & 5 deletions task05/src/com/example/task05/PolygonalLine.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.example.task05;
import java.util.ArrayList;

/**
* Ломаная линия
*/
public class PolygonalLine {
private final ArrayList<Point> line = new ArrayList<>();

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

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

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

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

}