Data : Width (double) and Height (double) that specify the width and height of the rectangle.
Methods:
- A no-arg constructor that creates a default rectangle.
- A Constructor that creates a rectangle with the specified width and height.
- A method named getArea() that returns the area of this rectangle.
- A method named getPerimeter() that returns the perimeter.
Program:-
package assignmentexamples;
public class Rectangle
{
double width , height;
Rectangle()
{
System.out.println("\n------|A Rectangle having width and height|------\n");
}
Rectangle(double a, double b)
{
width=a;
height=b;
System.out.println("Width of rectangle is :- \n "+ width);
System.out.println("Height of rectangle is :- \n "+ height);
}
Double getArea()
{
return(width*height);
}
Double getPerimeter()
{
return(2*(width+height));
}
public static void main(String[] args)
{
Rectangle obj = new Rectangle();
Rectangle obj1 = new Rectangle(2,3);
System.out.println(" ");
System.out.println("Area of Rectangle is:-\n"+obj1.getArea());
System.out.println("Perimeter of Rectangle is:-\n"+obj1.getPerimeter());
}
}
Output:-
2 Comments
Ty for this ����
ReplyDeleteHelpful coding
ReplyDelete