Answer to Question #316265 in Java | JSP | JSF for Jay

Question #316265

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)

1
Expert's answer
2022-03-24T07:49:03-0400
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));
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS