vitalii_hrushyn
8/16/2016 - 3:55 PM

Home Work 2

Home Work 2

package com.gmail.vhrushyn;

public class Triangle extends Shape {

	private Point a = new Point();
	private Point b = new Point();
	private Point c = new Point();

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

	public Triangle() {
		super();
	}

	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 double getPerimetr() {
		double sideA = Math.sqrt(Math.pow(a.getX() - b.getX(), 2) + Math.pow(a.getY() - b.getY(), 2));
		double sideB = Math.sqrt(Math.pow(b.getX() - c.getX(), 2) + Math.pow(b.getY() - c.getY(), 2));
		double sideC = Math.sqrt(Math.pow(a.getX() - c.getX(), 2) + Math.pow(a.getY() - c.getY(), 2));
		return sideA + sideB + sideC;
	}

	@Override
	public double getArea() {
		double sideA = Math.sqrt(Math.pow(a.getX() - b.getX(), 2) + Math.pow(a.getY() - b.getY(), 2));
		double sideB = Math.sqrt(Math.pow(b.getX() - c.getX(), 2) + Math.pow(b.getY() - c.getY(), 2));
		double sideC = Math.sqrt(Math.pow(a.getX() - c.getX(), 2) + Math.pow(a.getY() - c.getY(), 2));
		double p = sideA + sideB + sideC;
		double area = Math.sqrt((p / 2) * ((p / 2) - sideA) * ((p / 2) - sideB) * ((p / 2) - sideC));
		return area;
	}

	@Override
	public String toString() {
		return "Triangle [a:" + a + ", b:" + b + ", c:" + c + "]";
	}

}
package com.gmail.vhrushyn;

// do not check are points placed right

public class Square extends Shape {

	private Point a = new Point();
	private Point b = new Point();
	private Point c = new Point();
	private Point d = new Point();

	public Square(Point a, Point b, Point c, Point d) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
		this.d = d;
	}

	public Square() {
		super();
	}

	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;
	}

	public Point getD() {
		return d;
	}

	public void setD(Point d) {
		this.d = d;
	}

	@Override
	public String toString() {
		return "Square [a:" + a + ", b:" + b + ", c:" + c + ", d:" + d + "]";
	}

	@Override
	public double getPerimetr() {
		// if the sides may not parallel to the axes
		double sideA = Math.sqrt(Math.pow(a.getX() - b.getX(), 2) + Math.pow(a.getY() - b.getY(), 2));
		return sideA * 4;
	}

	@Override
	public double getArea() {
		double side = Math.sqrt(Math.pow(a.getX() - b.getX(), 2) + Math.pow(a.getY() - b.getY(), 2));
		return Math.pow(side, 2);
	}

}
package com.gmail.vhrushyn;

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 " x=" + x + ", y=" + y + " ";
	}

}
package com.gmail.vhrushyn;

//do not check are points placed right

public class Parallelepiped extends Shape {

	private Point a = new Point();
	private Point b = new Point();
	private Point c = new Point();
	private Point d = new Point();

	public Parallelepiped(Point a, Point b, Point c, Point d) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
		this.d = d;
	}

	public Parallelepiped() {
		super();
	}

	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;
	}

	public Point getD() {
		return d;
	}

	public void setD(Point d) {
		this.d = d;
	}

	@Override
	public String toString() {
		return "Parallelepiped [a:" + a + ", b:" + b + ", c:" + c + ", d:" + d + "]";
	}

	@Override
	public double getPerimetr() {
		// if the sides may not parallel to the axes
		double sideA = Math.sqrt(Math.pow(a.getX() - b.getX(), 2) + Math.pow(a.getY() - b.getY(), 2));
		double sideB = Math.sqrt(Math.pow(b.getX() - c.getX(), 2) + Math.pow(b.getY() - c.getY(), 2));
		double sideC = Math.sqrt(Math.pow(d.getX() - c.getX(), 2) + Math.pow(d.getY() - c.getY(), 2));
		double sideD = Math.sqrt(Math.pow(a.getX() - d.getX(), 2) + Math.pow(a.getY() - d.getY(), 2));
		return sideA + sideB + sideC + sideD;
	}

	@Override
	public double getArea() {
		double sideA = Math.sqrt(Math.pow(a.getX() - b.getX(), 2) + Math.pow(a.getY() - b.getY(), 2));
		double sideB = Math.sqrt(Math.pow(b.getX() - c.getX(), 2) + Math.pow(b.getY() - c.getY(), 2));
		return sideA * sideB;
	}

}
package com.gmail.vhrushyn;

// testing all just run this class

public class Main {

