Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. public abstract class Concept
  2. {
  3. private String id;
  4.  
  5. protected Concept( String anId )
  6. {
  7. if ( anId == null )
  8. {
  9. throw new NullPointerException( "id must not be null" );
  10. }
  11.  
  12. id = anId;
  13. }
  14.  
  15. public String getId()
  16. {
  17. return id;
  18. }
  19.  
  20. public void setId( final String id ) //changed
  21. {
  22. this.id = id;
  23. }
  24.  
  25. public boolean equals( Object other )
  26. {
  27. return other != null && other.getClass().equals( getClass() ) && id.equals( ( (Concept) other ).id );
  28. }
  29.  
  30. public String toString()
  31. { return "Concept(" + id + ")";
  32. }
  33. }
  34.  
  35. public class ConceptA extends Concept
  36. {
  37. private final Concept parent;
  38.  
  39. public ConceptA( String anId, Concept aParent )
  40. {
  41. super( anId );
  42.  
  43. parent = aParent;
  44. }
  45.  
  46. public Concept getParent()
  47. {
  48. return parent;
  49. }
  50.  
  51. public String toString()
  52. {
  53. return "ConceptA{" + getId() + ", parent=" + parent + '}';
  54. }
  55. }
  56.  
  57. import java.util.Set;
  58. import java.util.HashSet;
  59. import java.util.Iterator;
  60.  
  61. public class ConceptB extends ConceptA
  62. {
  63. private final Set children;
  64.  
  65. public ConceptB( final String anId, final Concept aParent )
  66. {
  67. super( anId, aParent );
  68.  
  69. children = new HashSet();
  70. }
  71.  
  72. public int getCount()
  73. {
  74. return children.size();
  75. }
  76.  
  77. public void addChild( Concept aChild )
  78. {
  79. children.add( aChild );
  80. }
  81.  
  82. public void removeChild( Concept aChild )
  83. {
  84. children.remove( aChild );
  85. }
  86.  
  87. public Iterator getChildren()
  88. {
  89. return children.iterator();
  90. }
  91.  
  92. public int getFamilySize()
  93. {
  94. int count = children.size();
  95.  
  96. for ( Iterator iter = getChildren(); iter.hasNext(); )
  97. {
  98. count += ( (ConceptB) iter.next() ).getFamilySize();
  99. }
  100.  
  101. return count;
  102. }
  103.  
  104. public int getAncestorCount()
  105. {
  106. int count = 0;
  107. Concept ancestor = getParent();
  108.  
  109. while ( ancestor != null )
  110. {
  111. count++;
  112. if ( ancestor instanceof ConceptA )
  113. {
  114. ancestor = ( (ConceptA) ancestor ).getParent();
  115. }
  116. else
  117. {
  118. ancestor = null;
  119. }
  120. }
  121.  
  122. return count;
  123. }
  124.  
  125. public String toString()
  126. {
  127. return "ConceptB{" + getId() + ", parent=" + getParent() + ", children=" + children.size() + "}";
  128. }
  129. }
  130.  
  131. package com.result.exam.a;
  132.  
  133. public class ConceptC extends ConceptA
  134. {
  135. private static int nextSerialNo = 0;
  136.  
  137. public static int getNextSerialNo()
  138. {
  139. return nextSerialNo++;
  140. }
  141.  
  142. private final int serialNo;
  143.  
  144. public ConceptC( String anId )
  145. {
  146. super( anId, null );
  147.  
  148. serialNo = getNextSerialNo();
  149. }
  150.  
  151. public int getSerialNo()
  152. {
  153. return serialNo;
  154. }
  155.  
  156. public String toString()
  157. {
  158. return "ConceptC(" + getId() + ", " + serialNo + ")";
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement