From e81b7e76f0df1119ee8663e9960d53d71a61a243 Mon Sep 17 00:00:00 2001 From: jhugunin Date: Wed, 27 Aug 2003 20:35:31 +0000 Subject: [PATCH] revised section 4 (untested) --- docs/teaching/exercises/answers/Answer4a.java | 7 +- docs/teaching/exercises/answers/Answer4b.java | 3 +- docs/teaching/exercises/answers/Answer4c.java | 3 +- docs/teaching/exercises/answers/Answer4d.java | 6 +- docs/teaching/exercises/answers/Answer4e.java | 6 +- .../exercises/figures/FigureElement.java | 4 +- docs/teaching/exercises/index.html | 68 +++++++------------ docs/teaching/exercises/tests/Test.java | 1 + docs/teaching/exercises/tests/Test4a.java | 12 +--- docs/teaching/exercises/tests/Test4b.java | 14 +--- docs/teaching/exercises/tests/Test4c.java | 7 -- docs/teaching/exercises/tests/Test4d.java | 4 -- docs/teaching/exercises/tests/Test4e.java | 4 -- 13 files changed, 45 insertions(+), 94 deletions(-) diff --git a/docs/teaching/exercises/answers/Answer4a.java b/docs/teaching/exercises/answers/Answer4a.java index fd0e2e684..e99eb1266 100644 --- a/docs/teaching/exercises/answers/Answer4a.java +++ b/docs/teaching/exercises/answers/Answer4a.java @@ -17,12 +17,7 @@ import figures.Group; import java.awt.Rectangle; aspect Answer4a { - private Rectangle wholeCanvas = - new Rectangle(FigureElement.MIN_VALUE, FigureElement.MIN_VALUE, - FigureElement.MAX_VALUE - FigureElement.MIN_VALUE, - FigureElement.MAX_VALUE - FigureElement.MIN_VALUE); - Rectangle around(): execution(Rectangle Group.getBounds()) { - return wholeCanvas; + return FigureElement.MAX_BOUNDS; } } diff --git a/docs/teaching/exercises/answers/Answer4b.java b/docs/teaching/exercises/answers/Answer4b.java index 4fd5354c1..33649c4e1 100644 --- a/docs/teaching/exercises/answers/Answer4b.java +++ b/docs/teaching/exercises/answers/Answer4b.java @@ -20,7 +20,8 @@ aspect Answer4b { private Rectangle Group.cache = null; Rectangle around(Group g): - execution(Rectangle Group.getBounds()) && this(g) { + execution(Rectangle Group.getBounds()) && this(g) + { if (g.cache == null) { g.cache = proceed(g); } diff --git a/docs/teaching/exercises/answers/Answer4c.java b/docs/teaching/exercises/answers/Answer4c.java index 35f4b90af..75e1123dd 100644 --- a/docs/teaching/exercises/answers/Answer4c.java +++ b/docs/teaching/exercises/answers/Answer4c.java @@ -20,7 +20,8 @@ aspect Answer4c { private Rectangle Group.cache = null; Rectangle around(Group g): - execution(Rectangle Group.getBounds()) && this(g) { + execution(Rectangle Group.getBounds()) && this(g) + { if (g.cache == null) { g.cache = proceed(g); } diff --git a/docs/teaching/exercises/answers/Answer4d.java b/docs/teaching/exercises/answers/Answer4d.java index f2127836a..58d31d44c 100644 --- a/docs/teaching/exercises/answers/Answer4d.java +++ b/docs/teaching/exercises/answers/Answer4d.java @@ -22,12 +22,14 @@ aspect Answer4d { private Group Point.enclosingGroup = null; before(Point p, Group g): - execution(void add(FigureElement)) && args(p) && target(g) { + execution(void add(FigureElement)) && args(p) && target(g) + { p.enclosingGroup = g; } Rectangle around(Group g): - execution(Rectangle Group.getBounds()) && this(g) { + execution(Rectangle Group.getBounds()) && this(g) + { if (g.cache == null) { g.cache = proceed(g); } diff --git a/docs/teaching/exercises/answers/Answer4e.java b/docs/teaching/exercises/answers/Answer4e.java index bab9e2afd..2a6e0aefd 100644 --- a/docs/teaching/exercises/answers/Answer4e.java +++ b/docs/teaching/exercises/answers/Answer4e.java @@ -22,12 +22,14 @@ aspect Answer4e { private Group FigureElement.enclosingGroup = null; before(FigureElement p, Group g): - execution(void add(FigureElement)) && args(p) && target(g) { + execution(void add(FigureElement)) && args(p) && target(g) + { p.enclosingGroup = g; } Rectangle around(Group g): - execution(Rectangle Group.getBounds()) && this(g) { + execution(Rectangle Group.getBounds()) && this(g) + { if (g.cache == null) { g.cache = proceed(g); } diff --git a/docs/teaching/exercises/figures/FigureElement.java b/docs/teaching/exercises/figures/FigureElement.java index 22bb579a9..26cbed272 100644 --- a/docs/teaching/exercises/figures/FigureElement.java +++ b/docs/teaching/exercises/figures/FigureElement.java @@ -17,8 +17,8 @@ import java.awt.*; import java.awt.geom.*; public interface FigureElement { - public static final int MIN_VALUE = 0; - public static final int MAX_VALUE = 500; + public static final Rectangle MAX_BOUNDS = + new Rectangle(0, 0, 500, 500); public abstract void move(int dx, int dy); diff --git a/docs/teaching/exercises/index.html b/docs/teaching/exercises/index.html index c69192e46..20671cdcf 100644 --- a/docs/teaching/exercises/index.html +++ b/docs/teaching/exercises/index.html @@ -638,50 +638,32 @@ handle those points contained in Lines and Boxes only if time permits.

a. Make a constant override

+

Problem: Pass tests.Test4a.

+ +

Tools: around, + FigureElement.MAX_BOUNDS +

+

Group's getBounds() method could be understood to be a conservative approximation of the bounding box of a group. If that is true, then it would be a legal (and much faster) implementation of getBounds() to simply always return a -rectangle consisting of the entire canvas, that is +rectangle consisting of the entire canvas. The entire canvas is returned +by the static method FigureElement.MAX_BOUNDS.

-
-new Rectangle(FigureElement.MIN_VALUE, FigureElement.MIN_VALUE, 
-              FigureElement.MAX_VALUE - FigureElement.MIN_VALUE,
-              FigureElement.MAX_VALUE - FigureElement.MIN_VALUE)
-
-

Write an aspect to implement this change. You can override Group's getBounds() method entirely with around advice intercepting the method.

-

Your code should pass the JUnit test case -tests.Test4a with this change. -

+

b. Make a constant cache

-

Answer: +

Problem: Pass tests.Test4b.

-
-package answers;
-
-import figures.*;
-import java.awt.Rectangle;
-
-aspect Answer4a {
-    private Rectangle wholeCanvas =
-        new Rectangle(FigureElement.MIN_VALUE, FigureElement.MIN_VALUE, 
-                      FigureElement.MAX_VALUE - FigureElement.MIN_VALUE,
-                      FigureElement.MAX_VALUE - FigureElement.MIN_VALUE);
-
-    Rectangle around(): execution(Rectangle Group.getBounds()) {
-        return wholeCanvas;
-    }
-}
-
- -

b. Make a constant cache

+

Tools: private Rectangle Group.mumble; +

Instead of making the (very) conservative approximation of getBounds() from part (a), write an aspect instead that @@ -693,25 +675,27 @@ call.

Hint: You can use an inter-type declaration to keep some state for every Group object.

-

Your code should pass the JUnit test case -tests.Test4b with this change. -

-

c. Invalidate, part 1

+

Problem: Pass tests.Test4c. +

+ +

Tools: before +

+

While caching in this way does save computation, it will lead to incorrect bounding boxes if a Group is ever moved. Change your aspect so that it invalidates the cache whenever the move() method of Group is called.

-

Your code should pass the JUnit test case -tests.Test4c with this change. -

-

d. Invalidate, part 2

+

Problem: Pass tests.Test4d.

+ +

Tools: your solution to 3c

+

Of course, part (c) didn't really solve the problem. What if a Point that is part of a Group moves? Whenever either of a Point's fields are set it should invalidate the @@ -720,12 +704,12 @@ modify your invalidation criteria in this way, but note that this is slightly different than the problem in 3c: Here you care about fields, where there you cared about method calls.

-

Your code should pass the JUnit test case -tests.Test4d with this change. -

-

e. Invalidate, part 3

+

Problem: Pass tests.Test4e.

+ +

Tools: You're on you're own

+

Did you really do part (d) correctly? Run the JUnit test tests.Test4e to see. If you pass, congratulations, now go help other people. Otherwise, you have fallen prey to our cruel diff --git a/docs/teaching/exercises/tests/Test.java b/docs/teaching/exercises/tests/Test.java index 17d8b12e6..d6d3bb4e6 100644 --- a/docs/teaching/exercises/tests/Test.java +++ b/docs/teaching/exercises/tests/Test.java @@ -18,6 +18,7 @@ import junit.framework.*; public class Test extends TestCase { public Test(String name) { super(name); } + public Test() {} public static void main(String[] args) { junit.textui.TestRunner.run(Test.class); diff --git a/docs/teaching/exercises/tests/Test4a.java b/docs/teaching/exercises/tests/Test4a.java index 7e8f0f234..f3644765f 100644 --- a/docs/teaching/exercises/tests/Test4a.java +++ b/docs/teaching/exercises/tests/Test4a.java @@ -18,21 +18,11 @@ import java.awt.Rectangle; import junit.framework.*; public class Test4a extends Test { - Rectangle wholeCanvas = - new Rectangle(FigureElement.MIN_VALUE, FigureElement.MIN_VALUE, - FigureElement.MAX_VALUE, FigureElement.MAX_VALUE); - - public Test4a(String name) { super(name); } - public static void main(String[] args) { junit.textui.TestRunner.run(Test4a.class); } - public void setUp() { - super.setUp(); - } - public void testGroupBounds() { - assertEquals(g.getBounds(), wholeCanvas); + assertEquals(g.getBounds(), FigureElement.MAX_BOUNDS); } } diff --git a/docs/teaching/exercises/tests/Test4b.java b/docs/teaching/exercises/tests/Test4b.java index 31bea5209..adc891008 100644 --- a/docs/teaching/exercises/tests/Test4b.java +++ b/docs/teaching/exercises/tests/Test4b.java @@ -18,17 +18,10 @@ import java.awt.Rectangle; import junit.framework.*; public class Test4b extends Test { - - public Test4b(String name) { super(name); } - public static void main(String[] args) { junit.textui.TestRunner.run(Test4b.class); } - public void setUp() { - super.setUp(); - } - public void testBasicEquality() { assertTrue(g.getBounds() == g.getBounds()); } @@ -58,12 +51,9 @@ public class Test4b extends Test { public void testNotWholeCanvas() { assertTrue("bounds for this group should not be the whole canvas", - g.getBounds().getWidth() < - (FigureElement.MAX_VALUE - FigureElement.MIN_VALUE)); + g.getBounds().getWidth() < FigureElement.MAX_BOUNDS.getWidth()); assertTrue("bounds for this group should not be the whole canvas", - g.getBounds().getHeight() < - (FigureElement.MAX_VALUE - FigureElement.MIN_VALUE)); - + g.getBounds().getHeight() < FigureElement.MAX_BOUNDS.getHeight()); } } diff --git a/docs/teaching/exercises/tests/Test4c.java b/docs/teaching/exercises/tests/Test4c.java index 1f30a9ae3..c4c5928ce 100644 --- a/docs/teaching/exercises/tests/Test4c.java +++ b/docs/teaching/exercises/tests/Test4c.java @@ -18,17 +18,10 @@ import java.awt.Rectangle; import junit.framework.*; public class Test4c extends Test { - - public Test4c(String name) { super(name); } - public static void main(String[] args) { junit.textui.TestRunner.run(Test4c.class); } - public void setUp() { - super.setUp(); - } - public void testBasicEquality() { assertTrue(g.getBounds() == g.getBounds()); } diff --git a/docs/teaching/exercises/tests/Test4d.java b/docs/teaching/exercises/tests/Test4d.java index c1871cfd0..234422235 100644 --- a/docs/teaching/exercises/tests/Test4d.java +++ b/docs/teaching/exercises/tests/Test4d.java @@ -25,10 +25,6 @@ public class Test4d extends Test { junit.textui.TestRunner.run(Test4d.class); } - public void setUp() { - super.setUp(); - } - public void testBasicEquality() { assertTrue(g.getBounds() == g.getBounds()); } diff --git a/docs/teaching/exercises/tests/Test4e.java b/docs/teaching/exercises/tests/Test4e.java index 91710b0ea..3fe4689ee 100644 --- a/docs/teaching/exercises/tests/Test4e.java +++ b/docs/teaching/exercises/tests/Test4e.java @@ -25,10 +25,6 @@ public class Test4e extends Test { junit.textui.TestRunner.run(Test4e.class); } - public void setUp() { - super.setUp(); - } - public void testBasicEquality() { assertTrue(g.getBounds() == g.getBounds()); } -- 2.39.5