Hello all
I have 4 classes that 'll be the package com.result.exam.a.Please correct it .if it has any syntax error.
com/result/exam/a/Concept.java
1package com.result.exam.a;
2
3public abstract class Concept
4{
5 private
String id;
6
7 protected Concept( String anId )
8 {
9 if ( anId == null )
10 {
11 throw new NullPointerException( "id must not be null" );
12 }
13
14 id = anId;
15 }
16
17 public String getId()
18 {
19 return id;
20 }
21
22 public void setId( final String id )
23 {
24 id = id;
25 }
26
27 public boolean equals( Object other )
28 {
29 return other != null && other.getClass().equals( getClass() ) && id.equals( ( (Concept) other ).id );
30 }
31
32 public String toString()
33 {
34 return "Concept(" + id + ")";
35 }
36}
com/result/exam/a/ConceptA.java
1package com.result.exam.a;
2
3public class ConceptA extends Concept
4{
5 private final Concept parent;
6
7 public ConceptA( String anId, Concept aParent )
8 {
9 super( anId );
10
11 parent = aParent;
12 }
13
14 public Concept getParent()
15 {
16 return parent;
17 }
18
19 public String toString()
20 {
21 return "ConceptA{" + getId() + ", parent=" + parent + '}';
22 }
23}
com/result/exam/a/ConceptB.java
1package com.result.exam.a;
2
3import java.util.Set;
4import java.util.HashSet;
5import java.util.Iterator;
6
7public class ConceptB extends ConceptA
8{
9 private final Set children;
10
11 public ConceptB( final String anId, final Concept aParent )
12 {
13 super( anId, aParent );
14
15 children = new HashSet();
16 }
17
18 public int getCount()
19 {
20 return children.size();
21 }
22
23 public void addChild( Concept aChild )
24 {
25 children.add( aChild );
26 }
27
28 public void removeChild( Concept aChild )
29 {
30 children.remove( aChild );
31 }
32
33 public Iterator getChildren()
34 {
35 return children.iterator();
36 }
37
38 public int getFamilySize()
39 {
40 int count = children.size();
41
42 for ( Iterator iter = getChildren(); iter.hasNext(); )
43 {
44 count += ( (ConceptB) iter.next() ).getFamilySize();
45 }
46
47 return count;
48 }
49
50 public int getAncestorCount()
51 {
52 int count = 0;
53 Concept ancestor = getParent();
54
55 while ( ancestor != null )
56 {
57 count++;
58 if ( ancestor instanceof ConceptA )
59 {
60 ancestor = ( (ConceptA) ancestor ).getParent();
61 }
62 else
63 {
64 ancestor = null;
65 }
66 }
67
68 return count;
69 }
70
71 public String toString()
72 {
73 return "ConceptB{" + getId() + ", parent=" + getParent() + ", children=" + children.size() + "}";
74 }
75}
com/result/exam/a/ConceptC.java
1package com.result.exam.a;
2
3public class ConceptC extends ConceptA
4{
5 private static int nextSerialNo = 0;
6
7 public static int getNextSerialNo()
8 {
9 return nextSerialNo++;
10 }
11
12 private final int serialNo;
13
14 public ConceptC( String anId )
15 {
16 super( anId, null );
17
18 serialNo = getNextSerialNo();
19 }
20
21 public int getSerialNo()
22 {
23 return serialNo;
24 }
25
26 public String toString()
27 {
28 return "ConceptC(" + getId() + ", " + serialNo + ")";
29 }
30}
Thanks in advance.
Regards
Rashmi