Create a class called Triangle that stores the length of the base and height of a right-angled triangle in two instance variables. Include a constructor that sets these values. Also include a default constructor. Define two functions. The first is hypot( ), which returns the length of the hypotenuse. The second is area( ), which returns the area of the rectangle. (8 mks)
ii) Write an appropriate driver program for the class created above. (4 mks)
import static java.lang.Math.sqrt;
public class Triangle {
double lengthOfTheBase;
double heightOfRightAngled;
public Triangle(){
}
public Triangle(double length,double height){
this.lengthOfTheBase=length;
this.heightOfRightAngled=height;
}
public double area(double length , double height){
return (length*height)/2;
}
public double hypot(double length , double height){
return sqrt((length*length) + (height*height));
}
public static void main(String args[]){
Triangle abc = new Triangle(10,15);
System.out.println("Length of the hypotenuse: "+abc.hypot(abc.lengthOfTheBase,abc.heightOfRightAngled));
System.out.println("The area of the rectangle: "+abc.area(abc.lengthOfTheBase, abc.heightOfRightAngled));
}
}
Comments
Leave a comment