Skip to content

Instantly share code, notes, and snippets.

@JavaDen
Created March 11, 2016 05:10
Show Gist options
  • Save JavaDen/e36bab918336167d87ef to your computer and use it in GitHub Desktop.
Save JavaDen/e36bab918336167d87ef to your computer and use it in GitHub Desktop.
Shape
package extend;
public class Circle extends Shape {
private double radius;
private String name = "";
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
double perimetr() {
// TODO Auto-generated method stub
if (this.radius == 0) {
double ran = Math.random() * 1000 + 1;
System.out.println("Radius is : " + ran +" !!! (was generated atomaticly) perimetr");
}
return 2* Math.PI * this.radius;
}
@Override
double area() {
// TODO Auto-generated method stub
if (this.radius == 0) {
double ran = Math.random() * 1000 + 1;
System.out.println("Radius is : " + ran +" !!! (was generated atomaticly)");
this.radius = ran;
}
return Math.pow(this.radius * Math.PI, 2);
}
}
package extend;
import java.util.Arrays;
public class Desk {
private Shape[] parts = new Shape[4];
public Shape[] getParts() {
return parts;
}
public boolean addShapeToPartsDesc(Shape shapeType) {
boolean isShapeInArray = false;
for (int i = 0; i < parts.length; i++) {
if (parts[i] == shapeType) {
isShapeInArray = true;
System.out.println("| " + shapeType.getName() + " | Already in use . System can not add ");
break;
}
}
for (int i = 0; i < parts.length; i++) {
if (parts[i] == null && !isShapeInArray) {
parts[i] = shapeType;
isShapeInArray = true;
System.out.println("| " + shapeType.getName() + " | Add process has been successfully completed");
}
}
return isShapeInArray;
}
public void deleteShapeFromDescPart(Shape shapeType) {
for (int i = 0; i < parts.length; i++) {
if (parts[i] == shapeType) {
parts[i] = null;
System.out.println("| " + shapeType.getName() + " | Removal process has been successfully completed");
}
}
}
@Override
public String toString() {
double result = 0;
int count = 0;
StringBuilder sb = new StringBuilder();
sb.append("\n Desk contains next used parts :");
for (Shape shape : parts) {
if (shape != null) {
count++;
sb.append("\n");
sb.append(" Parts " + count + " :" + shape.getName());
result = result + shape.area();
}
}
sb.append("\n");
sb.append("\n Total used parts : " + count);
sb.append("\n Amount all areas of shapes : ").append(result);
return sb.toString();
}
}
package extend;
public class Main {
public static void main(String[] args) {
Circle crl = new Circle();
crl.setName("Circle");
crl.setRadius(2.97);
Square sqr = new Square();
sqr.setName("Square");
sqr.setSideA(2.29);
Triangle trl = new Triangle();
trl.setName("Triangle");
trl.setSideA(23.20);
trl.setSideB(22.30);
trl.setSideC(21.80);
Desk desc= new Desk();
desc.addShapeToPartsDesc(trl);
desc.addShapeToPartsDesc(crl);
desc.addShapeToPartsDesc(sqr);
System.out.println(desc);
System.out.println( "\n");
desc.addShapeToPartsDesc(trl);
desc.addShapeToPartsDesc(crl);
desc.addShapeToPartsDesc(sqr);
System.out.println(desc);
System.out.println( "\n");
desc.deleteShapeFromDescPart(trl);
System.out.println(desc);
}
}
package extend;
public abstract class Point {
private double x;
private double y;
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;
}
}
package extend;
public abstract class Shape extends Point {
private String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
abstract double perimetr();
abstract double area();
}
package extend;
public class Square extends Shape {
private double sideA;
public double getSideA() {
return sideA;
}
public void setSideA(double sideA) {
this.sideA = sideA;
}
@Override
double perimetr() {
return this.sideA * 4;
}
@Override
double area() {
return Math.pow(this.sideA, 2);
}
}
package extend;
public class Triangle extends Shape {
private double sideA;
private double sideB;
private double sideC;
public double getSideA() {
return sideA;
}
public void setSideA(double sideA) {
this.sideA = sideA;
}
public double getSideB() {
return sideB;
}
public void setSideB(double sideB) {
this.sideB = sideB;
}
public double getSideC() {
return sideC;
}
public void setSideC(double sideC) {
this.sideC = sideC;
}
@Override
double perimetr() {
// TODO Auto-generated method stub
return this.sideA + this.sideB + this.sideC;
}
@Override
double area() {
// TODO Auto-generated method stub
this.sideA = (this.sideA == 0) ? Math.random() * 100 + 1 : this.sideA;
this.sideB = (this.sideB == 0) ? Math.random() * 100 + 1 : this.sideB;
this.sideC = (this.sideC == 0) ? Math.random() * 100 + 1 : this.sideC;
double p = (this.sideA + this.sideB + this.sideC) / 2;
return Math.sqrt(p * (p - this.sideA) * (p - this.sideB) * (p - this.sideC));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment