Right Triangle / Hypotenuse Code
unknown
java
a month ago
935 B
1
Indexable
Never
class RightTriangle { // Class private double base1; private double base2; private double hypotenuse; // Instance variables public RightTriangle() { base1 = 3; base2 = 4; hypotenuse = Math.sqrt(Math.pow(3,2) + Math.pow(4,2)); } public RightTriangle(double a, double b) { // These are our new variables base1 = a; base2 = b; hypotenuse = Math.sqrt(Math.pow(a,2) + Math.pow(b,2)); } public double getPerimeter() { return base1 + base2 + hypotenuse; } } public class Main { public static void main(String[] args) { RightTriangle tri = new RightTriangle(); System.out.println(tri.getPerimeter()); // Prints 12 bcz 3 + 4 + 5 = 12 RightTriangle triangle = new RightTriangle(5.0, 7.0); System.out.println(triangle.getPerimeter()); // Prints 20.6023 bcz 5 + 7 + 8.60232527 (the hypotenuse) = 20.6023 } }