	public static void main(String[] args) {
		// circle poins
		Point aC = new Point(0, 0);
		Point bC = new Point(3, 4);
		// triangle point
		Point aT = new Point(1, 2);
		Point bT = new Point(3, 4);
		Point cT = new Point(6, 1);
		// square point
		Point aS = new Point(1, 1);
		Point bS = new Point(1, 4);
		Point cS = new Point(4, 4);
		Point dS = new Point(4, 1);
		// parallelepiped points
		Point aP = new Point(1, 1);
		Point bP = new Point(1, 4);
		Point cP = new Point(5, 4);
		Point dP = new Point(5, 1);

		Circle circle = new Circle(aC, bC);
		Triangle triangle = new Triangle(aT, bT, cT);
		Square square = new Square(aS, bS, cS, dS);
		Parallelepiped parall = new Parallelepiped(aP, bP, cP, dP);
		Board board = new Board("myBoard");

		System.out.println("...creating shapes..." + System.lineSeparator());

		System.out.println(circle);
		System.out.println("area: " + circle.getArea());
		System.out.println("perimetr: " + circle.getPerimetr());

		System.out.println(System.lineSeparator());

		System.out.println(triangle);
		System.out.println("area: " + triangle.getArea());
		System.out.println("perimetr: " + triangle.getPerimetr());

		System.out.println(System.lineSeparator());

		System.out.println(square);
		System.out.println("area: " + square.getArea());
		System.out.println("perimetr: " + square.getPerimetr());

		System.out.println(System.lineSeparator());

		System.out.println(parall);
		System.out.println("area: " + parall.getArea());
		System.out.println("perimetr: " + parall.getPerimetr());

		System.out.println(System.lineSeparator());

		System.out.println("...creating free board..." + System.lineSeparator());

		board.showBoard();
		System.out.println(System.lineSeparator());

		System.out.println("...add circle and triangle to the board..." + System.lineSeparator());
		board.addShape(circle);
		board.addShape(triangle);

		board.showBoard();
		System.out.println(System.lineSeparator());

		System.out.println("...add another two shapes..." + System.lineSeparator());
		board.addShape(square);
		board.addShape(parall);

		board.showBoard();
		System.out.println(System.lineSeparator());

		System.out.println("...trying to add one more shape..." + System.lineSeparator());
		board.addShape(square);
		System.out.println(System.lineSeparator());

		System.out.println("...delete 3 shapes..." + System.lineSeparator());
		board.delShape(3);

		board.showBoard();
		System.out.println(System.lineSeparator());

		System.out
				.println("...try to delete more shapes (3 again) then placed at the board..." + System.lineSeparator());
		board.delShape(3);

		board.showBoard();
		System.out.println(System.lineSeparator());

		System.out.println("...try to delete shapes when the board is free..." + System.lineSeparator());
		board.delShape(3);

		board.showBoard();
	}
}
package com.gmail.vhrushyn;

public class Circle extends Shape {

	private Point centre;
	private Point radiusPoint;

	public Circle(Point centre, Point radiusPoint) {
		super();
		this.centre = centre;
		this.radiusPoint = radiusPoint;
	}

	public Circle() {
		super();
	}

	public Point getCentre() {
		return centre;
	}

	public void setCentre(Point centre) {
		this.centre = centre;
	}

	public Point getRadiusPoint() {
		return radiusPoint;
	}

	public void setRadiusPoint(Point radiusPoint) {
		this.radiusPoint = radiusPoint;
	}

	@Override
	public double getPerimetr() {
		double perimetr;
		double r = Math.sqrt(
				Math.pow(radiusPoint.getX() - centre.getX(), 2) + Math.pow(radiusPoint.getY() - centre.getY(), 2));
		perimetr = 2 * Math.PI * r;
		return perimetr;
	}

	@Override
	public double getArea() {
		double area;
		double r = Math.sqrt(
				Math.pow(radiusPoint.getX() - centre.getX(), 2) + Math.pow(radiusPoint.getY() - centre.getY(), 2));
		area = Math.PI * Math.pow(r, 2);
		return area;
	}

	@Override
	public String toString() {
		return "Circle [centre:" + centre + ", radiusPoint:" + radiusPoint + "]";
	}

}
package com.gmail.vhrushyn;

import java.util.Arrays;

// boar organized as a stack

public class Board {

	private String boardName;
	private Shape[] cells = new Shape[4];
	private int id = -1;

	public Board(String boardName) {
		super();
		this.boardName = boardName;

	}

	public Board() {
		super();
	}

	@Override
	public String toString() {
		return "Board [boardName=" + boardName + ", cells=" + Arrays.toString(cells) + "]";
	}

	public String getBoardName() {
		return boardName;
	}

	public void setBoardName(String boardName) {
		this.boardName = boardName;
	}

	// adding shape to the board
	public void addShape(Shape shape) {
		id++;
		if (id < 4) {

			cells[id] = shape;
		} else {
			id--;
			System.out.println("no any more free part at the board");
		}
	}

	// delete shapes from the board
	public void delShape(int quantity) {
		int i = 0;
		for (; quantity > 0; quantity--, i++) {
			if (i <= id + 1 && id >= 0) {
				cells[id] = null;
				id--;
			} else {
				break;
			}
		}
		if (i == 0) {
			System.out.println("parts of board are already free");
		} else {
			System.out.println(i + " part(s) of the board was cleaned");
		}

	}

	// information about shapes at the board and they total area
	public void showBoard() {
		double totalArea = 0;
		String occupation;
		for (int i = 0; i < cells.length; i++) {
			if (cells[i] == null) {
				occupation = "free";
			} else {
				occupation = cells[i].toString();
			}
			System.out.println("board part # " + (i + 1) + ": " + occupation);
			if (cells[i] != null) {
				totalArea += cells[i].getArea();
			}
		}
		System.out.println("Total area is " + totalArea);
	}

}