Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Shape{
- private String name;
- public Shape(String n){
- name = n;
- }
- public String getName(){
- return name;
- }
- public double area(){
- return 0.0;
- }
- public double perimeter(){
- return 0.0;
- }
- public String toString(){
- return "Shape";
- }
- }
- class Polygon extends Shape{
- private int numSides;
- public Polygon(String n, int ns){
- super(n);
- numSides = ns;
- }
- public String toString(){
- return "Polygon with " + numSides + " sides";
- }
- }
- class Triangle extends Polygon{
- private double leg1, leg2;
- public Triangle(double l1, double l2){
- super("Triangle", 3);
- leg1 = l1;
- leg2 = l2;
- }
- public double hypotenuse(){
- return Math.sqrt( Math.pow(leg1, 2) + Math.pow(leg2, 2) );
- }
- public double perimeter(){
- return leg1 + leg2 + hypotenuse();
- }
- public double area(){
- double s = perimeter() / 2;
- return Math.sqrt( s * (s - leg1) * (s - leg2) * (s - hypotenuse()) );
- }
- public String toString(){
- return super.toString() + "\n" + getName() + " with legs of length " + leg1 + " and " + leg2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement