944,117 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 521
  • Java RSS
Nov 2nd, 2009
0

Anonymous class

Expand Post »
I have this code, but instead of implementing class enumeration I want to create an anonymous class in the method getEnumeration that does the job right in the return, hwo do I do that?
Java Syntax (Toggle Plain Text)
  1. // File: TestList.java (Module 10)
  2. //
  3. // Author: Rahul Simha
  4. // Created: Nov 2, 1998
  5. //
  6. // Starting point for Ex. 10.1: an anonymous class
  7. // implementation of an Enumeration.
  8. // The current file has the list implement the enumeration.
  9.  
  10. import java.util.*;
  11.  
  12. abstract class ComparableObject {
  13. public abstract String toString ();
  14. public abstract int compare (ComparableObject c);
  15. }
  16.  
  17. class ListItem {
  18.  
  19. ComparableObject data = null;
  20. ListItem next = null;
  21.  
  22. // Constructor.
  23. public ListItem (ComparableObject obj)
  24. {
  25. data = obj; next = null;
  26. }
  27.  
  28. // Accessor.
  29. public ComparableObject getData ()
  30. {
  31. return data;
  32. }
  33. }
  34.  
  35.  
  36. // LinkedList now implements Enumeration itself.
  37.  
  38. class LinkedList implements Enumeration {
  39.  
  40. ListItem front = null;
  41. ListItem rear = null;
  42. int numItems = 0; // Current number of items.
  43.  
  44. // Instance method to add a data item.
  45. public void addData (ComparableObject obj)
  46. {
  47. if (front == null) {
  48. front = new ListItem (obj);
  49. rear = front;
  50. }
  51. else {
  52. // Find the right place.
  53. ListItem tempPtr=front, prevPtr=front;
  54. boolean found = false;
  55. while ( (!found) && (tempPtr != null) ) {
  56. if (tempPtr.data.compare(obj) > 0) {
  57. found = true;
  58. break;
  59. }
  60. prevPtr = tempPtr;
  61. tempPtr = tempPtr.next;
  62. }
  63. // Now insert.
  64. if (!found) { // Insert at rear.
  65. rear.next = new ListItem (obj);
  66. rear = rear.next;
  67. }
  68. else if (tempPtr == front) { // Insert in front.
  69. ListItem Lptr = new ListItem (obj);
  70. Lptr.next = front;
  71. front = Lptr;
  72. }
  73. else { // Insert in the middle.
  74. ListItem Lptr = new ListItem (obj);
  75. prevPtr.next = Lptr;
  76. Lptr.next = tempPtr;
  77. }
  78. }
  79. numItems++;
  80. }
  81.  
  82. public void printList ()
  83. {
  84. ListItem listPtr = front;
  85. System.out.println ("List: (" + numItems + " items)");
  86. int i = 1;
  87. while (listPtr != null) {
  88. System.out.println ("Item# " + i + ": "
  89. + listPtr.getData());
  90. // Must implement toString()
  91. i++;
  92. listPtr = listPtr.next;
  93. }
  94. }
  95.  
  96. ListItem enumPtr;
  97.  
  98. // Must implement this method.
  99. public boolean hasMoreElements ()
  100. {
  101. if (enumPtr == null)
  102. return false;
  103. else
  104. return true;
  105. }
  106.  
  107. // Must implement this method.
  108. public Object nextElement()
  109. {
  110. Object obj = enumPtr.data;
  111. enumPtr = enumPtr.next;
  112. return obj;
  113. }
  114.  
  115. // This is needed to return an Enumeration
  116. // instance to the user.
  117. public Enumeration getEnumeration ()
  118. {
  119. enumPtr = front;
  120. return this; // Using the "this" reserved word.
  121. }
  122.  
  123. } // End of class "LinkedList"
  124.  
  125.  
  126. // An object to use in the list:
  127.  
  128. class Person extends ComparableObject {
  129.  
  130. String name;
  131. String ssn;
  132.  
  133. // Constructor.
  134. public Person (String nameIn, String ssnIn)
  135. {
  136. name = nameIn; ssn = ssnIn;
  137. }
  138.  
  139. // Override toString()
  140. public String toString ()
  141. {
  142. return "Person: name=" + name + ", ssn=" + ssn;
  143. }
  144.  
  145. // Must implement compare
  146. public int compare (ComparableObject obj)
  147. {
  148. Person p = (Person) obj;
  149. return name.compareTo (p.name);
  150. }
  151.  
  152. } // End of class "Person"
  153.  
  154.  
  155. // Test class.
  156.  
  157. public class TestList {
  158.  
  159. public static void main (String[] argv)
  160. {
  161. // Create a new list object.
  162. LinkedList L = new LinkedList ();
  163.  
  164. // Create a Person instance and add it to list.
  165. L.addData (new Person ("Terminator", "444-43-4343"));
  166. L.addData (new Person ("James Bond", "666-65-6565"));
  167. L.addData (new Person ("Rambo", "555-54-5454"));
  168. L.addData (new Person ("Bruce Lee", "777-76-7676"));
  169.  
  170. // Print contents via an Enumeration.
  171. Enumeration e = L.getEnumeration();
  172. while (e.hasMoreElements())
  173. {
  174. Person p = (Person) e.nextElement();
  175. System.out.println (p);
  176. }
  177.  
  178. }
  179.  
  180. } // End of class "TestList"
Similar Threads
Reputation Points: 46
Solved Threads: 1
Junior Poster in Training
Samyx is offline Offline
73 posts
since Sep 2009
Nov 2nd, 2009
0
Re: Anonymous class
Just like any anonymous implemenation
Java Syntax (Toggle Plain Text)
  1. return new Enumeration() {
  2.  
  3. public boolean hasMoreElements() {
  4. throw new UnsupportedOperationException("Not supported yet.");
  5. }
  6.  
  7. public Object nextElement() {
  8. throw new UnsupportedOperationException("Not supported yet.");
  9. }
  10. };
Creating a small private inner class for the enumeration and returning an instance of that would be better though. You can see that idiom used quite a lot in the JDK classes.

Also keep in mind that Iterator is generally preferred to Enumeration these days.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 2nd, 2009
0
Re: Anonymous class
Thanks! Solved!
Reputation Points: 46
Solved Threads: 1
Junior Poster in Training
Samyx is offline Offline
73 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: hangman on the phone problem Java
Next Thread in Java Forum Timeline: netbeans troubles





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC