Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Лабораторная 4, Смирнов ПрИ-202 #607

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
36 changes: 32 additions & 4 deletions task01/src/com/example/task01/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,39 @@
* Класс точки на плоскости
*/
public class Point {
int x;
int y;
private int x;
private int y;

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

// метод void flip(), который будет "вращать" точку относительно начала координат
// на 180 градусов. Для этого нужно поменять знак каждой координаты и поменять их
// местами. Например, точка (5, -7) переходит в (7, -5)
public void flip(){
int temp = x * -1;
x = y * -1;
y = temp;
}

// метод double distance(Point point), который будет принимать в качестве параметра
// объект точки и должен считать расстояние между двумя точками
// (той, для которой вызывается метод, и переданной в качестве аргумента)

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

// метод public String toString(), который должен возвращать
// строковое представление точки, например в формате (x, y)

public String toString(){
return "(" + x + ", " + y + ")";
}

void print() {
String pointToString = String.format("(%d, %d)", x, y);
System.out.println(pointToString);
System.out.println(toString());
}
}
12 changes: 6 additions & 6 deletions task01/src/com/example/task01/Task01Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

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);
// p1.x = 10;
// p1.y = 45;
Point p2 = new Point(78, 12);
// p2.x = 78;
// p2.y = 12;

System.out.println("Point 1:");
p1.print();
Expand Down
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 hours = 0;
private int minutes = 0;
private int seconds = 0;

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


// метод, который должен прибавлять к текущему интервалу
// значение переданного интервала
public void add(TimeSpan time) {
hours += time.GetHours();
minutes += time.GetMinutes();
seconds += time.GetSeconds();
Check();
}
// метод, который должен вычитать из текущего интервала
// значение переданного интервала
public void subtract(TimeSpan time) {
hours -= time.GetHours();
minutes -= time.GetMinutes();
seconds -= time.GetSeconds();
Check();
}

// метод, который должен возвращать строковое представление интервала

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

private void Check(){
while (seconds > 60){
seconds -= 60;
minutes ++;
}
while (minutes > 60){
minutes -= 60;
hours ++;
}

if (seconds < 0){
minutes--;
seconds = 60 + minutes;
}
if (minutes < 0){
hours--;
minutes = 60 + minutes;
}
}

public int GetHours() { return hours; }
public int GetMinutes() { return minutes; }
public int GetSeconds() { return seconds; }

public void SetHours(int hours) { this.hours = hours; }
public void SetMinutes(int minutes) { this.minutes = minutes; }
public void SetSeconds(int seconds) { this.seconds = seconds; }
}
28 changes: 28 additions & 0 deletions task03/src/com/example/task03/ComplexNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.task03;

public class ComplexNumber {
private double real_part;
private double imaginary_part;

public ComplexNumber(double real_part, double imaginary_part){
this.real_part = real_part;
this.imaginary_part = imaginary_part;
}

static ComplexNumber Sum(ComplexNumber num1, ComplexNumber num2){
double real_part = num1.real_part + num2.real_part;
double imaginary_part = num1.imaginary_part + num2.imaginary_part;
return new ComplexNumber(real_part, imaginary_part);
}

// (a1 + b1i) (a2 + b2i) = (a1a2 - b1b2) + (a1b2 - a2b1) i
static ComplexNumber Mul(ComplexNumber num1, ComplexNumber num2){
double real_part = (num1.real_part * num2.real_part) - (num1.imaginary_part * num2.imaginary_part);
double imaginary_part = (num1.real_part * num2.imaginary_part) - (num2.real_part * num1.imaginary_part);
return new ComplexNumber(real_part, imaginary_part);
}

public String ToString(){
return this.real_part + " + " + this.imaginary_part + 'i';
}
}
5 changes: 5 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,11 @@

public class Task03Main {
public static void main(String[] args) {
ComplexNumber a = new ComplexNumber(2, 1);
ComplexNumber b = new ComplexNumber(3, 2);

System.out.println(ComplexNumber.Sum(a,b).ToString()); // 5 + 3i
System.out.println(ComplexNumber.Mul(a,b).ToString()); // 4 + i

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

public class Line {
private Point a; // Начало отрезка
private Point b; // Конец отрезка

public Line(Point p1, Point p2){
a = Point.MinX(p1, p2);
b = Point.MaxX(p1,p2);
}

public String toString(){
return a.toString() + ", " + b.toString();
}

void print() {
System.out.println(toString());
}

public boolean isCollinearLine(Point p){
return p.distance(a) + p.distance(b) == a.distance(b);
}

public Point getP1() {return a;}
public Point getP2() {return b;}
}
39 changes: 39 additions & 0 deletions task04/src/com/example/task04/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.task04;

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

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

public static Point MinX(Point p1, Point p2){
if (p1.GetX() > p2.GetX()) return p2;
else return p1;
}

public static Point MaxX(Point p1, Point p2){
if (p1.GetX() > p2.GetX()) return p1;
else return p2;
}

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

public String toString(){
return "(" + x + ", " + y + ")";
}

void print() {
System.out.println(toString());
}

public int GetX(){return x;}
public int GetY(){return y;}
}
23 changes: 23 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,29 @@

public class Task04Main {
public static void main(String[] args) {
Point a = new Point(0,0);
Point b = new Point(10,10);

Point x = new Point(4, 4);
Point y = new Point(-10, -10);

Line ab = new Line(a, b);
ab.print();


if (ab.isCollinearLine(x)){
System.out.println(x.toString() + " лежит на " + ab.toString());
}
else {
System.out.println(x.toString() + " НЕ лежит на " + ab.toString());
}

if (ab.isCollinearLine(y)){
System.out.println(y.toString() + " лежит на " + ab.toString());
}
else {
System.out.println(y.toString() + " НЕ лежит на " + ab.toString());
}

}
}
23 changes: 11 additions & 12 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,32 @@
* Точка в двумерном пространстве
*/
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();
}
public double getX() { return x; }

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

/**
* Подсчитывает расстояние от текущей точки до точки, переданной в качестве параметра
Expand All @@ -42,8 +38,11 @@ public double getY() {
* @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));
}

public Point clone() {
return new Point(x, y);
}

}
23 changes: 17 additions & 6 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 javax.sound.sampled.Line;
import java.util.LinkedList;
import java.util.List;

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

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

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

/**
Expand All @@ -30,7 +37,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 +46,12 @@ public void addPoint(double x, double y) {
* @return длину ломаной линии
*/
public double getLength() {
// TODO: реализовать
throw new AssertionError();
double length = 0;
if (points.size() > 1){
for (int i = 1; i < points.size(); i++) {
length += points.get(i).getLength(points.get(i - 1));
}
}
return length;
}

}