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.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.