Advertisement
Guest User

Untitled

a guest
Nov 19th, 2010
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import javax.swing.JOptionPane;
  4.  
  5. public class A21 {
  6. public static void main(String[] args) {
  7. LinkedList list = new LinkedList(); // instantiate LinkedList list object
  8. File file = new File(args[0]); // read file
  9.  
  10. String name = new String();
  11. Integer age = new Integer(0);
  12.  
  13. Scanner scan = null;
  14. try {
  15. scan = new Scanner(file).useDelimiter("\\s*,\\s*"); // set scanner's delimiting pattern to the specified pattern.
  16. if(scan != null) {
  17. while(scan.hasNext() || scan.hasNextInt() || scan.hasNextLine()) {
  18. name = scan.next(); // search name
  19. age = scan.nextInt(); // search age
  20. name = name.replace(",", ""); // replace commas stored in String age
  21. if(!list.equals("name, age")) {
  22. list.add(name, age); // add to list
  23. }
  24. }
  25. }
  26.  
  27. // A19 component
  28. JOptionPane.showMessageDialog(null, list.toString()); // print unsorted persons
  29.  
  30. // A21 components
  31. list.bubbleSort(); // sort
  32. JOptionPane.showMessageDialog(null, list.toString()); // print sorted persons
  33.  
  34. } // end of try-catch
  35. catch(FileNotFoundException fnfe) {
  36. JOptionPane.showMessageDialog(null, "ERROR: " + fnfe.getMessage());
  37. }
  38. catch(InputMismatchException ime) {
  39. JOptionPane.showMessageDialog(null, "ERROR: " + ime.getMessage());
  40. }
  41. catch(NoSuchElementException nsee) {
  42. JOptionPane.showMessageDialog(null, "ERROR: " + nsee.getMessage());
  43. }
  44. catch(NullPointerException npe) {
  45. JOptionPane.showMessageDialog(null, "ERROR: " + npe.getMessage());
  46. }
  47. catch(NumberFormatException nfe) {
  48. JOptionPane.showMessageDialog(null, "ERROR: " + nfe.getMessage());
  49. }
  50. } // end of main()
  51. } // end
  52.  
  53. /**
  54. * Implementments the PersonNode.
  55. * @author Jung Bum Lee
  56. */
  57. class LinkedList{
  58. protected PersonNode head;
  59. protected String output;
  60. protected Integer size;
  61.  
  62. /**
  63. * Initialize PersonNode head, String output, and Integer size objects.
  64. */
  65. public LinkedList() {
  66. head = null;
  67. output = new String();
  68. size = new Integer(0);
  69. }
  70.  
  71. /**
  72. * Adds String name and Integer age from PersonNode to list
  73. * @param name is the object that is added to list
  74. */
  75. public void add(String name, Integer age) {
  76. if (head == null) {
  77. head = new PersonNode(name, age, null); // assign name and age from PersonNode to an empty list
  78. }
  79. else {
  80. PersonNode previous = head; // add to the end of the list
  81. PersonNode current = head.getNext(); // current getNext() new name and age from PersonNode
  82. while (current != null) {
  83. previous = current; // swap previous with current PersonNode object
  84. current = current.getNext(); // current getNext() name and age from PersonNode
  85. }
  86. PersonNode node = new PersonNode(name, age, null); // create a new PersonNode object
  87. previous.setNext(node); // set list as new PersonNode object
  88. }
  89. size++; // increase size
  90. } //end of add()
  91.  
  92. /**
  93. * Displays the data stored in the data fields of name and age (in years) objects.
  94. * @return "[object name] is [object age] years old."
  95. */
  96. public String toString() {
  97. String message = new String();
  98. for(PersonNode current = head; current != null; current = current.getNext()) {
  99. message = message + " " + current.toString() + "\n";
  100. }
  101. return message;
  102. }
  103.  
  104. /**
  105. * A sorting algorithm that works by repeatedly stepping through the list to be sorted,
  106. * comparing each pair of adjacent items and swapping them if they are in the wrong
  107. * order.
  108. */
  109. public void bubbleSort() {
  110. String name = new String();
  111. Integer age = new Integer(0);
  112. boolean swap = false; // default swap
  113.  
  114. //loop stops when swap not needed, list ordered
  115. while(!swap) {
  116. swap = true;
  117. for(PersonNode current = head; current.getNext() != null; current = current.getNext()){
  118. if(current.getAge() > current.getNext().getAge()){
  119. name = current.getName();
  120. age = current.getAge();
  121.  
  122. current.setName(current.getNext().getName());
  123. current.setAge(current.getNext().getAge());
  124.  
  125. current.getNext().setName(name);
  126. current.getNext().setAge(age);
  127.  
  128. swap = false;
  129. }
  130. }
  131. }
  132. }
  133. } // end of LinkedList()
  134.  
  135. /**
  136. * Stores the reference to Data and the next Node.
  137. */
  138. class PersonNode {
  139. protected String name; // instantiate String name data field
  140. protected Integer age; // instantiate Integer age data field
  141. protected PersonNode next; // instantiate PersonNode next data field
  142.  
  143. /**
  144. * Constructor that is used to create each object and
  145. * initialize data fields.
  146. * @param name2 initializes the name reference variable.
  147. * @param age2 initializes the age reference variable.
  148. * @param next2 initializes the next reference variable.
  149. */
  150. public PersonNode(String name2, Integer age2, PersonNode next2) {
  151. name = name2;
  152. age = age2;
  153. next = next2;
  154. } // end of PersonNode()
  155.  
  156. /**
  157. * Displays the data stored in the data fields of name and age (in years) objects.
  158. * @return "[object name] is [object age] years old."
  159. */
  160. public String toString() {
  161. return name + " is " + age + " years old.";
  162. } // end of toString()
  163.  
  164. /**
  165. * Accessor method that is used to get data fields.
  166. * @return the address to the next node
  167. */
  168. public PersonNode getNext() {
  169. return next;
  170. } // end of getNext()
  171.  
  172. /**
  173. * Mutator method that is used to set data fields.
  174. * @param next2 is a pointer to the next node.
  175. */
  176. public void setNext(PersonNode next2) {
  177. next = next2;
  178. } // end of setNext()
  179.  
  180. /**
  181. * Accessor method that is used to get data fields.
  182. * @return the address to the next node
  183. */
  184. public String getName() {
  185. return name;
  186. } // end of getName()
  187.  
  188. /**
  189. * Mutator method that is used to set data fields.
  190. * @param next2 is a pointer to the next node.
  191. */
  192. public void setName(String name2) {
  193. name = name2;
  194. } // end of setName()
  195.  
  196. /**
  197. * Accessor method that gets the state of an object to be
  198. * accessed (retrieved) from other parts of a program.
  199. * @return String age
  200. */
  201. public Integer getAge() {
  202. return age;
  203. } // end of getAge()
  204.  
  205. /**
  206. * Mutator method that is used to set data fields.
  207. * @param next2 is a pointer to the next node.
  208. */
  209. public void setAge(Integer age2) {
  210. age = age2;
  211. } // end of setAge()
  212. } // end of PersonNode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement