Skip to content

Instantly share code, notes, and snippets.

@kryvoboker
Last active June 12, 2020 19:52
Show Gist options
  • Save kryvoboker/58b6d43b728c339bc93fe287a22c92f1 to your computer and use it in GitHub Desktop.
Save kryvoboker/58b6d43b728c339bc93fe287a22c92f1 to your computer and use it in GitHub Desktop.
HomeWork(OOP)Наследование
public abstract class Shape {
public abstract double getPerimetr();
public abstract double getArea();
}
public class Point {
private double x;
private double y;
public Point(double x, double y) {
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 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() {
super();
}
@Override
public double getPerimetr() {
double aB = (Math.sqrt((a.getX() * a.getX()) + (a.getY() + a.getY())));
double aC = (Math.sqrt((b.getX() * b.getX()) + (b.getY() + b.getY())));
double bC = (Math.sqrt((c.getX() * c.getX()) + (c.getY() + c.getY())));
return aB + aC + bC;
}
@Override
public double getArea() {
double aB = (Math.sqrt((a.getX() * a.getX()) + (a.getY() + a.getY())));
double aC = (Math.sqrt((b.getX() * b.getX()) + (b.getY() + b.getY())));
double bC = (Math.sqrt((c.getX() * c.getX()) + (c.getY() + c.getY())));
double poluP = (aB + aC + bC) / 2;
return Math.sqrt(poluP * (poluP - aB) * (poluP - aC) * (poluP - bC));
}
@Override
public String toString() {
return "Triangle{" + "a=" + a + ", b=" + b + ", c=" + c + '}';
}
}
public class Circle extends Shape {
private Point a;
private Point b;
public Circle(Point a, Point b) {
this.a = a;
this.b = b;
}
public Circle() {
super();
}
@Override
public double getPerimetr() {
double radius = Math.sqrt(((b.getX() * b.getX()) - (a.getX() * a.getX())) + ((b.getY() + b.getY()) - (a.getY() * a.getY())));
return 2 * Math.PI * radius;
}
@Override
public double getArea() {
double radius = Math.sqrt(((b.getX() * b.getX()) - (a.getX() * a.getX())) + ((b.getY() + b.getY()) - (a.getY() * a.getY())));
return Math.PI * Math.pow(radius, 2);
}
@Override
public String toString() {
return "Circle{" + "a=" + a + ", b=" + b + '}';
}
}
public class Square extends Shape {
private Point a;
private Point b;
private Point c;
public Square(Point a, Point b, Point c) {
this.a = a;
this.b = b;
this.c = c;
}
public Square() {
super();
}
@Override
public double getPerimetr() {
double aB = (Math.sqrt((a.getX() * a.getX()) + (a.getY() + a.getY())));
double aC = (Math.sqrt((b.getX() * b.getX()) + (b.getY() + b.getY())));
double cD = aB;
double bD = aC;
return aB + aC + cD + bD;
}
@Override
public double getArea() {
double aB = (Math.sqrt((a.getX() * a.getX()) + (a.getY() + a.getY())));
double aC = (Math.sqrt((b.getX() * b.getX()) + (b.getY() + b.getY())));
double cD = aB;
double bD = aC;
return aB * aC;
}
@Override
public String toString() {
return "Square{" + "a=" + a + ", b=" + b + ", c=" + c + '}';
}
}
import java.util.Arrays;
/**
*
* @author kamaz
*/
public class Board {
private Shape[] dc = new Shape[4];
public Board(Shape[] dc) {
this.dc = dc;
}
public Board() {
super();
}
public Shape[] getDc() {
return dc;
}
public void setDc(Shape[] dc) {
this.dc = dc;
}
public void setFigure(Shape sp, int index) {
for(int i = 0; i < dc.length; i++) {
if((i == index) && (dc[i] == null)) {
dc[i] = sp;
System.out.println("Figure: " + sp + " in index: " + i );
}
else if ((i == index) && (dc[i] != null)){
System.out.println(i + " ячейка занята другой фигурой");
}
}
}
public void deleteFigure(Shape sp, int index) {
for(int i = 0; i < dc.length; i++) {
if((i == index) && (dc[i] != null)) {
dc[i] = null;
System.out.println("Фигура удалена из ячейки");
} else if ((i == index) && (dc[i] == null)) {
System.out.println("Удалять нечего");
}
}
}
public void setAllFigures(Shape sp) {
for(int i = 0; i < dc.length; i++) {
if(dc[i] == null) {
dc[i] = sp;
if(i == 3) {
System.out.println("Ячейки заполнены фигурами");
}
break;
}
}
}
public void deleteAllFifures() {
for(int i = 0; i < dc.length; i++) {
if(dc[i] != null) {
dc[i] = null;
if(i == 3) {
System.out.println("Фигуры удалены");
}
}
}
}
public void getInfoFigure() {
for(int i = 0; i < dc.length; i++) {
if(dc[i] != null) {
System.out.println(dc[i] + " размещена в ячейке " + i);
System.out.println("Площадь фигуры: " + dc[i].getArea());
System.out.println("Периметр фигуры: " + dc[i].getPerimetr());
}
}
}
public void getSummPerimetr() {
double n = 0;
for(int i = 0; i < dc.length; i++) {
if(dc[i] != null) {
n += dc[i].getPerimetr();
}
}
System.out.println("Сумма периметров: " + n);
}
public void getSummArea() {
double n = 0;
for(int i = 0; i < dc.length; i++) {
if(dc[i] != null) {
n += dc[i].getArea();
}
}
System.out.println("Сумма площадей: " + n);
}
@Override
public String toString() {
return "Board{" + "dc=" + Arrays.toString(dc) + '}';
}
}
public class Main {
public static void main(String[] args) {
Point a = new Point(5, 3);
Point b = new Point(6, 6);
Point c = new Point(2, 7);
Triangle f = new Triangle(a, b, c);
Circle g = new Circle(a, b);
Square h = new Square(a, b, c);
System.out.println(h.getArea());
System.out.println(h.getPerimetr());
System.out.println(h);
System.out.println("---------------");
System.out.println(g.getArea());
System.out.println(g.getPerimetr());
System.out.println(g);
System.out.println("---------------");
System.out.println(f.getPerimetr());
System.out.println(f.getArea());
System.out.println(f);
System.out.println("---------------");
Board br = new Board();
br.setFigure(f, 1);
br.setFigure(g, 0);
System.out.println(br);
System.out.println("---------------ff");
Board dr = new Board();
dr.setAllFigures(h);
dr.setAllFigures(f);
dr.setAllFigures(g);
dr.setAllFigures(h);
System.out.println(dr);
System.out.println("---------------");
dr.getInfoFigure();
System.out.println(dr);
System.out.println("---------------");
dr.getSummArea();
System.out.println(dr);
System.out.println("---------------");
dr.getSummPerimetr();
System.out.println(dr);
System.out.println("---------------");
dr.deleteAllFifures();
dr.deleteAllFifures();
dr.deleteAllFifures();
dr.deleteAllFifures();
System.out.println(dr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment