Write a java program using class called Rectangle and an object called rect. This class should have four members: two data members of type int with private access and two member functions with public access: set_values() and area().Set_values() to initialize the values of rectangle and area() to return the area of rectangle. Then finally write a main function to implement the above class.
class Rectangle {
// four members: two data members of type int with private access
private int width;
private int height;
/**
* Set_values() to initialize the values of rectangle
*
* @param width
* @param height
*/
public void Set_values(int width, int height) {
this.width = width;
this.height = height;
}
/**
* area() to return the area of rectangle.
*
* @return
*/
public int area() {
return this.width * this.height;
}
}
public class Q201816 {
public static void main(String[] args) {
Rectangle rect = new Rectangle();
rect.Set_values(5, 4);
System.out.println("Width = 5");
System.out.println("Height = 4");
System.out.println("Rectangle area: " + rect.area());
}
}
Comments
Leave a comment