Advertisement
lgfjj

Program Assignment 6

Apr 30th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1.  
  2. package prgAssignment;
  3.  
  4. public class GradClass
  5. {
  6. private GradNode head;
  7.  
  8. public GradClass()
  9. {
  10. head = new GradNode(0);
  11. }
  12.  
  13. public boolean isEmpty()
  14. {
  15. if (head==null)
  16. return true;
  17. else
  18. return false;
  19. }
  20.  
  21. public void printList()
  22. {
  23. GradNode trav = head;
  24. while (trav!=null)
  25. {
  26. trav.displayNode();
  27. trav = trav.next;
  28. }
  29. }
  30.  
  31. public void delete(Object newData)
  32. {
  33. GradNode previous,current;
  34. GradNode temp = new GradNode(newData);
  35. boolean found = false;
  36. current = head;
  37. previous = null;
  38. String key = (String) newData;
  39. while (!found&& current !=null)
  40. {
  41. String firstValue = (String) current.data;
  42. if ((key).compareTo(firstValue)==0)
  43. found = true;
  44. else {
  45. previous = current;
  46. current = current.next;
  47. }
  48. }
  49. temp.next = current;
  50. if (previous==null)
  51. head = temp;
  52. else
  53. previous.next = current.next;
  54. }
  55.  
  56. public void insertInOrder(Object newData)
  57. {
  58. GradNode previous,current;
  59. GradNode temp = new GradNode(newData);
  60. boolean found = false;
  61. current = head;
  62. previous = null;
  63. String key = (String) newData;
  64.  
  65. while(!found && current!=null)
  66. {
  67. String firstValue = (String) current.data;
  68. if ((key).compareTo(firstValue)<0)
  69. found = true;
  70. else
  71. {
  72. previous = current;
  73. current = current.next;
  74. }
  75.  
  76.  
  77. }
  78. temp.next = current;
  79. if (previous==null)
  80. head = temp;
  81. else
  82. previous.next = temp;
  83. }
  84.  
  85. public Object getFirst()
  86. {
  87. return head.data;
  88. }
  89.  
  90. public void add(Object newData)
  91. {
  92. GradNode temp = new GradNode(newData);
  93. if (head==null)
  94. head = temp;
  95. else {
  96. temp.next = head;
  97. head = temp;}
  98.  
  99. }
  100.  
  101. public boolean search(Object newData)
  102. {
  103. GradNode trav = head;
  104. boolean found = false;
  105. while (trav!=null&&found==false)
  106. {
  107. if(trav.data.equals(newData))
  108. found = true;
  109. trav = trav.next;
  110. }
  111. return found;
  112. }
  113. }
  114. package prgAssignment;
  115.  
  116. public class GradNode
  117. {
  118. public Object data;
  119. public GradNode next;
  120.  
  121. public GradNode(Object newData)
  122. {
  123. data = newData;
  124. next = null;
  125. }
  126.  
  127. public void displayNode()
  128. {
  129. System.out.print(data + "->");
  130. }
  131. }
  132. package prgAssignment;
  133.  
  134. import java.io.FileReader;
  135. import java.io.IOException;
  136. import java.util.Scanner;
  137.  
  138. public class GradDemo
  139. {
  140. public static void main ( String [ ] args) throws IOException
  141. {
  142. GradClass cookies = new GradClass();
  143. Scanner scnr = new Scanner (System.in);
  144. GradClass Scharff = new GradClass();
  145. GradClass Chiou = new GradClass();
  146.  
  147. while (!false)
  148. {
  149. System.out.println("Enter a name to Add Print Delete or Search");
  150.  
  151. String a = scnr.next();
  152. String b = scnr.next();
  153.  
  154. String emptyString = scnr.nextLine();
  155.  
  156. if(a.equals("Add"))
  157. {
  158. System.out.print("What is this student's major");
  159. String major = scnr.next();
  160. if (major.equals("CS"))
  161. Scharff.add(b);
  162. else if (major.equals("IS"))
  163. Chiou.add(b);
  164. else
  165. System.out.println("That student is not in the Seidenberg School of Information Systems");
  166. }
  167.  
  168. if(a.equals("Print"))
  169. {
  170. if (b.equals("Scharff"))
  171. Scharff.printList();
  172. else if (b.equals("Chiou"))
  173. Chiou.printList();
  174. else
  175. System.out.println("Does not exist");
  176. }
  177.  
  178. if (a.equals("Search"))
  179. {
  180. if (Scharff.search(b))
  181. System.out.println("Student's advisor is Scharff");
  182. else if (Chiou.search(b))
  183. System.out.println("Student's advisor is Chiou");
  184. else
  185. System.out.println("Does not exist");
  186. }
  187.  
  188. if (a.equals("Delete"))
  189. {
  190. if (Scharff.search(b))
  191. Scharff.delete(b);
  192. else if(Chiou.search(b))
  193. Chiou.delete(b);
  194. else
  195. System.out.println("Does not exist");
  196. }
  197. }
  198. }
  199.  
  200. }
  201.  
  202. Enter a name to Add Print Delete or Search
  203. Add Kevin
  204. What is this student's majorCS
  205. Enter a name to Add Print Delete or Search
  206. Add Devin
  207. What is this student's majorCS
  208. Enter a name to Add Print Delete or Search
  209. Add Devon
  210. What is this student's majorIS
  211. Enter a name to Add Print Delete or Search
  212. Add Aleya
  213. What is this student's majorCS
  214. Enter a name to Add Print Delete or Search
  215. Add Patrick
  216. What is this student's majorCS
  217. Enter a name to Add Print Delete or Search
  218. Add Zach
  219. What is this student's majorIS
  220. Enter a name to Add Print Delete or Search
  221. Add Scott
  222. What is this student's majorCS
  223. Enter a name to Add Print Delete or Search
  224. Add Kenny
  225. What is this student's majorIS
  226. Enter a name to Add Print Delete or Search
  227. Add Josh
  228. What is this student's majorIS
  229. Enter a name to Add Print Delete or Search
  230. Add Ty
  231. What is this student's majorCS
  232. Enter a name to Add Print Delete or Search
  233. Print Scharff
  234. Ty->
  235. Scott->
  236. Patrick->
  237. Aleya->
  238. Devin->
  239. Kevin->
  240. 0->
  241. Enter a name to Add Print Delete or Search
  242. Search
  243. Ty
  244. System.out.println("Student's advisor is Scharff");
  245. Enter a name to Add Print Delete or Search
  246. Delete Kevin
  247. Enter a name to Add Print Delete or Search
  248. Print Scharff
  249. Ty->
  250. Scott->
  251. Patrick->
  252. Aleya->
  253. Devin->
  254. 0->
  255. Enter a name to Add Print Delete or Search
  256.  
  257. PART B---------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement