Anonymous class

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 62
Reputation: Samyx is on a distinguished road 
Solved Threads: 1
Samyx Samyx is offline Offline
Junior Poster in Training

Anonymous class

 
0
  #1
Nov 2nd, 2009
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?
  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"
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #2
Nov 2nd, 2009
Just like any anonymous implemenation
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 62
Reputation: Samyx is on a distinguished road 
Solved Threads: 1
Samyx Samyx is offline Offline
Junior Poster in Training
 
0
  #3
Nov 2nd, 2009
Thanks! Solved!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 296 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC