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

package com.example.task01;

import static java.lang.Math.*;

/**
* Класс точки на плоскости
*/
public class Point {
int x;
int y;
public int x;
public int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}

public Point(){}

public void flip(){
x *= -1;
y *= -1;
int temp = y;
y = x;
x = temp;
}

public double distance(Point point){
double result = sqrt(abs(((double) this.x - point.x) * ((double) this.x - point.x) + ((double) this.y - point.y) * ((double) this.y - point.y)));
return result;
}

void print() {
String pointToString = String.format("(%d, %d)", x, y);
System.out.println(pointToString);
public String toString(){
return String.format("(%d, %d)", String.valueOf(x), String.valueOf(y));
}
}
13 changes: 0 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,5 @@

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);
}
}
103 changes: 103 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,109 @@

public class Task02Main {
public static void main(String[] args) {
TimeSpan time1 = new TimeSpan(20, 10, 2);
TimeSpan time2 = new TimeSpan(40, 60, 3);
time1.add(time2);
}
}

class TimeSpan{
private int second;
private int minute;
private int hours;

public TimeSpan(int second, int minute, int hours){
this.second = second;
this.minute = minute;
this.hours = hours;
checkSecond();
checkMinute();

}

private void checkSecond(){
if (second >= 60){
while (second >= 60){
minute++;
second -= 60;
}
}
}

private void checkMinute(){
if (minute >= 60){
while (minute >= 60){
hours++;
minute -= 60;
}
}
}

public int getSecond(){
return second;
}

public void setSecond(int second){
this.second = second;
}

public int getMinute(){
return minute;
}

public void setMinute(int minute){
this.minute = minute;
}

public int getHours(){
return hours;
}

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

public void add(TimeSpan time){
hours += time.getHours();
minute += time.getMinute();
second += time.getSecond();

checkSecond();
checkMinute();
}

public void subtract(TimeSpan time){
if (second < time.getSecond()){
while (second >= time.getSecond()){
if(minute != 0){
minute--;
second += 60;
}
else System.out.println("Больше минут нет");
}
}
second -= time.getSecond();

if (minute < time.getMinute()){
while (minute >= time.getMinute()){
if(hours != 0){
hours--;
minute += 60;
}
else System.out.println("Больше часов нет");

}
}

if (hours >= time.getHours()){
hours -= time.getHours();
}
else{
System.out.println("Часы невозможно вычислить");
}
}

public String toString(){
return String.format("Часы: %d, минуты: %d, секунды: %d", hours, minute, second);
}
}
38 changes: 38 additions & 0 deletions task03/src/com/example/task03/Task03Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
package com.example.task03;

import java.io.PrintStream;

public class Task03Main {
public static void main(String[] args) {
complexNumber c1 = new complexNumber(3, 2);
complexNumber c2 = new complexNumber(1, 4);

complexNumber resultOfSum = c1.sumOfComplex(c2);
complexNumber resultOfComposition = c1.compositionOfComplex(c2);

System.out.println(resultOfSum.toString());
System.out.println(resultOfComposition.toString());
}
}

class complexNumber{
public double real;
public double imaginary;


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

public complexNumber sumOfComplex(complexNumber c){
complexNumber result = new complexNumber((real + c.real), (imaginary + c.imaginary));
return result;
}
// Суть в том, что при умножении мнимая единица i^2
public complexNumber compositionOfComplex(complexNumber c){
double realPart = real * c.real - imaginary * c.imaginary;
double imaginaryPart = real * c.imaginary + imaginary * c.real;

complexNumber result = new complexNumber(realPart, imaginaryPart);
return result;
}

public String toString(){
return String.format("%f + %f i", real, imaginary);
}

}
61 changes: 61 additions & 0 deletions task04/src/com/example/task04/Task04Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,64 @@ public static void main(String[] args) {

}
}

class Line{
private Point firstPoint;
private Point secondPoint;

public Line(Point p1, Point p2){
firstPoint = p1;
secondPoint = p2;
}

public Point getP1(){
return firstPoint;
}

public Point getP2(){
return secondPoint;
}

public boolean isCollinearLine(Point p){
int x1 = firstPoint.getX();
int y1 = firstPoint.getY();
int x2 = secondPoint.getX();
int y2 = secondPoint.getY();
int x3 = p.getX();
int y3 = p.getY();

// Формула площади треугольника через координаты
// Если площадь = 0, точки лежат на одной прямой
int area = (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1);

return area == 0;

}

public String toString(){
return String.format("Первая точка: %s вторая точка %s", firstPoint.toString(), secondPoint.toString());
}
}

class Point{
private final int x;
private 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);
}

public int getX(){
return x;
}

public int getY(){
return y;
}

}
21 changes: 14 additions & 7 deletions task05/src/com/example/task05/Point.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.task05;

import static java.lang.Math.abs;
import static java.lang.Math.sqrt;

/**
* Точка в двумерном пространстве
*/
Expand All @@ -11,8 +14,11 @@ public class Point {
* @param x координата по оси абсцисс
* @param y координата по оси ординат
*/
private final double x;
private final double y;
public Point(double x, double y) {
throw new AssertionError();
this.x = x;
this.y = y;
}

/**
Expand All @@ -21,8 +27,8 @@ public Point(double x, double y) {
* @return координату точки по оси X
*/
public double getX() {
// TODO: реализовать
throw new AssertionError();
return x;
// throw new AssertionError();
}

/**
Expand All @@ -31,8 +37,8 @@ public double getX() {
* @return координату точки по оси Y
*/
public double getY() {
// TODO: реализовать
throw new AssertionError();
return y;
// throw new AssertionError();
}

/**
Expand All @@ -42,8 +48,9 @@ public double getY() {
* @return расстояние от текущей точки до переданной
*/
public double getLength(Point point) {
// TODO: реализовать
throw new AssertionError();
double result = sqrt(((point.getX() - x) * (point.getX() - x)) + ((point.getY() - y) * (point.getY() - y)));
return result;
// throw new AssertionError();
}

}
Loading