Skip to content

Instantly share code, notes, and snippets.

Created September 24, 2016 12:23
Show Gist options
  • Save anonymous/4d90331db876ce843e4cc48a27964d84 to your computer and use it in GitHub Desktop.
Save anonymous/4d90331db876ce843e4cc48a27964d84 to your computer and use it in GitHub Desktop.
package org.stepic.java.shapes;
/**
* Created by vitaly on 24/09/16.
*/
public class Circle extends Shape {
private final Point center;
private final double radius;
public Circle(Point center, double radius, Color color) {
super(color);
this.center = center;
this.radius = radius;
}
public Point getCenter() { return center; }
public double getRaius() { return radius; }
@Override
public double getArea() { return radius * radius * Math.PI; }
@Override
public String toString() {
return "Circle {" +
"center=" + center +
", radius=" + radius +
", color=" + getColor() +
"}";
}
}
package org.stepic.java.shapes;
/**
* Created by vitaly on 24/09/16.
*/
public enum Color {
BLACK,
WHITE,
RED,
GREEN,
BLUE
}
package org.stepic.java.shapes;
import java.util.DoubleSummaryStatistics;
/**
* Created by vitaly on 24/09/16.
*/
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(
new Point(0, 0), 1, Color.BLACK);
Triangle triangle = new Triangle(
new Point(0, 0), new Point(1, 0), new Point(0, 1), Color.RED);
Square square = new Square(
new Point(5, 5), 2, Color.BLUE);
Shape shape = triangle;
Object object = triangle;
triangle = (Triangle) object;
Shape[] shapes = {circle, triangle, square};
printArrayElements(shapes);
Shape maxShape = findShapeWithMaxArea(shapes);
System.out.println("Shape with max area: " + maxShape);
}
private static void printArrayElements(Object[] objects) {
for (int i = 0; i < objects.length; i++) {
System.out.println(i + ": " + objects[i]);
}
}
private static Shape findShapeWithMaxArea(Shape[] shapes) {
Shape maxShape = null;
double maxArea = Double.NEGATIVE_INFINITY;
for (Shape shape: shapes) {
double area = shape.getArea();
if (area > maxArea) {
maxArea = area;
maxShape = shape;
}
}
return maxShape;
}
}
package org.stepic.java.shapes;
/**
* Created by vitaly on 24/09/16.
*/
public class Point {
private final double x;
private final double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() { return x; }
public double getY() { return y; }
@Override
public String toString() { return "(" + x + ", " + y + ")"; }
}
package org.stepic.java.shapes;
/**
* Created by vitaly on 24/09/16.
*/
public class Shape {
private final Color color;
public Shape(Color color) { this.color = color; }
public Color getColor() { return color; }
public double getArea() { return Double.NaN; }
}
package org.stepic.java.shapes;
/**
* Created by vitaly on 24/09/16.
*/
public class Square extends Shape {
private final Point corner;
private final double size;
public Square(Point corner, double size, Color color) {
super(color);
this.corner = corner;
this.size = size;
}
public Point getCorner() { return corner;}
public double getSize() { return size; }
@Override
public double getArea() { return size * size; }
@Override
public String toString() {
return "Square{" +
"corner=" + corner +
", size=" + size +
", color=" + getColor() +
"}";
}
}
package org.stepic.java.shapes;
/**
* Created by vitaly on 24/09/16.
*/
public class Triangle extends Shape {
private final Point a;
private final Point b;
private final Point c;
public Triangle(Point a, Point b, Point c, Color color) {
super(color);
this.a = a;
this.b = b;
this.c = c;
}
@Override
public double getArea() {
return Math.abs((a.getX() - c.getX()) * (b.getY() - c.getY())
- (b.getX() - c.getX()) * (a.getY() - c.getY())) / 2;
}
@Override
public String toString() {
return "Triangle{" +
"a=" + a +
", b=" + b +
", c=" + c +
", color=" + getColor() +
"}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment