create a class callede rectangular/.... Fields (instance variables) The class ha
ID: 3688376 • Letter: C
Question
create a class callede rectangular/....
Fields (instance variables) The class has two fields: len- a double representing the length of the rectangle. wid - a double representing the width of the rectangle. Methods you must implement Constructor - Define a constructor that takes two parameters (the length and width of a rectangle) and initializes the fields accordingly. area - Define a method called area that returns the area of the rectangle represented by the current object. merge - This method takes a Rectangle reference. The method will create a new Rectangle object whose length is the sum of the lengths of the current object and the length of the parameter. Similarly, the width of the new Rectangle object is the sum of the width of the current object and the width of the parameter. A reference to the Rectangle object created must be returned. The current object and the parameter object may not be modified. toString - Based on the provided output write the toString method associated with this class. totalArea - This is an static method that takes an array of Rectangles as a parameter. The method returns the sum of the areas of the rectangles in the arrayExplanation / Answer
Hello there ,
Please find below code and its O/P.
/**
* This class represents a rectangle having width and length.
*
* @author dipal.prajapati
*
*/
public class Rectangle {
private double len;
private double wid;
public Rectangle(double len, double wid) {
super();
this.len = len;
this.wid = wid;
}
/**
* It computes area of the current rectangle object.
*
* @return
*/
public double area() {
return this.len * this.wid;
}
/**
* Will create new rectangle having merged length and width.
* @param anotherRect
* @return
*/
public Rectangle merge(Rectangle anotherRect) {
return new Rectangle(this.len + anotherRect.len, this.wid + anotherRect.wid);
}
@Override
public String toString() {
return "Rectangle [len=" + len + ", wid=" + wid + "]";
}
/**
* It will compute total area for all the rectangles in the given array.
* @param arrayOfRect
* @return
*/
public static double totalArea(Rectangle[] arrayOfRect) {
double totalArea = 0;
for (int i = 0; i < arrayOfRect.length; i++) {
totalArea = totalArea + arrayOfRect[i].area();
}
return totalArea;
}
/**
* Driver Method to test :
* @param args
*/
public static void main (String args[]) {
Rectangle r1=new Rectangle(2,3);
Rectangle r2=new Rectangle(7,5);
System.out.println(r1);
System.out.println("Area: "+r1.area());
System.out.println(r1.merge(r2));
Rectangle[] allRects=new Rectangle[2];
allRects[0]=r1;
allRects[1]=r2;
System.out.println("Total Area: "+Rectangle.totalArea(allRects));
}
}
====O/P====
Rectangle [len=2.0, wid=3.0]
Area: 6.0
Rectangle [len=9.0, wid=8.0]
Total Area: 41.0
Let me know if you have any queries .
Thanks.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.