Skip to content

Instantly share code, notes, and snippets.

@GlebGomenyuk
Created January 21, 2020 21:18
Show Gist options
  • Save GlebGomenyuk/39e17e54a198c5014fe823e66ccb7a55 to your computer and use it in GitHub Desktop.
Save GlebGomenyuk/39e17e54a198c5014fe823e66ccb7a55 to your computer and use it in GitHub Desktop.
HW2_1
package com;
public class Board {
private Shape[] part = new Shape[4];
public Board() {
super();
// TODO Auto-generated constructor stub
}
public void addShape(Shape shape, int i) {
if (part[i] == null) {
part[i] = shape;
System.out.println("Фигура " + shape + " добавленна в поле " + (i + 1));
} else {
System.out.println("Поле " + (i + 1) + " занято");
}
}
public void delShape(int i) {
part[i] = null;
System.out.println("Поле " + (i + 1) + " очищенно");
}
public void infoShape() {
String shapesName = "";
double area = 0;
for (int i = 0; i < part.length; i++) {
if (part[i] != null) {
shapesName += part[i].getName() + ", ";
area += part[i].getArea();
// shapesName += part[i].getName() + " , ";
}
}
System.out.println("Добавленны фигуры " + shapesName + " их общая площадь = " + Math.round(area));
}
}
package com;
public class Circle extends Shape {
private double perimetr;
Point firstPoint;
Point secondPoint;
public Circle(Point firstPoint, Point secondPoint) {
super();
this.firstPoint = firstPoint;
this.secondPoint = secondPoint;
}
//
public Circle() {
super();
// TODO Auto-generated constructor stub
}
public Point getFirstPoint() {
return firstPoint;
}
public void setFirstPoint(Point firstPoint) {
this.firstPoint = firstPoint;
}
public Point getSecondPoint() {
return secondPoint;
}
public void setSecondPoint(Point secondPoint) {
this.secondPoint = secondPoint;
}
@Override
public String getName() {
return this.getClass().getSimpleName();
}
@Override
public double getPerimetr() {
double perimetr = 2 * Math.PI * Point.calculatR(firstPoint, secondPoint);
return perimetr;
}
@Override
public double getArea() {
double area = Math.PI * Math.pow(Point.calculatR(firstPoint, secondPoint), 2);
return area;
}
@Override
public String toString() {
return "Circle [getPerimetr()=" + getPerimetr() + ", getArea()=" + getArea() + "]";
}
}
package com;
public class Main {
public static void main(String[] args) {
Triangle triangle = new Triangle (new Point(12, 23),new Point(21, 13),new Point(10, 20));
Circle circle = new Circle(new Point(3,7),new Point(5,3));
Rhombus rhombus = new Rhombus(new Point(12, 23),new Point(21, 13),new Point(10, 20),new Point(13,21));
Rectangle rectangle = new Rectangle(new Point(12, 23),new Point(21, 13),new Point(10, 20),new Point(13,21));
System.out.println(triangle);
System.out.println(circle);
System.out.println(rhombus);
System.out.println(rectangle);
Board board = new Board();
board.addShape(triangle, 3);
board.delShape(3);
board.addShape(triangle, 2);
board.addShape(circle, 3);
board.infoShape();
}
}
package com;
public class Point {
private double x;
private double y;
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
public Point() {
super();
}
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 calculat(Point firstPoint, Point seconPoint) {
return Math.sqrt((firstPoint.x - seconPoint.x) * (firstPoint.x - seconPoint.x) + (firstPoint.y - seconPoint.y) * (firstPoint.y - seconPoint.y));
}
public static double calculatR(Point firstPoint, Point seconPoint) {
return Math.sqrt(Math.pow((seconPoint.getX()-firstPoint.getX()), 2)+ Math.pow((seconPoint.getY()-firstPoint.getY()), 2));
}
}
package com;
public class Rectangle extends Shape {
private Point onePoint;
private Point twoPoint;
private Point threePoint;
private Point fourPoint;
public Rectangle() {
super();
}
public Rectangle(Point onePoint, Point twoPoint, Point threePoint, Point fourPoint) {
super();
this.onePoint = onePoint;
this.twoPoint = twoPoint;
this.threePoint = threePoint;
this.fourPoint = fourPoint;
}
public Point getOnePoint() {
return onePoint;
}
public void setOnePoint(Point onePoint) {
this.onePoint = onePoint;
}
public Point getTwoPoint() {
return twoPoint;
}
public void setTwoPoint(Point twoPoint) {
this.twoPoint = twoPoint;
}
public Point getThreePoint() {
return threePoint;
}
public void setThreePoint(Point threePoint) {
this.threePoint = threePoint;
}
public Point getFourPoint() {
return fourPoint;
}
public void setFourPoint(Point fourPoint) {
this.fourPoint = fourPoint;
}
@Override
public String getName() {
return this.getClass().getSimpleName();
}
@Override
public double getPerimetr() {
double perimetr = Point.calculat(onePoint, twoPoint) + Point.calculat(twoPoint, threePoint)+Point.calculat(threePoint, fourPoint)+Point.calculat(threePoint, onePoint);
return perimetr;
}
@Override
public double getArea() {
double area = Point.calculat(onePoint, twoPoint) * Point.calculat(twoPoint, threePoint);
return area;
}
@Override
public String toString() {
return "Rectangle [getPerimetr()=" + getPerimetr() + ", getArea()=" + getArea() + "]";
}
}
package com;
public class Rhombus extends Shape {
private Point onePoint;
private Point twoPoint;
private Point threePoint;
private Point fourPoint;
public Rhombus(Point onePoint, Point twoPoint, Point threePoint, Point fourPoint) {
super();
this.onePoint = onePoint;
this.twoPoint = twoPoint;
this.threePoint = threePoint;
this.fourPoint = fourPoint;
}
public Rhombus() {
super();
// TODO Auto-generated constructor stub
}
public Point getOnePoint() {
return onePoint;
}
public void setOnePoint(Point onePoint) {
this.onePoint = onePoint;
}
public Point getTwoPoint() {
return twoPoint;
}
public void setTwoPoint(Point twoPoint) {
this.twoPoint = twoPoint;
}
public Point getThreePoint() {
return threePoint;
}
public void setThreePoint(Point threePoint) {
this.threePoint = threePoint;
}
public Point getFourPoint() {
return fourPoint;
}
public void setFourPoint(Point fourPoint) {
this.fourPoint = fourPoint;
}
@Override
public String getName() {
return this.getClass().getSimpleName();
}
@Override
public double getPerimetr() {
double perimetr = Point.calculat(onePoint, twoPoint) + Point.calculat(twoPoint, threePoint) + Point.calculat(threePoint, fourPoint)+Point.calculat(fourPoint, onePoint);
return perimetr;
}
@Override
public double getArea() {
double area = Math.pow(Point.calculat(onePoint, twoPoint), 2);
return area;
}
@Override
public String toString() {
return "Rhombus [getPerimetr()=" + getPerimetr() + ", getArea()=" + getArea() + "]";
}
}
package com;
public abstract class Shape {
abstract double getPerimetr();
abstract double getArea();
abstract String getName();
}
package com;
import java.text.DecimalFormat;
public class Triangle extends Shape {
private Point pointOne;
private Point pointTwo;
private Point pointThree;
public Triangle() {
super();
}
public Triangle(Point pointOne, Point pointTwo, Point pointThree) {
super();
this.pointOne = pointOne;
this.pointTwo = pointTwo;
this.pointThree = pointThree;
}
public Point getPointOne() {
return pointOne;
}
public void setPointOne(Point pointOne) {
this.pointOne = pointOne;
}
public Point getPointTwo() {
return pointTwo;
}
public void setPointTwo(Point pointTwo) {
this.pointTwo = pointTwo;
}
public Point getPointThree() {
return pointThree;
}
public void setPointThree(Point pointThree) {
this.pointThree = pointThree;
}
@Override
public String getName() {
return this.getClass().getSimpleName();
}
@Override
public double getPerimetr() {
double perimeter = Point.calculat(pointOne, pointTwo)+ Point.calculat(pointTwo, pointThree) + Point.calculat(pointThree, pointOne);
return perimeter;
}
@Override
public double getArea() {
double area;
double perimetr = this.getPerimetr();
area = Math.sqrt(perimetr * (perimetr - Point.calculat(pointOne, pointTwo)) * (perimetr - Point.calculat(pointTwo, pointThree)) * (perimetr - Point.calculat(pointThree, pointOne)));
return area;
}
@Override
public String toString() {
return "Triangle [getPerimetr()=" + getPerimetr() + ", getArea()=" + getArea() + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment