Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. // Assignment #: Arizona State University CSE205 #10
  2. // Name: Your name
  3. // StudentID: Your id
  4. // Lecture: Your lecture
  5. // Description: The Assignment 10 class displays a menu of choices to a user
  6. // and performs the chosen task. It will keep asking a user to
  7. // enter the next choice until the choice of 'Q' (Quit) is
  8. // entered.
  9.  
  10. import java.io.*;
  11.  
  12. public class Assignment9
  13. {
  14. public static void main(String[] args)
  15. {
  16. char input1;
  17. String inputInfo = new String();
  18. int operation2;
  19. String line = new String();
  20.  
  21. //create a linked list to be used in this method.
  22. LinkedList list1 = new LinkedList();
  23.  
  24. try
  25. {
  26. // print out the menu
  27. printMenu();
  28.  
  29. // create a BufferedReader object to read input from a keyboard
  30. InputStreamReader isr = new InputStreamReader (System.in);
  31. BufferedReader stdin = new BufferedReader (isr);
  32.  
  33. do
  34. {
  35. System.out.print("What action would you like to perform?\n");
  36. line = stdin.readLine().trim(); //read a line
  37. input1 = line.charAt(0);
  38. input1 = Character.toUpperCase(input1);
  39.  
  40. if (line.length() == 1) //check if a user entered only one character
  41. {
  42. switch (input1)
  43. {
  44. case 'O': // List Current Size
  45. System.out.print("The current size is " + list1.size() + "\n");
  46. break;
  47.  
  48. case 'A': //Add String
  49. System.out.print("Please enter a string to add:\n");
  50. String str1 = stdin.readLine().trim();
  51. System.out.print("Please enter an index to add:\n");
  52. inputInfo = stdin.readLine().trim();
  53. int addIndex = Integer.parseInt(inputInfo);
  54. list1.insertElement(addIndex, str1);
  55. System.out.print(str1 + " is inserted at index " + addIndex + "\n");
  56. break;
  57.  
  58. case 'I': //Search for the Index of a String
  59. System.out.print("Please enter a string to search:\n");
  60. inputInfo = stdin.readLine().trim();
  61. //operation2=list1.searchElement(inputInfo);
  62. // if (operation2 > -1)
  63. // System.out.print(inputInfo + " found at index " + operation2 + "\n");
  64. //else
  65. System.out.print(inputInfo + " not found\n");
  66. break;
  67.  
  68. case 'E': //Search for String at an Index
  69. System.out.print("Please enter an index to search:\n");
  70. inputInfo = stdin.readLine().trim();
  71. int searchIndex = Integer.parseInt(inputInfo);
  72. //System.out.print("string at index " + searchIndex + " is " + list1.getElement(searchIndex) + "\n");
  73. break;
  74.  
  75. case 'S': //Set a new element at specified index
  76. System.out.print("Please enter a new string to set:\n");
  77. String str2 = stdin.readLine().trim();
  78. System.out.print("Please enter an index to set:\n");
  79. inputInfo = stdin.readLine().trim();
  80. int setIndex = Integer.parseInt(inputInfo);
  81. list1.setElement(setIndex, str2);
  82. System.out.print(str2 + " is set at index " + setIndex + "\n");
  83. break;
  84.  
  85. case 'R': //Remove an element at a specified index
  86. System.out.print("Please enter an index to remove:\n");
  87. inputInfo = stdin.readLine().trim();
  88. int removeIndex = Integer.parseInt(inputInfo);
  89. //list1.removeElement(removeIndex);
  90. System.out.print("string at index " + removeIndex + " is removed\n");
  91. break;
  92.  
  93. case 'C': //Count the number of occurences of a specific element
  94. System.out.print("Please enter a string to count:\n");
  95. inputInfo = stdin.readLine().trim();
  96. //int counter1 =list1.countHowMany(inputInfo);
  97. //System.out.print("There are " + counter1 + " " + inputInfo + " found inside the linked list\n");
  98. break;
  99.  
  100. case 'D': //Remove all occurences of a given element
  101. System.out.print("Please enter a string to remove:\n");
  102. inputInfo = stdin.readLine().trim();
  103. list1.removeDuplicate(inputInfo);
  104. System.out.print(inputInfo + " is removed from the linked list\n");
  105. break;
  106.  
  107. case 'P': //Append a given element a number of times at the end of the linked list
  108. System.out.print("Please enter a string to append at the end:\n");
  109. String str3 = stdin.readLine().trim();
  110. System.out.print("Please enter the number of times you want to append:\n");
  111. inputInfo = stdin.readLine().trim();
  112. int times = Integer.parseInt(inputInfo);
  113. list1.appendAtEnd(str3, times);
  114. System.out.print(str3 + " is appended " + times + " times at end of the linked list\n");
  115. break;
  116.  
  117. case 'T': //Append a given element after the first occurence of another element
  118. System.out.print("Which string do you want to append after:\n");
  119. String str4 = stdin.readLine().trim();
  120. System.out.print("Please enter the string you want to append:\n");
  121. inputInfo = stdin.readLine().trim();
  122. list1.appendAfter(str4, inputInfo);
  123. System.out.print(inputInfo + " is appended after " + str4 + "\n");
  124. break;
  125.  
  126. case 'V': //Reverse the first few element of the linked list
  127. System.out.print("Please enter the number of elements you want to reverse:\n");
  128. inputInfo = stdin.readLine().trim();
  129. int reverseNum = Integer.parseInt(inputInfo);
  130. list1.reverseFirstFew(reverseNum);
  131. System.out.print("The first " + reverseNum + " elements are reversed\n");
  132. break;
  133.  
  134. case 'L': //List all strings inside the linked list
  135. System.out.print(list1.toString());
  136. break;
  137.  
  138. case 'Q': //Quit
  139. break;
  140.  
  141. case '?': //Display Menu
  142. printMenu();
  143. break;
  144. default:
  145. System.out.print("Unknown action\n");
  146. break;
  147. }
  148. }
  149. else
  150. {
  151. System.out.print("Unknown action\n");
  152. }
  153. } while (input1 != 'Q' || line.length() != 1);
  154. }
  155. catch (IOException exception)
  156. {
  157. System.out.print("IO Exception\n");
  158. }
  159. }
  160.  
  161. /** The method printMenu displays the menu to a user **/
  162. public static void printMenu()
  163. {
  164. System.out.print("Choice\t\tAction\n" +
  165. "------\t\t------\n" +
  166. "O\t\tList Current Size\n" +
  167. "I\t\tSearch Element\n" +
  168. "E\t\tGet Element by Index\n" +
  169. "S\t\tSet Element by Index\n" +
  170. "A\t\tInsert Element by Index\n" +
  171. "R\t\tRemove Element by Index\n" +
  172. "C\t\tCount How Many\n" +
  173. "D\t\tRemove Duplicates\n" +
  174. "P\t\tAppend at the End\n" +
  175. "T\t\tAppend After\n" +
  176. "V\t\tReverse First Few\n" +
  177. "L\t\tList Linked List\n" +
  178. "Q\t\tQuit\n" +
  179. "?\t\tDisplay Help\n\n");
  180.  
  181. } //end of printMenu()
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement