Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RightTriangle{
- //instance variables
- private double base, height;
- //Constructors
- //default (parameterless)
- public RightTriangle(){
- base = 0.0;
- height = 0.0;
- }
- //specified (parameterized)
- public RightTriangle( double b, double h ){
- base = b;
- height = h;
- }
- //accessor (getters) methods
- public double getBase(){
- return base;
- }
- public double getHeight(){
- return height;
- }
- //mutator (setters) methods
- public void setBase( double b ){
- base = b;
- }
- public void setHeight( double h ){
- height = h;
- }
- //toString - textual representation
- public String toString(){
- return "RightTriangle with base " + base + " and height " + height;
- }
- //equals - compare two objects for equality
- public boolean equals( RightTriangle other ){
- if( base == other.getBase() && height == other.getHeight() ) return true;
- else return false;
- }
- //"special" methods
- //area
- public double area(){
- return 0.5 * base * height;
- }
- //hypotenuse
- public double hypotenuse(){
- return Math.sqrt( Math.pow( base, 2 ) + Math.pow( height, 2 ) );
- }
- //perimeter
- public double perimeter(){
- return base + height + hypotenuse();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement