01: package U2.T2.examples;
02:
03: /**
04: * A simple class representing clubmembers. A clubmember may have a
05: * sponsor, which is another clubmember. So, this class is
06: * recursive. We impose as a class invariant that the sponsor relation
07: * should not be circular.
08: *
09: */
10: public class ClubMember {
11:
12: public String name;
13: private ClubMember sponsor;
14:
15: /**
16: * Make a new ClubMember with the specified name. The sponsor is
17: * set to null.
18: */
19: public ClubMember(String n) {
20: name = n;
21: sponsor = null;
22: }
23:
24: /**
25: * The class invariant, saying the sponsor chain is not circular.
26: */
27: public boolean classinv() {
28: ClubMember t = this .sponsor;
29: while (t != null && t != this )
30: t = t.sponsor;
31: // System.out.println("> " + (t != this)) ;
32: return t != this ;
33: }
34:
35: public String getName() {
36: return name;
37: }
38:
39: /**
40: * Contains an error. Should impose a pre-cond that the sponsor
41: * field is not null.
42: */
43: public String getSponsor() {
44: return sponsor.name;
45: }
46:
47: /**
48: * Set s as the sponsor of this ClubMember. The method contains
49: * error, e.g. it allows the clubmember itself to be the sponsor
50: * (violating the class invariant).
51: */
52: public void sponsoredBy(ClubMember s) {
53: sponsor = s;
54: }
55:
56: /*
57: static public void main(String[] args) {
58:
59: ClubMember x = new ClubMember("x") ;
60: ClubMember y = new ClubMember("y") ;
61: x.toSponsor(y) ;
62: }
63: */
64:
65: }
|