Skip to content

Commit

Permalink
Homework2
Browse files Browse the repository at this point in the history
  • Loading branch information
Hnoianko committed Nov 21, 2017
1 parent 0df2b36 commit 5ff0972
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Homework2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Homework2.iml
.idea/
out/
48 changes: 48 additions & 0 deletions Homework2/src/com/gmail/gnoianko/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.gmail.gnoianko;

public class Circle extends Shape {
private Point centr;
private Point circlePoint;
private double radius;

public Circle(Point centr, Point circlePoint) {
this.centr = centr;
this.circlePoint = circlePoint;
radius = Point.getLength(centr, circlePoint);
}

public Point getCentr() {
return centr;
}

public void setCentr(Point centr) {
this.centr = centr;
}

public Point getCirclePoint() {
return circlePoint;
}

public void setCirclePoint(Point circlePoint) {
this.circlePoint = circlePoint;
}

@Override
public String toString() {
return "Circle{" +
"centr=" + centr +
", circlePoint=" + circlePoint +
", radius=" + radius +
'}';
}

@Override
public double calculatePerimetr() {
return 2 * Math.PI * radius;
}

@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
27 changes: 27 additions & 0 deletions Homework2/src/com/gmail/gnoianko/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
1. Создайте абстрактный класс Shape в котором есть два
абстрактных метода double calculateArea() и double calculatePerimetr().
2. Создайте класс Point в котором есть два свойства double x
double y.
3. Создайте классы которые описывают 2
геометрические фигуры (они должны быть подклассами
Shape), при этом они в качестве свойств должны содержать
классы Point.
*/
package com.gmail.gnoianko;

public class Main {
public static void main(String[] args) {
Shape trOne = new Triangle(new Point(0, 1), new Point(3, 5), new Point(8, 4));
System.out.println(trOne);
System.out.println(trOne.calculateArea());
System.out.println(trOne.calculatePerimetr());

System.out.println();

Shape crOne = new Circle(new Point(0, 0), new Point(3, 3));
System.out.println(crOne);
System.out.println(crOne.calculatePerimetr());
System.out.println(crOne.calculateArea());
}
}
42 changes: 42 additions & 0 deletions Homework2/src/com/gmail/gnoianko/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.gmail.gnoianko;

public class Point {
private double x;
private double y;

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

public Point() {
}

public double getX() {
return x;
}

public void setX(double x) {
this.x = x;
}

public double getY() {
return y;
}

public void setY(double y) {
this.y = y;
}

@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}

public static double getLength(Point a, Point b) {
return Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
}
7 changes: 7 additions & 0 deletions Homework2/src/com/gmail/gnoianko/Shape.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gmail.gnoianko;

public abstract class Shape {
abstract double calculatePerimetr();

abstract double calculateArea();
}
76 changes: 76 additions & 0 deletions Homework2/src/com/gmail/gnoianko/Triangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.gmail.gnoianko;

public class Triangle extends Shape {
private Point a;
private Point b;
private Point c;


public Triangle(Point a, Point b, Point c) {
this.a = a;
this.b = b;
this.c = c;
}

public Triangle() {

}

public Point getA() {
return a;
}

public void setA(Point a) {
this.a = a;
}


public Point getB() {
return b;
}

public void setB(Point b) {
this.b = b;
}

public Point getC() {
return c;
}

public void setC(Point c) {
this.c = c;
}

@Override
public String toString() {
return "Triangle{" +
"a=" + a +
", b=" + b +
", c=" + c +
'}';
}

@Override
public double calculatePerimetr() {
if (Point.getLength(a, b) > Point.getLength(b, c) + Point.getLength(c, a)
|| Point.getLength(b, c) > Point.getLength(c, a) + Point.getLength(a, b)
|| Point.getLength(c, a) > Point.getLength(b, c) + Point.getLength(a, b)) {
return -1;
} else {
return Point.getLength(a, b) + Point.getLength(b, c) + Point.getLength(a, c);
}
}

@Override
public double calculateArea() {
double p = this.calculatePerimetr() / 2;
if (Point.getLength(a, b) > Point.getLength(b, c) + Point.getLength(c, a)
|| Point.getLength(b, c) > Point.getLength(c, a) + Point.getLength(a, b)
|| Point.getLength(c, a) > Point.getLength(b, c) + Point.getLength(a, b)) {
return -1;
} else {
return Math.sqrt(p * (p - Point.getLength(a, b)) * (p - Point.getLength(b, c))
* (p - Point.getLength(c, a)));
}
}
}
73 changes: 73 additions & 0 deletions Homework2/src/com/gmail/gnoianko/uml.uml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<Diagram>
<ID>JAVA</ID>
<OriginalElement />
<nodes>
<node x="214.5" y="134.0">com.gmail.gnoianko.Triangle</node>
<node x="36.5" y="134.0">com.gmail.gnoianko.Circle</node>
<node x="144.29642857142858" y="280.0">com.gmail.gnoianko.Main</node>
<node x="273.7297619047619" y="22.0">com.gmail.gnoianko.Shape</node>
<node x="59.06309523809526" y="0.0">com.gmail.gnoianko.Point</node>
</nodes>
<notes />
<edges>
<edge source="com.gmail.gnoianko.Triangle" target="com.gmail.gnoianko.Point">
<point x="0.0" y="-48.0" />
<point x="269.0" y="94.0" />
<point x="164.36309523809527" y="94.0" />
<point x="46.80000000000001" y="37.0" />
</edge>
<edge source="com.gmail.gnoianko.Triangle" target="com.gmail.gnoianko.Shape">
<point x="36.333333333333314" y="-48.0" />
<point x="305.3333333333333" y="114.0" />
<point x="314.7297619047619" y="114.0" />
<point x="0.0" y="15.0" />
</edge>
<edge source="com.gmail.gnoianko.Main" target="com.gmail.gnoianko.Point">
<point x="-28.875" y="-15.0" />
<point x="153.92142857142858" y="260.0" />
<point x="26.0" y="260.0" />
<point x="26.0" y="104.0" />
<point x="70.76309523809526" y="104.0" />
<point x="-46.8" y="37.0" />
</edge>
<edge source="com.gmail.gnoianko.Circle" target="com.gmail.gnoianko.Shape">
<point x="52.66666666666666" y="-48.0" />
<point x="168.16666666666666" y="114.0" />
<point x="287.3964285714286" y="114.0" />
<point x="-27.333333333333314" y="15.0" />
</edge>
<edge source="com.gmail.gnoianko.Main" target="com.gmail.gnoianko.Triangle">
<point x="9.625" y="-15.0" />
<point x="192.42142857142858" y="250.0" />
<point x="269.0" y="250.0" />
<point x="0.0" y="48.0" />
</edge>
<edge source="com.gmail.gnoianko.Main" target="com.gmail.gnoianko.Circle">
<point x="-9.625" y="-15.0" />
<point x="173.17142857142858" y="250.0" />
<point x="115.5" y="250.0" />
<point x="0.0" y="48.0" />
</edge>
<edge source="com.gmail.gnoianko.Circle" target="com.gmail.gnoianko.Point">
<point x="0.0" y="-48.0" />
<point x="115.5" y="114.0" />
<point x="117.56309523809526" y="114.0" />
<point x="0.0" y="37.0" />
</edge>
<edge source="com.gmail.gnoianko.Main" target="com.gmail.gnoianko.Shape">
<point x="28.875" y="-15.0" />
<point x="211.67142857142858" y="260.0" />
<point x="342.06309523809523" y="260.0" />
<point x="27.333333333333314" y="15.0" />
</edge>
</edges>
<settings layout="Hierarchic Group" zoom="0.9" x="157.34615384615375" y="181.84615384615384" />
<SelectedNodes />
<Categories>
<Category>Fields</Category>
</Categories>
<SCOPE>All</SCOPE>
<VISIBILITY>private</VISIBILITY>
</Diagram>

0 comments on commit 5ff0972

Please sign in to comment.