Advertisement
fosterbl

RightTriangle.java

Oct 29th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. public class RightTriangle{
  2.    
  3.    //instance variables
  4.    private double base, height;
  5.    
  6.    //Constructors
  7.    
  8.    //default (parameterless)
  9.    public RightTriangle(){
  10.       base = 0.0;
  11.       height = 0.0;
  12.    }
  13.    
  14.    //specified (parameterized)
  15.    public RightTriangle( double b, double h ){
  16.       base = b;
  17.       height = h;
  18.    }
  19.    
  20.    //accessor (getters) methods
  21.    public double getBase(){
  22.       return base;
  23.    }
  24.    
  25.    public double getHeight(){
  26.       return height;
  27.    }
  28.    
  29.    //mutator (setters) methods
  30.    public void setBase( double b ){
  31.       base = b;
  32.    }
  33.    
  34.    public void setHeight( double h ){
  35.       height = h;
  36.    }
  37.    
  38.    //toString - textual representation
  39.    public String toString(){
  40.       return "RightTriangle with base " + base + " and height " + height;
  41.    }
  42.    
  43.    //equals - compare two objects for equality
  44.    public boolean equals( RightTriangle other ){
  45.       if( base == other.getBase() && height == other.getHeight() ) return true;
  46.       else return false;
  47.    }
  48.    
  49.    //"special" methods
  50.    
  51.    //area
  52.    public double area(){
  53.       return 0.5 * base * height;
  54.    }
  55.    
  56.    //hypotenuse
  57.    public double hypotenuse(){
  58.       return Math.sqrt( Math.pow( base, 2 ) + Math.pow( height, 2 ) );
  59.    }
  60.    
  61.    //perimeter
  62.    public double perimeter(){
  63.       return base + height + hypotenuse();
  64.    }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement