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

public Point(){
x = 0;
y = 0;
}

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){
double distX = Math.pow(x - point.x, 2);
double distY = Math.pow(y - point.y, 2);
return Math.sqrt(distX + distY);
}

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


void print() {
String pointToString = String.format("(%d, %d)", x, y);
System.out.println(pointToString);
Expand Down
7 changes: 6 additions & 1 deletion task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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

TimeSpan time1 = new TimeSpan(353, 255, 1);
TimeSpan time2 = new TimeSpan(600, 120, 2);
time1.add(time2);
System.out.println(time1.toString());
time1.setSeconds(60);
System.out.println(time1.toString());
}
}
66 changes: 66 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.example.task02;

public class TimeSpan {
private int timeSec;
private int timeMin;
private int timeHour;

public TimeSpan(int timeSec, int timeMin, int timeHour){
this.timeSec = timeSec;
this.timeMin = timeMin;
this.timeHour = timeHour;
correctTime();
}

public int getTimeSec(){
return timeSec;
}

public int getTimeMin(){
return timeMin;
}

public int getTimeHour(){
return timeHour;
}

public void setSeconds(int timeSec){
this.timeSec = timeSec;
correctTime();
}

public void setMins(int timeMin){
this.timeMin = timeMin;
correctTime();
}

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

void add(TimeSpan time){
this.timeSec += time.timeSec;
this.timeMin += time.timeMin;
this.timeHour += time.timeHour;
correctTime();
}

void subtract(TimeSpan time){
this.timeSec -= time.timeSec;
this.timeMin -= time.timeMin;
this.timeHour -= time.timeHour;
correctTime();
}

void correctTime(){
int totalSec = timeHour * 3600 + timeMin * 60 + timeSec;
timeHour = totalSec / 3600;
timeMin = (totalSec % 3600) / 60;
timeSec = (totalSec % 3600) % 60;
}
@Override
public String toString(){
return timeHour + ":" + timeMin + ":" + timeSec;
}
}
41 changes: 41 additions & 0 deletions task03/src/com/example/task03/Complex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.task03;

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

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

public double getReal(){
return real;
}

public double getImaginary() {
return imaginary;
}

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

public Complex multiply(Complex other) {
// (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
double newReal = this.real * other.real - this.imaginary * other.imaginary;
double newImaginary = this.real * other.imaginary + this.imaginary * other.real;
return new Complex(newReal, newImaginary);
}

@Override
public String toString() {
if (imaginary >= 0) {
return real + " + " + imaginary + "i";
} else {
return real + " - " + Math.abs(imaginary) + "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) {
Complex num1 = new Complex(3, 2); // 3 + 2i
Complex num2 = new Complex(1, -4); // 1 - 4i

Complex sum = num1.add(num2);
System.out.println("Сумма: " + num1 + " + " + num2 + " = " + sum);

Complex product = num1.multiply(num2);
System.out.println("Произведение: " + num1 + " * " + num2 + " = " + product);
}
}
38 changes: 38 additions & 0 deletions task04/src/com/example/task04/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 double length() {
return p1.distance(p2);
}

public boolean isCollinearLine(Point p) {
// точки коллинеарны, если площадь треугольника равна 0
// Если S = 0, то точки лежат на одной прямой

double area = (p2.getX() - p1.getX()) * (p.getY() - p1.getY())
- (p.getX() - p1.getX()) * (p2.getY() - p1.getY());

return Math.abs(area) < 1e-10;
}

@Override
public String toString() {
return "Line from " + p1 + " to " + p2;
}
}
48 changes: 48 additions & 0 deletions task04/src/com/example/task04/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.task04;

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

public Point(){
x = 0;
y = 0;
}

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

public double getX() {
return x;
}

public double getY() {
return y;
}

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

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Point point = (Point) obj;
return Double.compare(point.x, x) == 0 && Double.compare(point.y, y) == 0;
}

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


void print() {
String pointToString = String.format("(%d, %d)", x, y);
System.out.println(pointToString);
}
}
15 changes: 15 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,21 @@

public class Task04Main {
public static void main(String[] args) {
Point point1 = new Point(0, 0);
Point point2 = new Point(3, 3);
Point point3 = new Point(1, 1);
Point point4 = new Point(1, 2);

Line line = new Line(point1, point2);

System.out.println(line);
System.out.println("Длина отрезка: " + line.length());
System.out.println("Точка " + point3 + " лежит на прямой: " + line.isCollinearLine(point3));
System.out.println("Точка " + point4 + " лежит на прямой: " + line.isCollinearLine(point4));

Point point5 = new Point(2, 2);
Point point6 = new Point(4, 3);
System.out.println("Точка " + point5 + " лежит на прямой: " + line.isCollinearLine(point5));
System.out.println("Точка " + point6 + " лежит на прямой: " + line.isCollinearLine(point6));
}
}
17 changes: 10 additions & 7 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ public class Point {
* @param x координата по оси абсцисс
* @param y координата по оси ординат
*/
private final double x;
private final double y;

public Point(double x, double y) {
throw new AssertionError();
this.x = x;
this.y = y;
}

/**
Expand All @@ -21,8 +25,7 @@ public Point(double x, double y) {
* @return координату точки по оси X
*/
public double getX() {
// TODO: реализовать
throw new AssertionError();
return x;
}

/**
Expand All @@ -31,8 +34,7 @@ public double getX() {
* @return координату точки по оси Y
*/
public double getY() {
// TODO: реализовать
throw new AssertionError();
return y;
}

/**
Expand All @@ -42,8 +44,9 @@ public double getY() {
* @return расстояние от текущей точки до переданной
*/
public double getLength(Point point) {
// TODO: реализовать
throw new AssertionError();
double distX = Math.pow(x - point.x, 2);
double distY = Math.pow(y - point.y, 2);
return Math.sqrt(distX + distY);
}

}
33 changes: 28 additions & 5 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;

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

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

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

/**
Expand All @@ -30,7 +43,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 +52,18 @@ public void addPoint(double x, double y) {
* @return длину ломаной линии
*/
public double getLength() {
// TODO: реализовать
throw new AssertionError();
if (points.size() < 2) {
return 0.0;
}

double totalLength = 0.0;
for (int i = 0; i < points.size() - 1; i++) {
Point current = points.get(i);
Point next = points.get(i + 1);
totalLength += current.getLength(next);
}

return totalLength;
}

}