Write a program using class ‘Rectangle’ to compute the area and perimeter of rectangle. The Class rectangle should have the following members:
· Two data members’ length and width.
· One member function ‘setData’ to assign values to data members ‘length’ & ‘width’. It should also verify that length and width between 0.0 and 25.0
· One member function ‘area’ to compute and print the area of rectangle.
Area =length * width
· One member function ‘perimeter’ to compute and print the perimeter of rectangle.
Perimeter =2*(length * width)
public class Rectangle {
int width;
int length;
public void setData(int width, int length) {
if (width > 0 && width <= 25) {
this.width = width;
} else {
System.out.println("Wrong value!");
}
if (length > 0 && length <= 25) {
this.length = length;
} else {
System.out.println("Wrong value!");
}
}
public int area() {
int area = length * width;
return area;
}
public int perimeter() {
int perimeter = 2 * (length + width);
return perimeter;
}
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.setData(5, 7);
System.out.println("Rectangle area:" + rectangle.area());
System.out.println("Rectangle perimeter:" + rectangle.perimeter());
}
}
Comments
Leave a comment