-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRectangle.java
91 lines (69 loc) · 2.19 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Written by Jessica Thorne (thorn573)
import java.awt.Color;
public class Rectangle {
double topLeftXPos, topLeftYPos, width, height;
Color color;
public Rectangle (double x, double y, double w, double h) {
topLeftXPos = x;
topLeftYPos = y;
width = w;
height = h;
} // Constructor
public double calculatePerimeter() {
return (width * 2) + (height * 2);
}
public double calculateArea() {
return width * height;
}
public void setColor(Color newC) {
color = newC;
}
public void setPos(double newX, double newY) {
topLeftXPos = newX;
topLeftYPos = newY;
}
public void setHeight(double newH) {
height = newH;
}
public void setWidth(double newW) {
width = newW;
}
public Color getColor() {
return color;
}
public double getXPos() {
return topLeftXPos;
}
public double getYPos() {
return topLeftYPos;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public static void main(String[] args) {
// Tests for Rectangle Class:
Rectangle myRectangle = new Rectangle(1.2, 4.5, 2.1, 6.5);
// 1) Calculate perimeter
System.out.println("Rectangle perimeter: " + myRectangle.calculatePerimeter());
// 2) Calculate area
System.out.println("Rectangle area: " + myRectangle.calculateArea());
// 3) Setting & getting color
myRectangle.setColor(Color.blue);
System.out.println("Rectangle color: " + myRectangle.getColor());
// 4) Setting & getting new position
System.out.println("Old rectangle position: (" + myRectangle.getXPos() + ", " + myRectangle.getYPos() + ")");
myRectangle.setPos(8.9, 7.2);
System.out.println("New rectangle position: (" + myRectangle.getXPos() + ", " + myRectangle.getYPos() + ")");
// 5) Setting & getting new width
System.out.println("Old rectangle width: " + myRectangle.getWidth());
myRectangle.setWidth(9.2);
System.out.println("New rectangle width: " + myRectangle.getWidth());
// 6) Setting & getting new height
System.out.println("Old rectangle height: " + myRectangle.getHeight());
myRectangle.setHeight(6.7);
System.out.println("New rectangle height: " + myRectangle.getHeight());
}
} // Rectangle Class