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
40 changes: 35 additions & 5 deletions task01/src/com/example/task01/Point.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
package com.example.task01;

import java.text.MessageFormat;

/**
* Класс точки на плоскости
*/
public class Point {
int x;
int y;
private int x;
private 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);
public void flip(){
int temp = x;
x = -y;
y = -temp;
}

public double distance(Point point){
double Xd = Math.abs(point.x - x);
double Yd = Math.abs(point.y - y);

return Math.sqrt(Math.pow(Xd, 2) + Math.pow(Yd, 2));
}

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



}
16 changes: 3 additions & 13 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,8 @@

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;

System.out.println("Point 1:");
p1.print();
System.out.println(p1);
System.out.println("Point 2:");
p2.print();
System.out.println(p2);
Point point = new Point(5, -7);
point.flip();
System.out.println(point);
}
}
5 changes: 4 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,9 @@

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

TimeSpan time1 = new TimeSpan(10, 20, 36);
TimeSpan time2 = new TimeSpan(5, 10, 24);
time1.add(time2);
System.out.println(time1.toString());
}
}
62 changes: 62 additions & 0 deletions task02/src/com/example/task02/TimeSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.example.task02;

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

public int getHours(){
return hours;
}
public int getMinutes(){
return minutes;
}
public int getSeconds(){
return seconds;
}

public void setHours(int hours){
this.hours = hours;
correct();
}
public void setMinutes(int minutes){
this.minutes = minutes;
correct();
}
public void setSeconds(int seconds){
this.seconds = seconds;
correct();
}

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

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

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

public String toString(){
return String.format("%dч %dм %dс", hours, minutes, seconds);
}

private void correct(){
int totalTime = hours* 3600 + minutes * 60 + seconds;
hours = totalTime / 3600;
minutes = (totalTime % 3600) / 60;
seconds = (totalTime % 3600) % 60;
}
}
37 changes: 37 additions & 0 deletions task03/src/com/example/task03/ComplexNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.task03;

public class ComplexNumber {
private final double real;
private final double imaginary;

public double getReal(){
return real;
}
public double getImaginary(){
return imaginary;
}

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

public ComplexNumber add(ComplexNumber complexNumber){
double realPart = real + complexNumber.real;
double imaginaryPart = imaginary + complexNumber.imaginary;
return new ComplexNumber(realPart, imaginaryPart);
}

public ComplexNumber mult(ComplexNumber complexNumber){
double realPart = real * complexNumber.real - imaginary * complexNumber.imaginary;
double imaginaryPart = real * complexNumber.imaginary + imaginary * complexNumber.real;
return new ComplexNumber(realPart, imaginaryPart);
}

public String toString(){
if(imaginary >= 0){
return real + "+" + imaginary + "i";
}
return real + "-" + (-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) {

ComplexNumber number1 = new ComplexNumber(1, 3);
ComplexNumber number2 = new ComplexNumber(1, -4);
System.out.println(number1);
System.out.println(number2);
System.out.println(number1.add(number2));
System.out.println(number1.mult(number2));
}
}
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 Point getP1(){
return p1;
}

public Point getP2(){
return p2;
}

public Line(Point p1, Point p2){
this.p1 = p1;
this.p2 = p2;
}

public boolean isCollinearLine(Point p) {
return (p2.x - p1.x) * (p.y - p1.y) - (p.x - p1.x) * (p2.y - p1.y) == 0;
}

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

import java.text.MessageFormat;

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


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

public double distance(Point point){
double Xd = Math.abs(point.x - x);
double Yd = Math.abs(point.y - y);

return Math.sqrt(Math.pow(Xd, 2) + Math.pow(Yd, 2));
}

public String toString(){
return String.format("(%d,%d)",x,y);
}
}
41 changes: 12 additions & 29 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,29 @@
* Точка в двумерном пространстве
*/
public class Point {
private final double x;
private 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();
double Xd = Math.abs(point.x - x);
double Yd = Math.abs(point.y - y);

return Math.sqrt(Math.pow(Xd, 2) + Math.pow(Yd, 2));
}

}
45 changes: 16 additions & 29 deletions task05/src/com/example/task05/PolygonalLine.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,33 @@
package com.example.task05;

/**
* Ломаная линия
*/

import java.util.ArrayList;

public class PolygonalLine {
private ArrayList<Point> linePoints = new ArrayList<Point>();

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

/**
* Добавляет точку к ломаной линии
*
* @param point точка, которую нужно добавить к ломаной
*/
public void addPoint(Point point) {
// TODO: реализовать
linePoints.add(new Point(point.getX(), point.getY()));
}

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

/**
* Возвращает длину ломаной линии
*
* @return длину ломаной линии
*/
public double getLength() {
// TODO: реализовать
throw new AssertionError();
double length = 0;
for(int i = 0; i < linePoints.size() - 1; i++){
Point p1 = linePoints.get(i);
Point p2 = linePoints.get(i + 1);
length += p1.getLength(p2);
}
return length;
}

}