-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRectangle.java
More file actions
97 lines (89 loc) · 2.43 KB
/
Copy pathRectangle.java
File metadata and controls
97 lines (89 loc) · 2.43 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
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
92
93
94
95
96
97
/**
* The Rectangle class models a three-dimensional Rectangle
*/
public class Rectangle implements Comparable<Rectangle> {
private final int height;
private final int width;
/**
* Create a new Rectangle with the specified dimensions (height, width).
*
* @param height the height of the Rectangle
* @param width the width of the Rectangle
*/
public Rectangle(int height, int width) {
this.height = height;
this.width = width;
}
/**
* Create a copy of Rectangle.
*
* @param Rectangle A Rectangle to copy.
*/
public Rectangle(Rectangle Rectangle) {
this.height = Rectangle.height;
this.width = Rectangle.width;
}
/**
* Get this Rectangle's area
*
* @return the Rectangle's area
*/
public int area() {
return height * width;
}
/**
* @return The Rectangle's height
*/
public int getHeight() {
return height;
}
/**
* @return The Rectangle's width
*/
public int getWidth() {
return width;
}
/**
* Defines if two Rectangles should be considered equal based on their attributes.
*
* @param o an object
* @return true if the given object has equal width and height to this Rectangle
*/
@Override
public boolean equals(Object o)
{
if (!(o instanceof Rectangle)) {
return false;
}
Rectangle other = (Rectangle) o;
return this.width == other.width && this.height == other.height;
}
/**
* Defines the hash code of this Rectangle.
*
* This is required by the contract of hashCode, which states that if for
* two objects x and y, x.equals(y) is true,
* then x.hashCode() == y.hashCode() must also be true. So, as we override
* the Object.equals(Object o), we must also override Object.hashCode().
*
* For a good explanation, see Effective Java Recipe Item 9
* @return the hash code of this Rectangle
*/
@Override
public int hashCode(){
int result = 13;
result = 31 * result + height;
result = 31 * result + width;
return result;
}
@Override
public int compareTo(Rectangle rectangle) {
if (this.area() > rectangle.area()) {
return 1;
} else if (this.area() == rectangle.area()) {
return 0;
} else {
return -1;
}
}
}