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
22 changes: 21 additions & 1 deletion task01/src/com/example/task01/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,27 @@ public class Point {
int y;

void print() {
String pointToString = String.format("(%d, %d)", x, y);
String pointToString = toString();
System.out.println(pointToString);
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void flip(){
int temp = x;
this.x = -this.y;
this.y = -temp;
}
public double distance(Point point){
double d = Math.sqrt(Math.pow(point.x - this.x,2) + Math.pow(point.y - this.y,2));


return d;

//d = √((x₂ - x₁)² + (y₂ - y₁)²)
}
public String toString(){
return "(" + this.x + "," + this.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();
p1.flip();
p1.print();
System.out.println(p1);
System.out.println("Point 2:");
p2.print();
System.out.println(p2);
}

}
4 changes: 4 additions & 0 deletions task02/src/com/example/task02/Task02Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.example.task02;

import java.sql.Time;

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

TimeSpan timeSpan = new TimeSpan(100,200,400);
System.out.println(timeSpan.toString());
}
}
54 changes: 54 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.example.task02;

public class TimeSpan {
private int hour;
private int minute;
private int second;

private int totalSecond;
public void setHour(int hour) {
this.hour = hour;
}
public void setMinute(int minute) {
this.minute = minute;
}
public void setSecond(int second) {
this.second = second;
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public int getSecond() {
return second;
}
public TimeSpan(int hour, int minute, int second) {
totalSecond = hour*60*60 + minute*60 + second;
formatTimeSpan();
}
private void formatTimeSpan() {

this.hour = totalSecond/3600;
this.minute = (totalSecond%3600)/60;
this.second = (totalSecond%3600)%60;
}
void add(TimeSpan time) {
totalSecond += time.getHour()*60 + time.getMinute()*60 + time.getSecond();
formatTimeSpan();
}
void subtract(TimeSpan time){
totalSecond -= time.getHour()*60 + time.getMinute()*60 + time.getSecond();
formatTimeSpan();
}
public String toString(){

String s = String.format("%s:%s:%s",hour,minute,second);
return s;
}



}

50 changes: 50 additions & 0 deletions task03/src/com/example/task03/ComplexNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.task03;

public class ComplexNumber {

private double real;
private double imaginary;

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

public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;

}
public double getImaginary() {
return imaginary;
}
public void setImaginary(double imaginary) {
this.imaginary = imaginary;

}

public ComplexNumber add(ComplexNumber cnum) {
return new ComplexNumber(real+cnum.getReal(), imaginary+cnum.getImaginary());
}
public ComplexNumber mult(ComplexNumber cnum) {
double realPart = this.real * cnum.real - this.imaginary * cnum.imaginary;
double imagPart = this.real * cnum.imaginary + this.imaginary * cnum.real;
return new ComplexNumber(realPart, imagPart);
}
@Override
public String toString() {
return this.real + " + " + this.imaginary +"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 @@ -3,5 +3,17 @@
public class Task03Main {
public static void main(String[] args) {

ComplexNumber cnum1 = new ComplexNumber(123,123);
ComplexNumber cnum2 = new ComplexNumber(1223,0.123);

System.out.println(cnum1.add(cnum2).toString());
System.out.println(cnum1.mult(cnum2).toString());
System.out.println(cnum1.toString());

}
}

//Создайте класс комплексного числа, описав все необходимые свойства. Подберите понятные имена и правильные типы данных
//Опишите в классе конструктор, позволяющий при создании инициализировать все его свойства.
//Добавьте в класс методы, позволяющие вычислить сумму и произведение комплексного числа с другим комплексным числом. Метод должен возвращать новый объект, не изменяя старое
//Используя построенный класс напишите код, где вы создадите два комплексных числа и посчитаете их сумму и произведение.
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 Line(Point x, Point y) {
this.p1 = x;
this.p2 = y;
}
@Override
public String toString() {
return "Line{" + "p1=" + p1 + ", p2=" + p2 + '}';
}
public Point getP1() {
return p1;
}
public Point getP2() {
return p2;
}
public boolean isCollinearLine(Point p){
//(x - x₁)/(x₂ - x₁) = (y - y₁)/(y₂ - y₁)

return Math.abs((p.getX() - p1.getX()) * (p2.getY() - p1.getY()) - (p.getY() - p1.getY()) * (p2.getX() - p1.getX())) < 1e-10;
}




}
36 changes: 36 additions & 0 deletions task04/src/com/example/task04/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.task04;

/**
* Класс точки на плоскости
*/
public class Point {
final int x;
final int y;

void print() {
String pointToString = toString();
System.out.println(pointToString);
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}

public double distance(Point point){
double d = Math.sqrt(Math.pow(point.x - this.x,2) + Math.pow(point.y - this.y,2));


return d;

//d = √((x₂ - x₁)² + (y₂ - y₁)²)
}
public String toString(){
return "(" + this.x + "," + this.y + ")";
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
}
4 changes: 4 additions & 0 deletions task04/src/com/example/task04/Task04Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
public class Task04Main {
public static void main(String[] args) {

Line line1 = new Line(new Point(0, 0), new Point(0, 2));
Point p1 = new Point(-1, -1);
System.out.println(line1.isCollinearLine(p1));

}
}
19 changes: 11 additions & 8 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
/**
* Точка в двумерном пространстве
*/
public class Point {


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;
}

/**
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 d = Math.sqrt(Math.pow(point.x - this.x,2) + Math.pow(point.y - this.y,2));
return d;
}


}
28 changes: 20 additions & 8 deletions task05/src/com/example/task05/PolygonalLine.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package com.example.task05;

import java.util.ArrayList;

/**
* Ломаная линия
*/
public class PolygonalLine {


public class PolygonalLine {
private final ArrayList<Point> polygonalLine = new ArrayList<Point>();
/**
* Устанавливает точки ломаной линии
*
* @param points массив точек, которыми нужно проинициализировать ломаную линию
* @param polygonalLine массив точек, которыми нужно проинициализировать ломаную линию
*/
public void setPoints(Point[] points) {
// TODO: реализовать
public void setPolygonalLine(Point[] points) {
for (int i = 0; i < points.length; i++) {
addPoint(points[i]);
}

}

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

/**
Expand All @@ -30,7 +37,7 @@ public void addPoint(Point point) {
* @param y координата по оси ординат
*/
public void addPoint(double x, double y) {
// TODO: реализовать
polygonalLine.add(new Point(x, y));
}

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

}
Loading