-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetter_setter.java
More file actions
61 lines (49 loc) · 1.45 KB
/
getter_setter.java
File metadata and controls
61 lines (49 loc) · 1.45 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Rectangle {
private double length;
private double width;
public Rectangle() {
this.length = 0.0;
this.width = 0.0;
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
if (length >= 0) {
this.length = length;
} else {
System.out.println("Length cannot be negative.");
}
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
if (width >= 0) {
this.width = width;
} else {
System.out.println("Width cannot be negative.");
}
}
public double calculateArea() {
return length * width;
}
public double calculatePerimeter() {
return 2 * (length + width);
}
}
public class getter_setter {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.setLength(10.5);
rectangle.setWidth(5.0);
System.out.println("Length: " + rectangle.getLength());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
}
}