Skip to content

Instantly share code, notes, and snippets.

@borncorp
Created June 27, 2014 05:54
Show Gist options
  • Save borncorp/772c3db1df436fe6336e to your computer and use it in GitHub Desktop.
Save borncorp/772c3db1df436fe6336e to your computer and use it in GitHub Desktop.
Shape Lab
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
//Create ArrayList of Shapes
ArrayList<Shape> myShapes=new ArrayList<Shape>();
//Create Shapes
Shape Rectangle = new Shape("Rectangle", "blue", 10, 20);
Shape Circle = new Shape("Circle", "red", 10);
Shape Square = new Shape("Square", "green", 20);
Shape Triangle = new Shape("Triangle", "yellow", 30, 30, 30);
//Store Shapes in myShapes
myShapes.add(Rectangle);
myShapes.add(Circle);
myShapes.add(Square);
myShapes.add(Triangle);
printShapes(myShapes);
}
public static void printShapes(ArrayList <Shape> myShapes){
for (Shape shape : myShapes) {
System.out.println(shape.toString());
}
}
}
import java.lang.Math;
public class Shape {
private int dimension1, dimension2, dimension3;
private String shape;
private String color;
private float area;
public Shape(String shape, String color, int dimension1) {
this.dimension1 = dimension1;
this.shape = shape;
this.color = color;
calcArea(shape, dimension1);
}
//Overloaded constructor
public Shape(String shape, String color, int dimension1, int dimension2) {
this.dimension1 = dimension1;
this.dimension2 = dimension2;
this.shape = shape;
this.color = color;
calcArea(shape, dimension1, dimension2);
}
public Shape(String shape, String color, int dimension1, int dimension2, int dimension3) {
this.dimension1 = dimension1;
this.dimension2 = dimension2;
this.dimension3 = dimension3;
this.shape = shape;
this.color = color;
calcArea(shape, dimension1, dimension2, dimension3);
}
public void calcArea(String shape, int dimension1){
if (shape.equalsIgnoreCase("square"))
setArea(dimension1*dimension1);
if (shape.equalsIgnoreCase("circle"))
setArea(dimension1*dimension1*3.14159f);
}
//Overloaded method
public void calcArea(String shape, int dimension1, int dimension2){
setArea(dimension1*dimension2);
}
//Overloaded method
public void calcArea(String shape, int dimension1, int dimension2, int dimension3){
/*
* Area of a triangle = sqrt(s(s-a)(s-b)(s-c)) by Heron's Formula (or
* Hero's Formula), where a, b and c are the lengths of the sides of the
* triangle, and s = ½ (a + b + c) is the semi-perimeter of the
* triangle.
*/
if (shape.equalsIgnoreCase("triangle")){
float s = (dimension1 + dimension2 + dimension3)/2.0f;
float area = s*(s-dimension1)*(s-dimension2)*(s-dimension3);
area= (float) Math.sqrt(area);
setArea(area);
}
}
public int getDimension1() {
return dimension1;
}
public void setDimension1(int dimension1) {
this.dimension1 = dimension1;
}
public int getDimension2() {
return dimension2;
}
public void setDimension2(int dimension2) {
this.dimension2 = dimension2;
}
public int getDimension3() {
return dimension3;
}
public void setDimension3(int dimension3) {
this.dimension3 = dimension3;
}
public String getShape() {
return shape;
}
public void setShape(String shape) {
this.shape = shape;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public float getArea() {
return area;
}
public void setArea(float area) {
this.area = area;
}
@Override
public String toString() {
if (getDimension3()!=0)
if (shape.equalsIgnoreCase("triangle")&&(getDimension1()+getDimension2())<=getDimension3())
return "Impossible Triangle";
else
return "Shape [shape=" + shape + ", color=" + color + ", dimension1="
+ dimension1 + ", dimension2=" + dimension2 + ", dimension3=" + dimension3 + ", area=" + area
+ "]";
if (getDimension2()==0)
return "Shape [shape=" + shape + ", color=" + color + ", dimension1="
+ dimension1 + ", area=" + area
+ "]";
return "Shape [shape=" + shape + ", color=" + color + ", dimension1="
+ dimension1 + ", dimension2=" + dimension2 + ", area=" + area
+ "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment