Commit 98abbdc

mokha <mokha@cisco.com>
2019-05-05 19:45:03
solve for side B
1 parent 5f3b22b
Changed files (3)
assignments
assignment1
src
main
java
ca
mokhan
assignment1
test
java
ca
mokhan
assignments/assignment1/src/main/java/ca/mokhan/assignment1/Triangle.java
@@ -1,6 +1,7 @@
 package ca.mokhan.assignment1;
 
 public class Triangle {
+  public static double NULL = 0.0;
   private double a, b, c;
 
   public Triangle(double a, double b, double c) {
@@ -14,6 +15,7 @@ public class Triangle {
   }
 
   public double getB() {
+    if (this.b == NULL) this.b = Math.sqrt(Math.pow(this.getC(), 2) - Math.pow(this.getA(), 2));
     return this.b;
   }
 
assignments/assignment1/src/test/java/ca/mokhan/assignment1/TriangleTest.java
@@ -5,8 +5,6 @@ import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
 public class TriangleTest extends TestCase {
-  private Triangle subject;
-
   public TriangleTest(String testName) {
     super(testName);
   }
@@ -19,4 +17,9 @@ public class TriangleTest extends TestCase {
     assertFalse(new Triangle(45.0, 55.0, 75.0).isRightTriangle());
     assertTrue(new Triangle(28.0, 45.0, 53.0).isRightTriangle());
   }
+
+  public void testGetB() {
+    assertEquals(64.0, new Triangle(48.0, Triangle.NULL, 80.0).getB());
+    assertEquals(35.0, new Triangle(84.0, Triangle.NULL, 91.0).getB());
+  }
 }
assignments/assignment1/README.md
@@ -148,8 +148,14 @@ You can find the square root of a number by calling the standard function Math.s
 For example, the statement `double y = Math.sqrt(x)` sets `y` to the square root of `x`.
 
 I. Given the right triangles described below, write a program to compute the lengths of the remaining sides using a program.
-a. a=48, c=80
-b. a=84, c=91
+a. a=48, c=80  b = 64
+b. a=84, c=91  b = 35
+
+```text
+  a^2 + b^2 = c^2
+  b^2 = c^2 - a^2
+  b = sqrt(c^2 - a^2)
+```
 
 II. Determine if the following triangles are right-angled triangles:
 a. a=45, b=55, c=75