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
public class Rectangle {
private int width;
private int height;
public void set_values(int width, int height) {
this.width = width;
this.height = height;
}
public int area() {
return width * height;
}
}
public class Main {
public static void main(String[] args) {
Rectangle rect = new Rectangle();
rect.set_values(10, 6);
System.out.println(rect.area());
}
}
Comments
Leave a comment