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
28 changes: 26 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,32 @@ 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 temp = x;
x = -y;
y = -temp;
}

double distance(Point point) {
int xDif = this.x - point.x;
int yDif = this.y - point.y;
return Math.sqrt(xDif * xDif + yDif * yDif);
}

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);
}
}
102 changes: 102 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.example.task02;

import java.sql.Time;
import java.text.MessageFormat;

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

public TimeSpan(int h, int m, int s) {
hours = h;
minutes = m;
seconds = s;
}

public int getHours() {
return hours;
}

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

public int getMinutes() {
return minutes;
}

public void setMinutes(int minutes) {
int h = minutes / 60;
int m = minutes % 60;

hours = h;
minutes = m;
}

public int getSeconds() {
return seconds;
}

public void setSeconds(int seconds) {
int h = seconds / 3600;
int m = seconds % 3600 / 60;
int s = seconds % 3600 % 60;

hours = h;
minutes = m;
seconds = s;
}

private void addAndSeparateMinutes(int m1, int m2) {
hours += (m1 + m2) / 60;
minutes += (m1 + m2) % 60;
}

private void subAndSeparateMinutes(int m1, int m2) {
hours -= (m1 - m2) / 60;
minutes -= (m1 - m2) % 60;
}

public void add(TimeSpan time) {
hours += time.hours;

if (minutes + time.minutes >= 60)
addAndSeparateMinutes(minutes, time.minutes);
else
minutes += time.minutes;

if (seconds + time.seconds >= 60) {
int tempMinutes = time.seconds / 60;
if (minutes + tempMinutes >= 60) {
addAndSeparateMinutes(minutes, tempMinutes);
}
seconds += (seconds + time.seconds) % 60;
}
else
seconds += time.seconds;
}

public void subtract(TimeSpan time) {
hours -= time.hours;

if (minutes - time.minutes < 0)
subAndSeparateMinutes(minutes, time.minutes);
else
minutes -= time.minutes;

if (seconds - time.seconds < 0) {
int tempMinutes = time.seconds / 60;
if (minutes - tempMinutes < 0) {
subAndSeparateMinutes(minutes, tempMinutes);
}
seconds -= (seconds - time.seconds) % 60;
}
else
seconds -= time.seconds;
}

public String toString() {
return MessageFormat.format("Time — {0}h:{1}m:{2}s", hours, minutes, seconds);
}
}
43 changes: 43 additions & 0 deletions task03/src/com/example/task03/ComplexNum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.task03;

import java.text.MessageFormat;

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

public ComplexNum() {
this(0, 0);
}

public ComplexNum(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}

public ComplexNum add(ComplexNum addable) {
return new ComplexNum(this.real + addable.real,
this.imaginary + addable.imaginary);
}

public ComplexNum mult(ComplexNum multipliable) {
double realRes = this.real * multipliable.real - this.imaginary * multipliable.imaginary;
double imaginaryRes = this.real * multipliable.imaginary + this.imaginary * multipliable.real;

return new ComplexNum(realRes, imaginaryRes);
}

public String toString() {
if (imaginary == 0) {
return MessageFormat.format("{0,number,0.00}", real);
} else if (real == 0) {
return MessageFormat.format("{0,number,0.00}i", imaginary);
} else {
String sign = imaginary > 0 ? " + " : " - ";
double absImaginary = Math.abs(imaginary);

return MessageFormat.format("{0,number,0.00}{1}{2,number,0.00}i",
real, sign, absImaginary);
}
}
}
6 changes: 6 additions & 0 deletions task03/src/com/example/task03/Task03Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.example.task03;

import java.text.MessageFormat;

public class Task03Main {
public static void main(String[] args) {
ComplexNum num1 = new ComplexNum(5, -3.6);
ComplexNum num2 = new ComplexNum(1, 2);

System.out.println("Сумма комплексных чисел: " + num1.add(num2));
System.out.println("Произведение комплексных чисел: " + num1.mult(num2));
}
}
63 changes: 63 additions & 0 deletions task04/src/com/example/task04/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.example.task04;

import java.text.MessageFormat;

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

public Line(int p1X, int p1Y, int p2X, int p2Y) {
this.p1 = new Point(p1X, p1Y);
this.p2 = new Point(p2X, p2Y);
}

private class Point {
private final int x;
private final int y;

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

public Point() {
this(0, 0);
}

double distance(Point point) {
int xDif = this.x - point.x;
int yDif = this.y - point.y;
return Math.sqrt(xDif * xDif + yDif * yDif);
}

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

void print() {
System.out.println(this);
}
}

public Point getP1() {
return p1;
}

public Point getP2() {
return p2;
}

public boolean isCollinearLine(Point p) {
int vectorX = p2.x - p1.x;
int vectorY = p2.y - p1.y;

int vectorToP1X = p.x - p1.x;
int vectorToP1Y = p.y - p1.y;

return vectorX * vectorToP1Y - vectorY * vectorToP1X == 0;
}

public String toString() {
return MessageFormat.format("[{0};{1}]", p1, p2);
}
}
16 changes: 9 additions & 7 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 {
private final double x;
private 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,9 @@ public double getY() {
* @return расстояние от текущей точки до переданной
*/
public double getLength(Point point) {
// TODO: реализовать
throw new AssertionError();
double xDif = this.x - point.x;
double yDif = this.y - point.y;
return Math.sqrt(xDif * xDif + yDif * yDif);
}

}
17 changes: 12 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 ArrayList<Point> points = new ArrayList<Point>();

/**
* Устанавливает точки ломаной линии
*
* @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: реализовать
points.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: реализовать
points.add(new Point(x, y));
}

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

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

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

PolygonalLine line = new PolygonalLine();
line.setPoints(new Point[] {
new Point(1,2),
new Point(3,1),
new Point(5,6)
});
line.addPoint(7, 1);
System.out.println("Длина ломаной линии: " + line.getLength());
}
}