Skip to content

Instantly share code, notes, and snippets.

@Banafasto
Created January 21, 2017 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Banafasto/f9496a72904328baf4998931983ae09f to your computer and use it in GitHub Desktop.
Save Banafasto/f9496a72904328baf4998931983ae09f to your computer and use it in GitHub Desktop.
JavaOOPHomeWork2
package com.gmail.kudr641.example1;
import java.util.Scanner;
public class Board {
private Shape[] arrayShape = new Shape[4];
private Scanner scanner = new Scanner(System.in);
public void action(){
System.out.println("Select an action");
System.out.println("1 - Create figure");
System.out.println("2 - Delete figure");
System.out.println("3 - Print result");
System.out.println("4 - Stop");
int i = scanner.nextInt();
if(1 > i || i > 4){
System.out.println("Options are not suitable!");
System.out.println("Try it again!");
action();
}else{
switch(i){
case 1: putShape();
action();
break;
case 2: deleteShape();
action();
break;
case 3: printResult();
action();
break;
case 4: printResult();
break;
}
}
}
private Shape selecShape() {
System.out.println("Select shape:");
System.out.println("1 - Circle");
System.out.println("2 - Triangle");
System.out.println("3 - Square");
System.out.println("4 - Not create");
int i = scanner.nextInt();
switch (i) {
case 1:
return (Shape) new Сircle().createCircle();
case 2:
return (Shape) new Triangle().createTriangle();
case 3:
return (Shape) new Square().createSquare();
case 4:
return null;
default:
System.out.println("Options are not suitable!");
System.out.println("Try it again!");
return selecShape();
}
}
private void putShape(){
System.out.println("where to put a figure?");
System.out.println("1 - at the top of the left");
System.out.println("2 - at the top of the right");
System.out.println("3 - in the lower left corner");
System.out.println("4 - in the lower right corner");
int i = scanner.nextInt();
if(1 > i || i > 4){
System.out.println("Options are not suitable!");
System.out.println("Try it again!");
putShape();
}
switch(i){
case 1:if(arrayShape[0] == null){
arrayShape[0] = selecShape();
}else{
System.out.println("Busy place");
putShape();
}
break;
case 2: if(arrayShape[1] == null){
arrayShape[1] = selecShape();
}else{
System.out.println("Busy place");
putShape();
}
break;
case 3: if(arrayShape[2] == null){
arrayShape[2] = selecShape();
}else{
System.out.println("Busy place");
putShape();
}
break;
case 4:if(arrayShape[3] == null){
arrayShape[3] = selecShape();
}else{
System.out.println("Busy place");
putShape();
}
break;
}
}
private void deleteShape(){
System.out.println("where to delet a figure?");
System.out.println("1 - at the top of the left");
System.out.println("2 - at the top of the right");
System.out.println("3 - in the lower left corner");
System.out.println("4 - in the lower right corner");
int i = scanner.nextInt();
if(i < 1 || i > 4){
System.out.println("Options are not suitable!");
System.out.println("Try it again!");
putShape();
}
switch(i){
case 1: if(arrayShape[0] != null){
System.out.println("Figure delet!");
arrayShape[0] = null;
}else{
System.out.println("There is already too empty!");
}
break;
case 2: if(arrayShape[1] != null){
System.out.println("Figure delet!");
arrayShape[1] = null;
}else{
System.out.println("There is already too empty!");
}
break;
case 3: if(arrayShape[2] != null){
System.out.println("Figure delet!");
arrayShape[2] = null;
}else{
System.out.println("There is already too empty!");
}
break;
case 4: if(arrayShape[3] != null){
System.out.println("Figure delet!");
arrayShape[3] = null;
}else{
System.out.println("There is already too empty!");
}
break;
}
}
private void printResult(){
String[] arrayString = {"At the top of the left:", "At the top of the right:",
"In the lower left corner:", "In in the lower right corner:" };
double sumPerimeter = 0;
double sumArea = 0;
for(int i = 0; i < arrayShape.length; i ++){
System.out.println(arrayString[i]);
if(arrayShape[i] == null){
System.out.println("It's empty");
}else{
System.out.println(arrayString[i] + arrayShape[i] +
" perimeter:" + arrayShape[i].getPerimeter() +
" area: " + arrayShape[i].getArea());
sumPerimeter += arrayShape[i].getPerimeter();
sumArea += arrayShape[i].getArea();
}
}
System.out.println("Sum perimeter: " + sumPerimeter);
System.out.println("Sum Area: " + sumArea);
}
}
package com.gmail.kudr641.example1;
import java.util.Scanner;
public class Сircle extends Shape{
private Point one;
private Point two;
private double radius;
private Scanner scanner;
public Сircle(Point one, Point two, double radius){
this.one = one;
this.two = two;
this.radius = radius;
}
public Сircle(){
}
public Сircle createCircle(){
scanner = new Scanner(System.in);
Point center = new Point();
Point side = new Point();
System.out.print("Input x for center:");
center.setX(scanner.nextDouble());
System.out.print("Imput y for center:");
center.setY(scanner.nextDouble());
System.out.print("Input x for side:");
side.setX(scanner.nextDouble());
System.out.print("Input y for side:");
side.setY(scanner.nextDouble());
return new Сircle(center, side, getLength(center, side));
}
@Override
public String toString() {
return "Сircle [one=" + one +
", two=" + two +
", radius=" + radius + "]";
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
}
package com.gmail.kudr641.example1;
public class Point {
private double x;
private double y;
public Point(){
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
package com.gmail.kudr641.example1;
public class Runner {
public static void main(String... args){
Board board = new Board();
board.action();
}
}
package com.gmail.kudr641.example1;
public abstract class Shape {
public abstract double getPerimeter();
public abstract double getArea();
public double getLength(Point one, Point two){
return Math.sqrt(Math.pow(two.getX()
- one.getX(), 2) + Math.pow(two.getY() - one.getY(), 2));
}
}
package com.gmail.kudr641.example1;
import java.util.Scanner;
public class Square extends Shape {
private Point onePoint;
private Point twoPoint;
private Point threePoint;
private Point fourPoint;
private double side;
private Scanner scanner;
public Square(Point onePoint, Point twoPoint, Point threePoint, Point fourPoint, double side){
this.onePoint = onePoint;
this.twoPoint = twoPoint;
this.threePoint = threePoint;
this.fourPoint = fourPoint;
this.side = side;
}
public Square(){
}
public Square createSquare(){
scanner = new Scanner(System.in);
Point one = new Point();
Point two = new Point();
Point three = new Point();
Point four = new Point();
System.out.print("Inpout x for point upper left corner:");
one.setX(scanner.nextDouble());
System.out.print("Input y for point upper left corner:");
one.setY(scanner.nextDouble());
System.out.println("Input size side:");
double side = scanner.nextDouble();
two.setX(one.getX() + side);
two.setY(one.getY());
three.setX(one.getX());
three.setY(one.getY() - side);
four.setX(two.getX());
four.setY(three.getY());
return new Square(one, two, three, four, side);
}
@Override
public String toString() {
return "Square [onePoint=" + onePoint +
", twoPoint=" + twoPoint +
", threePoint=" + threePoint +
", fourPoint=" + fourPoint +
", side=" + side + "]";
}
@Override
public double getPerimeter() {
return side * 4;
}
@Override
public double getArea() {
return Math.pow(side, 2);
}
}
package com.gmail.kudr641.example1;
import java.util.Scanner;
public class Triangle extends Shape {
private Point onePoint;
private Point twoPoint;
private Point threePoint;
private double sideOne;
private double sideTwo;
private double sideThree;
private Scanner scanner;
public Triangle(Point onePoint, Point twoPoint, Point threePoint,
double sideOne, double sideTwo, double sideThree) {
this.onePoint = onePoint;
this.twoPoint = twoPoint;
this.threePoint = threePoint;
this.sideOne = sideOne;
this.sideTwo = sideTwo;
this.sideThree = sideThree;
}
public Triangle(){
}
public Triangle createTriangle(){
scanner = new Scanner(System.in);
Point one = new Point();
Point two = new Point();
Point three = new Point();
System.out.print("Input x for point one:");
one.setX(scanner.nextDouble());
System.out.print("Input y for point one:");
one.setY(scanner.nextDouble());
System.out.print("Input x for point two:");
two.setX(scanner.nextDouble());
System.out.print("Input y for point two:");
two.setY(scanner.nextDouble());
System.out.print("Input x for point three:");
three.setX(scanner.nextDouble());
System.out.print("Input y for point three:");
three.setY(scanner.nextDouble());
double sideOne = getLength(one, two);
double sideTwo = getLength(two, three);
double sideThree = getLength(three, one);
if(sideOne + sideTwo <= sideThree || sideTwo + sideThree <= sideOne
|| sideOne + sideTwo <= sideTwo){
System.out.println("Options are not suitable!");
System.out.println("Try it again!");
return createTriangle();
}else{
return new Triangle(one, two, three, sideOne, sideTwo, sideThree);
}
}
@Override
public double getPerimeter() {
return sideOne + sideTwo + sideThree;
}
@Override
public double getArea() {
double semiperimeter = getSemiperimeter();
return Math.sqrt(
semiperimeter * (semiperimeter - sideOne) *
(semiperimeter - sideTwo) * (semiperimeter - sideThree));
}
@Override
public String toString() {
return "Triangle [onePoint=" + onePoint +
", twoPoint=" + twoPoint +
", threePoint=" + threePoint +
", sideOne=" + sideOne +
", sideTwo=" + sideTwo +
", sideThree=" + sideThree + "]";
}
private double getSemiperimeter() {
return getPerimeter() / 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment