public class Rectangle
2: /**
3: * A class to represent the dimensions of a Rectangle.
4: * Now with a toString method.
5: *
6: * @author Mark Young (A00000000)
7: */
8: public class Rectangle {
10: // ----- Instance variables / Fields ------------------------------- //
12: private double height;
13: private double width;
16: // ----- Constructor ----------------------------------------------- //
18: /**
19: * Create a Rectangle with the given dimensions.
20: * Negative dimensions are replaced with zeroes,
21: * and a warning message is printed.
22: *
23: * @param reqHgt the requested height
24: * @param reqWid the requested width
25: */
26: public Rectangle(double reqHgt, double reqWid) {
27: if (reqHgt >= 0) {
28: height = reqHgt;
29: } else {
30: height = 0;
31: System.out.println("Illegal height for Rectangle: " + reqHgt);
32: }
33: if (reqWid >= 0) {
34: width = reqWid;
35: } else {
36: width = 0;
37: System.out.println("Illegal width for Rectangle: " + reqWid);
38: }
39: }
42: // ----- Getters --------------------------------------------------- //
44: /**
45: * Get the height of this Rectangle.
46: *
47: * @return the height of this Rectangle
48: */
49: public double getHeight() {
50: return height;
51: }
53: /**
54: * Get the width of this Rectangle.
55: *
56: * @return the width of this Rectangle
57: */
58: public double getWidth() {
59: return width;
60: }
62: /**
63: * Get the area of this Rectangle
64: *
65: * @return the area of this Rectangle
66: */
67: public double getArea() {
68: return height * width;
69: }
72: // ----- Setters --------------------------------------------------- //
74: /**
75: * Change the height of this Rectangle.
76: * Request to change to a negative height will be rejected
77: * with a warning message.
78: *
79: * @param reqHgt the requested new height for this Rectangle
80: */
81: public void setHeight(double reqHgt) {
82: if (reqHgt >= 0.0) {
83: height = reqHgt;
84: } else {
85: System.out.println("Illegal height for a Rectangle: " + reqHgt);
86: }
87: }
89: /**
90: * Change the width of this Rectangle.
91: * Request to change to a negative width will be rejected
92: * with a warning message.
93: *
94: * @param reqWid the requested new width for this Rectangle
95: */
96: public void setWidth(double reqWid) {
97: if (reqWid >= 0.0) {
98: width = reqWid;
99: } else {
100: System.out.println("Illegal width for a Rectangle: " + reqWid);
101: }
102: }
104: /**
105: * Return a String representing this Rectangle.
106: *
107: * NOTE TO STUDENTS: toString does not print anything. That's not its job.
108: *
109: * NOTE TO STUDENTS: don't worry about the @Override. We'll explain it in
110: * CSCI 1228.
111: *
112: * @return a String in the form HxW Rectangle, where H and W are this
113: * Rectangle's height and width.
114: */
115: @Override
116: public String toString() {
117: return height + "x" + width + " Rectangle";
118: }
120: }