Write a Java statement to create an object Rectangle from the class
Shapes. Also mention any two attributes of the object “Rectangle".
Answers
Answer:
class FindRectangleArea
{
public static void main(String arg[])
{
Rectangle rect = new Rectangle(10, 5);
System.out.println("Length = " + rect.length);
System.out.println("Breadth = " + rect.breadth);
System.out.println("Area = " + rect.getArea());
System.out.println("Perimeter = " + rect.getPerimeter());
}
}
class Rectangle
{
double length;
double breadth;
Rectangle(double length, double breadth)
{
this.length = length;
this.breadth = breadth;
}
double getArea()
{
return length * breadth;
}
double getPerimeter()
{
return 2 * (length + breadth);
}
}
OUTPUT
Length = 10.0
Breadth = 5.0
Area = 50.0
Perimeter = 30.0
Explanation: