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
21 changes: 21 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,29 @@ public class Point {
int x;
int y;

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

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

void flip(){
int auxiliaryVariable = x;
x = -y;
y = -auxiliaryVariable;
}

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

public String toString(){
return "("+x+", "+y+")";
}
}
13 changes: 7 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,19 @@

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();
System.out.println(p1.distance(p2));
p1.flip();
System.out.println(p1);

System.out.println("Point 2:");
p2.print();
System.out.println(p2.distance(p1));
p2.flip();
System.out.println(p2);
}
}
6 changes: 6 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,12 @@

public class Task02Main {
public static void main(String[] args) {
TimeSpan time1 = new TimeSpan(12, 35, 0);
TimeSpan time2 = new TimeSpan(0, 452, 67);

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

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

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

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

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

public TimeSpan(int hours,int minutes, int seconds){
setNormalizedTime(hours, minutes, seconds);
}

private void setNormalizedTime(int hours, int minutes, int seconds){
if (hours < 0 || minutes < 0 || seconds < 0)
throw new IllegalArgumentException("Время не может быть отрицательным.");

int totalSeconds = hours*3600 + minutes*60 + seconds;
this.hours = totalSeconds / 3600;
this.minutes = (totalSeconds % 3600)/60;
this.seconds = totalSeconds % 60;
}

void add(TimeSpan time){
setNormalizedTime(hours+this.hours, minutes+this.minutes,seconds+this.seconds);
}

void subtract(TimeSpan time){
if ((time.hours * 3600 + time.minutes * 60 + time.seconds) > (hours * 3600 + minutes * 60 + seconds)){
hours = 0;
minutes = 0;
seconds = 0;
}
else{
setNormalizedTime(hours-this.hours, minutes-this.minutes,seconds-this.seconds); }
}

public String toString(){
return hours + ":" + minutes + ":" + seconds;
}
}
39 changes: 39 additions & 0 deletions task03/src/com/example/task03/Complex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.task03;

public class Complex {
private double realPart;
private double imaginaryPart;

public Complex(double realPart, double imaginaryPart) {
this.realPart = realPart;
this.imaginaryPart = imaginaryPart;
}

public double getRealPart() {
return realPart;
}

public double getImaginaryPart() {
return imaginaryPart;
}

public Complex add(Complex other) {
double newReal = this.realPart + other.realPart;
double newImaginary = this.imaginaryPart + other.imaginaryPart;
return new Complex(newReal, newImaginary);
}

public Complex multiply(Complex other) {
double newReal = this.realPart * other.realPart - this.imaginaryPart * other.imaginaryPart;
double newImaginary = this.realPart * other.imaginaryPart + this.imaginaryPart * other.realPart;
return new Complex(newReal, newImaginary);
}

public String toString() {
if (imaginaryPart >= 0) {
return realPart + " + " + imaginaryPart + "i";
} else {
return realPart + " - " + Math.abs(imaginaryPart) + "i";
}
}
}
12 changes: 12 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,18 @@

public class Task03Main {
public static void main(String[] args) {
Complex number1 = new Complex(3, 2); // 3 + 2i
Complex number2 = new Complex(1, -4); // 1 - 4i

Complex sum = number1.add(number2);
System.out.println("Первое число: " + number1);
System.out.println("Второе число: " + number2);
System.out.println("Сумма: " + sum);

Complex product = number1.multiply(number2);
System.out.println("Произведение: " + product);

System.out.println(number1);
System.out.println(number2);
}
}
30 changes: 30 additions & 0 deletions task04/src/com/example/task04/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 double length() {
return p1.distance(p2);
}

private double distance(Point p) {
return Math.abs((p2.getY() - p1.getY()) * p.getX() - (p2.getX() - p1.getX()) * p.getY() + p2.getX() * p1.getY() - p2.getY() * p1.getX()) / this.length();
}

public boolean isCollinearLine(Point p) {
return distance(p) == 0;
}

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

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

public int getX() {return x; }
public int getY() { return y; }

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

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

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

public String toString(){
return "("+x+", "+y+")";
}
}
6 changes: 6 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,12 @@

public class Task04Main {
public static void main(String[] args) {
Point p1 = new Point(1, 2);
Point p2 = new Point(3, 4);
Point p3 = new Point(2, 3);
Line line = new Line(p1, p2);

System.out.println(line.isCollinearLine(p3));
System.out.println(line.length());
}
}
47 changes: 10 additions & 37 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,22 @@
package com.example.task05;

/**
* Точка в двумерном пространстве
*/
public class Point {
final private double x;
final private 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 координату точки по оси Y
*/
public double getY() {
// TODO: реализовать
throw new AssertionError();
}
public double getX() { return x; }
public double getY() { return y; }

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

public String toString() {
return String.format("("+x+","+y+")");
}
}
63 changes: 33 additions & 30 deletions task05/src/com/example/task05/PolygonalLine.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
package com.example.task05;

/**
* Ломаная линия
*/
public class PolygonalLine {
private Point[] points;

/**
* Устанавливает точки ломаной линии
*
* @param points массив точек, которыми нужно проинициализировать ломаную линию
*/
public void setPoints(Point[] points) {
// TODO: реализовать
if (points == null) { throw new NullPointerException("Массив точек не должен быть null"); }
if (points.length == 0) { throw new IllegalArgumentException("Массив точек не должен быть пустым!"); }

Point[] values = new Point[points.length];
for (int i = 0; i < points.length; i++) {
double x = points[i].getX();
double y = points[i].getY();
values[i] = new Point(x, y);
}

this.points = values;
}

/**
* Добавляет точку к ломаной линии
*
* @param point точка, которую нужно добавить к ломаной
*/
public void addPoint(Point point) {
// TODO: реализовать
if (points == null) {
points = new Point[] {point};
return;
}

Point[] pointsWithAddedPoint = new Point[points.length + 1];
for (int i = 0; i < points.length; i++) {
double x = points[i].getX();
double y = points[i].getY();
pointsWithAddedPoint[i] = new Point(x, y);
}
pointsWithAddedPoint[pointsWithAddedPoint.length - 1] = new Point(point.getX(), point.getY());
points = pointsWithAddedPoint;
}

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

/**
* Возвращает длину ломаной линии
*
* @return длину ломаной линии
*/
public double getLength() {
// TODO: реализовать
throw new AssertionError();
}
double length = 0.0;

for (int i = 1; i < points.length; i++) {
length += points[i].getLength(points[i-1]);
}

return length;
}
}
Loading