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
20 changes: 20 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,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(Math.pow((point.x - x),2)
+ Math.pow((point.y - y),2));
}

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
4 changes: 2 additions & 2 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

public class Task01Main {
public static void main(String[] args) {
Point p1 = new Point();
Point p1 = new Point(10, 45);
p1.x = 10;
p1.y = 45;
Point p2 = new Point();
Point p2 = new Point(78, 12);
p2.x = 78;
p2.y = 12;

Expand Down
5 changes: 5 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,11 @@

public class Task02Main {
public static void main(String[] args) {
TimeSpan time = new TimeSpan(0,71,80);
System.out.println(time.getSeconds());
System.out.println(time.getMinutes());
System.out.println(time.getHours());

System.out.println(time.toString());
}
}
74 changes: 74 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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;
timeConvert();
}

public int getMinutes(){
return minutes;
}

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

public int getSeconds(){
return seconds;
}

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

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

timeConvert();
}

public void timeConvert(){
while (seconds >= 60) {
minutes += 1;
seconds -= 60;
}

while (minutes >= 60){
hours += 1;
minutes -= 60;
}
}

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

timeConvert();
}

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

timeConvert();
}

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

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

public double getRealPart(){
return realPart;
}

public double getImaginaryPart(){
return imaginaryPart;
}

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

public ComplexNumber sumComplexNum(ComplexNumber complexNum){
double newRealPart = realPart + complexNum.realPart;
double newImaginaryPart = imaginaryPart + complexNum.imaginaryPart;
return new ComplexNumber(newRealPart, newImaginaryPart);
}

public ComplexNumber multiplyComplexNum(ComplexNumber complexNum){
double newRealPart = realPart * complexNum.realPart - imaginaryPart * complexNum.imaginaryPart;
double newImaginaryPart = realPart * complexNum.imaginaryPart + complexNum.realPart * imaginaryPart;
return new ComplexNumber(newRealPart, newImaginaryPart);
}

public String toString(){
if (imaginaryPart < 0){
return realPart + " - " + (-imaginaryPart) + "i";
}

return realPart + " + " + imaginaryPart + "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(1, 3);
ComplexNumber complexNum2 = new ComplexNumber(1, -4);

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

System.out.println(complexNum1.sumComplexNum(complexNum2));
System.out.println(complexNum1.multiplyComplexNum(complexNum2));
}
}
28 changes: 28 additions & 0 deletions task04/src/com/example/task04/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.task04;

public class Line {
final private Point p1;
final 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 String toString(){
return "A" + p1 + " |--------------| " + "B" + p2;
}

public boolean isCollinearLine(Point p){
return (p2.x - p1.x) * (p.y - p1.y) - (p.x - p1.x) * (p2.y - p1.y) == 0;
}
}
15 changes: 15 additions & 0 deletions task04/src/com/example/task04/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 String toString(){
return String.format("(%d,%d)", x, y);
}
}
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));
}
}
38 changes: 8 additions & 30 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,24 @@
* Точка в двумерном пространстве
*/
public class Point {
final double x;
final 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 x;
}

/**
* Возвращает координату точки по оси ординат
*
* @return координату точки по оси Y
*/
public double getY() {
// TODO: реализовать
throw new AssertionError();
return y;
}

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

}
25 changes: 19 additions & 6 deletions task05/src/com/example/task05/PolygonalLine.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.example.task05;

import java.util.ArrayList;
import java.util.List;

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

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

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

/**
Expand All @@ -30,7 +36,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 +45,15 @@ public void addPoint(double x, double y) {
* @return длину ломаной линии
*/
public double getLength() {
// TODO: реализовать
throw new AssertionError();
}
double sumDistance = 0;

for (int i = 1; i < points.size(); i++){
Point p1 = points.get(i-1);
Point p2 = points.get(i);

sumDistance += p1.getLength(p2);
}

return sumDistance;
}
}
16 changes: 16 additions & 0 deletions task05/src/com/example/task05/Task05Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

public class Task05Main {
public static void main(String[] args) {
Point[] points = {
new Point(1,3),
new Point(2,4),
new Point(3,5)
};

PolygonalLine line = new PolygonalLine();

line.setPoints(points);

double sum = line.getLength();
System.out.println(sum);
System.out.println(line);
line.addPoint(new Point(4,6));
System.out.println(line);
line.addPoint(5,7);
System.out.println(line);
}
}