3. Write a Java program to create a class called "Rectangle" with width and height attributes. Calculate the area and perimeter of the rectangle
class Rectangle
{
private double width;
private double height;
public Rectangle(double w,double h)
{
width=w;
height=h;
}
void setWidth(double w)
{
width=w;
}
void setHeight(double h)
{
height=h;
}
double getWidth()
{
return width;
}
double getHeight()
{
return height;
}
double calculateArea()
{
return width * height;
}
double calculatePerimeter()
{
return 2 * (width + height);
}
void print()
{
System.out.println("Width:"+width);
System.out.println("Height:"+height);
System.out.println("Area:"+calculateArea());
System.out.println("Perimeter:"+calculatePerimeter());
}
public static void main(String st[])
{
Rectangle obj=new Rectangle(22.4,7.5);
obj.print();
}
}
OUTPUT
Width:22.4
Height:7.5
Area:168.0
Perimeter:59.8