Search⌘ K

Solution Review: Calculating the Area

Explore how to implement a solution in Java to calculate the area of a right angle triangle. Learn to use private variables, overloaded constructors, and methods, including static methods for class-wide functionality, to write clean and maintainable code.

Solution: Did you find the ‘right’ area? #

Java
class rightAngleTriangle {
//Define the member variables, constructor and
// relevant area method
private double length;
private double height;
public rightAngleTriangle(int l, int h) {
length = l;
height = h;
}
public double area() {
return (length * height / 2.0);
}
}
class challenge_one {
public static double test(rightAngleTriangle rt) {
return rt.area();
}
public static void main( String args[] ) {
rightAngleTriangle one= new rightAngleTriangle(3,5);
System.out.println("Area of right Angle traingle:" + test(one));
}
}

Understanding the code

...