13.12 Write 2-3 paragraphs comparing a design in which sumArea() takes an argume
ID: 3831872 • Letter: 1
Question
13.12 Write 2-3 paragraphs comparing a design in which sumArea() takes an argument of type of array of GeometricObjects to a design in which the sumArea() method takes an argument of a child class type, such as Rectangle. Which design offers more flexibility? code reuse? better maintainability? For each answer you give along these dimensions explain your reasoning.
[JAVA] (Do not need the program, just the written paragraphs)
*13.12 (Sum the areas of geometric objects Write a method that sums the areas of all the geometric objects in an array. The method signature is public static double sumArea (Geometric0bject aD Write a test program that creates an array of four objects (two circles and two rectangles) and computes their total area using the sumArea method.Explanation / Answer
The first one is better than second one.
if we define : sumArea(GeometricObjects[] objects), then since reference of GeometricObjects class can hold the object of any child object, so we just need to add those child objects in one array and pass this array to this method.
double sumArea(GeometricObject[] objects){
double areaSum = 0;
for(GeometricObject obj : objects)
areaSum = areaSum + obj.getArea(); // respective getArea method will be called : using Polymorphism
return areaSum;
}
If we develop sepearate method then in this case we need to define seperate method for each Geometric Object type
double sumArea(Rectangle[] objects){
double areaSum = 0;
for(Rectangle obj : objects)
areaSum = areaSum + obj.getArea();
return areaSum;
}
double sumArea(Circle[] objects){
double areaSum = 0;
for(Rectangle obj : objects)
areaSum = areaSum + obj.getArea();
return areaSum;
}
double sumArea(Square[] objects){
double areaSum = 0;
for(Rectangle obj : objects)
areaSum = areaSum + obj.getArea();
return areaSum;
}
THis is code redundancy and not easy to maintain code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.