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
30 changes: 27 additions & 3 deletions task01/src/com/example/task01/Point.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
package com.example.task01;

/**
* Класс точки на плоскости
*/

public class Point {
int x;
int y;

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

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

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(distY + distX);
}

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(1, 120, 0);
TimeSpan time2 = new TimeSpan(0, 0, 10);
time1.add(time2);
System.out.println(time1.toString());
time1.setSeconds(60);
System.out.println(time1.toString());
}
}
69 changes: 69 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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;
correctTime();
}

public int getHours() {
return hours;
}

public int getMinutes() {
return minutes;
}

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

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

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


private void correctTime() {
int totalSecond = hours * 3600 + minutes * 60 + seconds;
this.hours = totalSecond / 3600;
this.minutes = (totalSecond % 3600) / 60;
this.seconds = (totalSecond % 3600) % 60;
}


void add(TimeSpan time) {
this.hours += time.hours;
this.minutes += time.minutes;
this.seconds += time.seconds;
correctTime();
}

void subtract(TimeSpan time) {
this.hours -= time.hours;
this.minutes -= time.minutes;
this.seconds -= time.seconds;
correctTime();
}

@Override
public String toString(){
return hours + ":" + minutes + ":" + seconds;
}
}

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 final double real;
private final 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 сNumber) {
double newReal = this.real + сNumber.real;
double newImaginary = this.imaginary + сNumber.imaginary;
return new Complex(newReal, newImaginary);
}

public Complex multiply(Complex сNumber) {
// (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
double newReal = this.real * сNumber.real - this.imaginary * сNumber.imaginary;
double newImaginary = this.real * сNumber.imaginary + this.imaginary * сNumber.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: 6 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,11 @@

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

Complex c1 = new Complex(0, 0);
Complex c2 = new Complex(6, -2);
System.out.println(c1);
System.out.println(c2);
System.out.println(c1.add(c2));
System.out.println(c1.multiply(c1));
}
}
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 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 boolean isCollinearLine(Point p){
return (p2.x - p1.x) * (p.y - p1.y) - (p.x - p1.x) * (p2.y - p1.y) == 0;
}
@Override
public String toString() {
return "Line from " + p1 + " to " + p2;
}

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

public double getX() {
return x;
}

public double getY() {
return y;
}

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

public String toString(){
return String.format("%d, %d", x, y);
}
}
6 changes: 5 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,10 @@

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

Point p1 = new Point(3,5);
Point p2 = new Point(2,4);
Line l = new Line(p1,p2);
System.out.println(l);
System.out.println(l.isCollinearLine(new Point(3,3)));
}
}
30 changes: 10 additions & 20 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,38 +14,26 @@ public class Point {
* @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();
double a = Math.pow(x - point.x, 2);
double b = Math.pow(y - point.y, 2);
return Math.sqrt(a + b);
}

}
21 changes: 14 additions & 7 deletions task05/src/com/example/task05/PolygonalLine.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
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 +22,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 +32,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 +41,13 @@ public void addPoint(double x, double y) {
* @return длину ломаной линии
*/
public double getLength() {
// TODO: реализовать
throw new AssertionError();
double length = 0.0;
for (int i = 0; i < points.size()-1; i++){
Point p1 = points.get(i);
Point p2 = points.get(i+1);
length += p1.getLength(p2);
}
return length;
}

}
7 changes: 6 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,11 @@

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

Point p1 = new Point(0, 0);
Point p2 = new Point(1, 2);
Point p3 = new Point(3, 1);
PolygonalLine polygonalLine = new PolygonalLine();
polygonalLine.setPoints(new Point[]{p1, p2, p3});
System.out.println(polygonalLine.getLength());
}
}