11import java .awt .Rectangle ;
22
33/**
4- * Example from "My method doesn't return what I expect."
4+ * Examples from Logic Errors section.
55 */
66public class Complex {
77
8+ /**
9+ * My method doesn't return what I expect.
10+ */
811 public static Rectangle intersection (Rectangle a , Rectangle b ) {
912 int x1 = Math .min (a .x , b .x );
1013 int y1 = Math .min (a .y , b .y );
@@ -14,10 +17,19 @@ public static Rectangle intersection(Rectangle a, Rectangle b) {
1417 return rect ;
1518 }
1619
20+ /**
21+ * I've got a big, hairy expression and it doesn’t do what I expect.
22+ */
1723 public static void main (String [] args ) {
18- Rectangle x = new Rectangle (0 , 0 , 10 , 10 );
19- Rectangle y = new Rectangle (-5 , -5 , -5 , -5 );
20- System .out .println (intersection (x , y ));
24+ Rectangle rect = new Rectangle (0 , 0 , 10 , 10 );
25+ Rectangle ngle = new Rectangle (-5 , -5 , -5 , -5 );
26+ System .out .println (intersection (rect , ngle ));
27+
28+ double halfWidth = 0.5 * rect .getWidth ();
29+ double halfHeight = 0.5 * rect .getHeight ();
30+ int dx = (int ) Math .round (halfWidth );
31+ int dy = (int ) Math .round (halfHeight );
32+ rect .translate (dx , dy );
2133 }
2234
2335}
0 commit comments