forked from SonarSource/sonarqube
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRectangle.java
49 lines (40 loc) · 1.03 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
42
43
44
45
46
47
48
49
public class Rectangle {
private double width;
private double height;
public Rectangle(double width, double height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Width and height cannot be negative.");
}
this.width = width;
this.height = height;
}
public Rectangle() {
this(0.0, 0.0);
}
public void setWidth(double width) {
if (width >= 0) {
this.width = width;
} else {
System.err.println("Width cannot be negative.");
}
}
public void setHeight(double height) {
if (height >= 0) {
this.height = height;
} else {
System.err.println("Height cannot be negative.");
}
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
}