Question: Comment the code. Using the provided code (Question01.java and Triangle.java), write the largerAreaSum method, found in the provided code, that takes in two arrays of
Comment the code.
Using the provided code (Question01.java and Triangle.java), write the largerAreaSum method, found in the provided code, that takes in two arrays of Triangles and returns the array that has the larger sum of the areas. Each instance of a triangle has the properties base and height that can be accessed via their accessors, and the formula to calculate the area is:
You may assume that both arrays have been constructed and contain no null values. If the sums of areas of both are equal, then the method should return null.
Solution Tests:
- If the first array has the values {(Base = 1.0, Height = 2.0), (Base = 3.0, Height = 4.0), (Base = 5.0, Height = 6.0)} and the second array has the value {(Base = 7.0, Height = 8.0), (Base = 9.0, Height = 10.0), (Base = 11.0, Height = 12.0)} does the program printout?
The triangle array with the larger sum is:
Triangle with Base: 7.0 Height: 8.0
Triangle with Base: 9.0 Height: 10.0
Triangle with Base: 11.0 Height: 12.0
- If the first array has the values {(Base = 92.0, Height = 35.0), (Base = 27.0, Height = 9.0), (Base = 83.0, Height = 89.0)} and the second array has the value {(Base = 51.0, Height = 11.0), (Base = 42.0, Height = 35.0), (Base = 32.0, Height = 7.0)} does the program printout?
The triangle array with the larger area sum is:
Triangle with Base: 92.0 Height: 35.0
Triangle with Base: 27.0 Height: 9.0
Triangle with Base: 83.0 Height: 89.0
- If the first array has the values {(Base = 1.0, Height = 1.0), (Base = 1.0, Height = 1.0), (Base = 1.0, Height = 1.0)} and the second array has the value {(Base = 1.0, Height = 1.0), (Base = 1.0, Height = 1.0), (Base = 1.0, Height = 1.0)} does the program printout?
The triangle array with the larger sum is:
null
Question01.java:
public class Question01 {
public static Triangle[] largerAreaSum(Triangle[] a, Triangle[] b)
{
//-----------------------------------------------------------------------------------
//Write your solution here
}//Do not alter this
//Write any additional helper properties or methods here
//--------------------------------------------------------------------------------
//Solution Tests
public static void main(String[] args)
{
System.out.println("Test 01");
Triangle[] triArray1 = {new Triangle(1.0, 2.0), new Triangle(3.0, 4.0), new Triangle(5.0, 6.0)};
Triangle[] triArray2 = {new Triangle(7.0, 8.0), new Triangle(9.0, 10.0), new Triangle(11.0, 12.0)};
Triangle[] largerSum = largerAreaSum(triArray1,triArray2);
System.out.println("The triangle array with the larger area sum is: ");
if(largerSum == null)
System.out.println("null");
else
for(int i=0;i System.out.println(largerSum[i]); System.out.println(" Test 02"); Triangle[] triArray3 = {new Triangle(92.0, 35.0), new Triangle(27.0, 9.0), new Triangle(83.0, 89.0)}; Triangle[] triArray4 = {new Triangle(51.0, 11.0), new Triangle(42.0, 35.0), new Triangle(32.0, 7.0)}; largerSum = largerAreaSum(triArray3,triArray4); System.out.println("The triangle array with the larger area sum is: "); if(largerSum == null) System.out.println("null"); else for(int i=0;i System.out.println(largerSum[i]); System.out.println(" Test 03"); Triangle[] triArray5 = {new Triangle(1.0, 1.0), new Triangle(1.0, 1.0), new Triangle(1.0, 1.0)}; Triangle[] triArray6 = {new Triangle(1.0, 1.0), new Triangle(1.0, 1.0), new Triangle(1.0, 1.0)}; largerSum = largerAreaSum(triArray5,triArray6); System.out.println("The triangle array with the larger area sum is: "); if(largerSum == null) System.out.println("null"); else for(int i=0;i System.out.println(largerSum[i]); } Triangle.java: public class Triangle { private double base; private double height; public Triangle(double aB, double aH) { this.setBase(aB); this.setHeight(aH); } public double getBase() { return base; } public void setBase(double base) { if(base > 0.0) this.base = base; } public double getHeight() { return height; } public void setHeight(double height) { if(height > 0.0) this.height = height; } public String toString() { return "Triangle with Base: "+this.base+" Height: "+this.height; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
