Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define a class named Rectangle that contains integer instance data that represen

ID: 3531835 • Letter: D

Question

Define a class named Rectangle that contains integer instance data that represents the width and the length as well as a string color. Define a rectangle constructor to accept and initialize the instance data and include getters and setters methods for each data .Include methods that calculate and return perimeter and area of rectangle. Include a to string method that returns a one-line description of the rectangle as well as the equals method that returns true if two rectangles have same dimensions and colors and false otherwise.

Explanation / Answer

class Rectangle

{

int l,b;

String c;

public Rectangle()

{

l=0;

b=0;

c="black";

}

public Rectangle(int x,int y,String col)

{

l=x;

b=y;

c=col;

}

public int getLength()

{

return l;

}

public int getBreadth()

{

return b;

}

public String getColor()

{

return c;

}

public void setLength(int x)

{

l=x;

}

public void setBreadth(int x)

{

b=x;

}

public void setColor(String x)

{

c=x;

}

public int calcArea()

{

return (l*b);

}

public int calcPerimeter()

{

return (2*(l+b));

}

public boolean equals(Rectangle r)

{

if(r.l==l)

return false;

else if(r.b==b)

return false;

else if(r.c==c)

return false;

else

return true;

}

public String toString()

{

return ("This is a rectangle with length "+l+",breadth "+b+" and is "+c+" in color.");

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote