For this problem you will implement a complete class called

. For this problem, you will implement a complete class called Rectangle which represents a rectangle. Here are the specifications: Instance Variables: The class has two instance variables.

length: an int representing the length of the rectangle.

width: an int representing the width of the rectangle.

Methods you must implement:

Constructor: Define a constructor that takes two parameters (length and width) and initializes the instance variables accordingly.

Accessor methods (get methods): These methods should let accessing length and width.

area: This method should return the area of the rectangle.

merge: This method takes a Rectangle type parameter. The method will create a new Rectangle object whose length is the sum of length of the calling object and the length of the parameter. Similarly, the width of the new Rectangle object is the sum of width of the calling object and the width of the parameter. A reference to the new Rectangle object should be returned. The calling object and the parameter object should not be modified.

toString: Write the toString method associated with this class. The returned String has to be in the following form: “length : [length value here], width: [width value here]”.

totalArea: This is a static method that takes an array of Rectangles as a parameter. The method returns the sum of the areas of the rectangles in the array.

Problem 3. Implement a ColoredRectangle class which extends the Rectangle class you have defined in Problem 3. In addition to the instance variables of Rectangle, ColoredRectangle has the following instance variable:

• color: a String that represents the color associated with the rectangle.

Methods you must implement:

Constructor: Define a constructor that takes three parameters (length, width and color).

merge: This method takes a ColoredRectangle type parameter. The method will create a new ColoredRectangle object whose length is the sum of length of the calling object and the length of the parameter. Similarly, the width of the new ColoredRectangle object is the sum of width of the calling object and the width of the parameter. The color of the new ColoredRectangle object will be the color of the parameter object. A reference to the new ColoredRectangle object should be returned. The calling object and the parameter object should not be modified. YOU WILL GET 3 bonus points if you correctly call the super’s merge(...) within this method.

Is this merge method considered overriding the merge method of Rectangle? Why or why not?

•   toString: Write the toString method associated with this class. The returned String has to be in the following form: “length : [length value here], width: [width value here], color: [color value here] ”.

Solution

Rectangle.java

public class Rectangle {
   private int length;
   private int width;
  
   public Rectangle(int l, int w){
       length=l;
       width=w;
   }
  
   public int area(){
       return length*width;
   }
  
   public Rectangle merge(Rectangle r){
       Rectangle newRectangle= new Rectangle(this.length+r.getLength(),this.width+r.getWidth());
       return newRectangle;
   }
  
   public int getLength() {
       return length;
   }
   public int getWidth() {
       return width;
   }
  
   public String toString(){
       return \"length : [\"+ length+\"], width: [\"+ width+ \"]\";
   }
  
   public static int totalArea(Rectangle recArray[]){
       int sum=0;
       for(int i=0;i<recArray.length;i++){
           sum+=recArray[i].area();
       }
       return sum;
   }
  
  
}

ColoredRectangle.java:

public class ColoredRectangle extends Rectangle{
   private String color;
  
   public ColoredRectangle(int length,int width,String color){
       super(length,width);
       this.color=color;
   }
  
   public Rectangle merge(ColoredRectangle cr){
       Rectangle r=super.merge(new Rectangle(cr.getLength(),cr.getWidth()));
       ColoredRectangle newColoredRectangle=new ColoredRectangle(r.getLength(),r.getWidth(),cr.color);
       return newColoredRectangle;      
   }
  
   public String toString(){
       return super.toString()+ \"color: [\"+ color +\"]\";
   }
  
}

The merge method is overridden, as ColoredRectangle class provides a specific implementation of the merge method defined in Rectangle class.

As per the definition of method overriding-

\"The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class. \"

. For this problem, you will implement a complete class called Rectangle which represents a rectangle. Here are the specifications: Instance Variables: The clas
. For this problem, you will implement a complete class called Rectangle which represents a rectangle. Here are the specifications: Instance Variables: The clas

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site