Write a RectangleRunner program that creates two Box3 objects, one aggregating R
ID: 3666116 • Letter: W
Question
Write a RectangleRunner program that creates two Box3 objects, one aggregating Rectangle and the other aggregating RectangleWithColor. You will have to modify Rectangle so that it, too, implements the Rectangular interface.
Experience in designing and maintaining classes suggests that when we have a choice between inheritance and aggregation, we should favor aggregation over inheritance. The reason is that a change in a superclass propagates the change to all subclasses in the inheritance hierarchy In the code we have written in Lab 12.3.2, a change in the Rectangle class, such as adding a Color variable, is propagated to Boxl whenever we recompile. On the other hand, when using aggregation, we can employ interfaces in subclasses to mitigate the effects of changes in the aggregated classes we are referencing To see how this idea works, consider the Rectangular interface below public interface Rectangular void setlenatbin length); void Satuawwn width) ; or le ariable W're salos, which is s We also construct a new class, BestanglebitbCalor, which is similar to Rectangle, but also contains a Color variable. We're not really using the Color variable in this code, but the class is meant to represent a variant of Rectangle that may be quite different from Rectangle. Notice that the class below implements the Rectangular interface import public class BestangleilithCalax implements Rectangular private jnti length; private jati width; private Color color; public BartonavenwCalawu length, WA width, Color color) thiz^idth- width; = color; public iat catlength return length; public int aewlidthh return width; public Color gatxolaxh) return color; public void setentbiixt, length)Explanation / Answer
public class RectangleDemo {
private class Rectangle {
private double height;
private double width;
private String color;
public Rectangle(double wid, double high)
{
height = high;
width = wid;
}
public Rectangle()
{
height = 1;
width = 1;
color = "White";
}
public void setHeight(double high)
{
height = high;
}
public void setWidth(double wid)
{
width = wid;
}
public void setColor(String col)
{
color = col;
}
public double getArea()
{
return height*width;
}
public double getvolume()
{
return 2*(height + width);
}
public void getColor()
{
System.out.println("Color is: " + color +" ");
[B]return;[/B]
}
}
}
NEW CLASS
public class RectangleDemo {
public static void main(String[] args){
Rectangle box1 = new Rectangle();
Rectangle box2 = new Rectangle(4, 40);
Rectangle box3 = new Rectangle(3.5, 35.9);
String Color = "Red";
box1.setColor(Color);
box2.setColor(Color);
box3.setColor(Color);
box1.getColor();
box2.getColor();
box3.getColor();
System.out.println("The volume of the first box is: " + box1.getvolume() + " ");
System.out.println("The volume of the second box is: " + box2.getvolume() + " ");
System.out.println("The volume of the third box is: " + box3.getvolume() + " ");
System.out.println("The area of the first box is: " + box1.getArea() + " ");
System.out.println("The area of the second box is: " + box2.getArea() + " ");
System.out.println("The area of the third box is: " + box3.getArea() + "
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.