Subject: Extra Credit
From: John Sung (john_j_sung@yahoo.com)
Date: Sun Feb 17 2002 - 15:49:33 EST
Sorry about sending this out late.
Here's the Extra Credit problem to make up for points lost in homework2
Question 2 part 1. This is good for extra 20 points towards your homework
grade. Anyone that wishes to get extra credit may do the problem and hand
them in.
Submition: Please submit via E-MAIL ATTACHMENT, the Main.java file. All
other methods of submition will be IGNORED.
Due Date: Sunday, Feb. 24th, 2002, 11:59pm.
Late Policy: No Late Submition Accepted. Only for Special Circumstances.
Grading Policy: Incorrect Traversal -10, Does not compile -5.
Implement the traversal in Main.java using the traversal and visitor pattern
in pure java, without violating the Law of Demeter. Please note the
difference in the Class Graph of the program from Homework2 Question 2 Part
1.
// Main.java
import java.util.*;
import edu.neu.ccs.demeter.dj.*;
class Main{
static public void main(String args[]) {
A a = new A (
new B(
new D(
new E(
new F(7)
),
new X( new F(9))
)
), new C(),
new X( new F(10)));
ClassGraph cg = new ClassGraph(true, false);
cg.traverse(a, "from A through X to F", new MyPrintVisitor());
}
}
class A{
A(B b, C c, C c2) {
this.b = b;
this.c = c;
this.c2 = c2;
}
B b;
C c, c2;
}
class B {
B(D d) { this.d = d; }
D d;
}
class C {
C(E ei) { e = ei;}
C() { e = null;}
E e;
}
class D {
D(E ei, X xi){ e = ei; x = xi;}
E e;
X x;
}
class E {
E(F fi){ f = fi;}
F f;
}
class F {
F(int ii){ i = ii;}
int i;
}
class X extends C {
X(F fi, E ei) { super(ei); f = fi; }
X(F fi){ super(null); f = fi; }
F f;
}
class MyPrintVisitor extends Visitor{
MyPrintVisitor() {}
void before(A a) {
System.out.println("Before A");
}
void before(B b) {
System.out.println("Before B");
}
void before(C c) {
System.out.println("Before C");
}
void before(D d) {
System.out.println("Before D");
}
void before(E e) {
System.out.println("Before E");
}
void before(X x) {
System.out.println("Before X");
}
void before(F f) {
System.out.println("Before F");
}
}
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
This archive was generated by hypermail 2b28 : Sun Feb 17 2002 - 15:48:58 EST