-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRectangle.java
41 lines (33 loc) · 1.13 KB
/
Rectangle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Rectangle {
// define attributes
private int length;
private int width;
// constructor method to instantiate the attributes
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
// getter methods
public int getLength() {
// Returns the value of private attr length
return this.length;
}
public int getWidth() {
// Returns the value of private attr width
return this.width;
}
// define method calculate area of the rectangle
public int calculate_area() {
return this.length * this.width;
}
// main entry point to java program
public static void main(String[] args) {
// Creating an instance of Rectangle
Rectangle myRectangle = new Rectangle(5, 3);
System.out.println("Length of the square: " + myRectangle.length);
System.out.println("Width of the square: " + myRectangle.width);
// Calculating and printing the area
int area = myRectangle.calculate_area();
System.out.println("The area of the rectangle is: " + area);
}
}