Created
January 9, 2016 04:51
-
-
Save Vaporjawn/10ff0937c0cc3420d984 to your computer and use it in GitHub Desktop.
ShapeTester
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package ec.shapetester; | |
import java.util.Arrays; | |
import java.util.Scanner; | |
/** | |
* | |
* @author tug03556 | |
*/ | |
public class ECShapeTester{ | |
public static void main(String[] args){ | |
Scanner keyboard = new Scanner(System.in); | |
Shape[] array = new Shape[4]; | |
//System.out.println(""); | |
Square S = new Square("Square"); | |
Circle C = new Circle ("Circle"); | |
Triangle T = new Triangle("Triangle"); | |
EqualateralTriangle ET = new EqualateralTriangle("Equalateral Triangle"); | |
System.out.println("Enter the height and length of your square"); | |
S.setDimensions(keyboard.nextInt(), keyboard.nextInt()); | |
array[0] = S; | |
System.out.println("Please enter the radius of your circle"); | |
C.setDimensions(keyboard.nextInt()); | |
array[1] = C; | |
System.out.println(" Please enter 3 sides of your triangle"); | |
T.setDimensions(keyboard.nextInt(), keyboard.nextInt(), keyboard.nextInt()); | |
array[2] = T; | |
System.out.println("Please enter a side length of your equalateral triangle"); | |
ET.setDimensions(keyboard.nextInt()); | |
array[3] = ET; | |
for(int i=0; i< array.length; i++){ | |
System.out.println(); | |
System.out.println(array[i]); | |
} | |
} | |
} | |
class Shape { | |
private String name; | |
public Shape(String name) { | |
this.name = name; | |
} | |
/** returns the name of the shape */ | |
public String getName() { | |
return name; | |
} | |
/** returns the area of the shape */ | |
public double getArea() { | |
return 0.0; | |
} | |
public void printDimensions(){ | |
System.out.println("No dimensions"); | |
} | |
} | |
class Square extends Shape{ | |
private int length, height; | |
Square(String name){ | |
super(name); | |
} | |
public void setDimensions(int length, int height){ | |
this.length = length; | |
this.height = height; | |
} | |
@Override | |
public void printDimensions(){ | |
System.out.println("Length is " + length); | |
System.out.println("Height is " + height); | |
} | |
@Override | |
public double getArea(){ | |
return length *height; | |
} | |
} | |
class Circle extends Shape{ | |
private double radius; | |
Circle(String name){ | |
super(name); | |
} | |
public void setDimensions(double radius){ | |
this.radius = radius; | |
} | |
@Override | |
public void printDimensions(){ | |
System.out.println("The Radius is " + radius); | |
//System.out.println("The Area is "+ (3.14 * (radius * radius))); | |
} | |
@Override | |
public double getArea(){ | |
return (3.14 * (radius * radius)); | |
} | |
} | |
class Triangle extends Shape{ | |
private int side1, side2, side3; | |
Triangle(String name){ | |
super(name); | |
} | |
public void setDimensions(int side1, int side2, int side3){ | |
this.side1 = side1; | |
this.side2 = side2; | |
this.side3 = side3; | |
} | |
@Override | |
public void printDimensions(){ | |
System.out.println("The sides are "+side1+" "+side2+" "+side3); | |
} | |
@Override | |
public double getArea(){ | |
return Math.sqrt(((side1+side2+side3)/2)*(((side1+side2+side3)/2)-side1)* | |
(((side1+side2+side3)/2)-side2)*((side1+side2+side3)/2)-side3); | |
} | |
} | |
class EqualateralTriangle extends Shape{ | |
private int side; | |
EqualateralTriangle(String name){ | |
super(name); | |
} | |
public void setDimensions(int side){ | |
this.side = side; | |
} | |
@Override | |
public void printDimensions(){ | |
System.out.println("The side is " + side); | |
} | |
@Override | |
public double getArea(){ | |
int side1 = side; | |
int side2 = side; | |
int side3 = side; | |
return Math.sqrt(((side1+side2+side3)/2)*(((side1+side2+side3)/2)-side1)* | |
(((side1+side2+side3)/2)-side2)*((side1+side2+side3)/2)-side3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment