Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. package JavaApplication6;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Arrays;
  7.  
  8.  
  9. public class JavaApplication6 {
  10. int [] arr = new int[10]; //initialize the container
  11. int item; //variable that will contain the inserted value
  12.  
  13. void insertItem(int item, int index){
  14. if(index != arr.length){
  15. arr[index] = item;
  16. } else{
  17. System.out.println("Container is full!");
  18. }
  19. }
  20.  
  21. void deleteItem(int index){
  22. if(index < 0){
  23. System.out.println("Item not found!");
  24. } else{
  25. arr[index] = 0;
  26. }
  27. }
  28.  
  29. int findLocationInsert(){
  30. for(int i = 0; i < arr.length; i++){
  31. if(arr[i] == 0){
  32. return i;
  33. }
  34. }
  35.  
  36. return arr.length;
  37. }
  38.  
  39. /* Using linear search, if the inserted item match with array, the index containing the item
  40. * is returned. Otherwise, -1 is returned if the array does not contain the item.
  41. */
  42. int findLocationDelete(){
  43. for(int i = 0; i < arr.length; i++){
  44. if(arr[i] == item){
  45. return i;
  46. }
  47. }
  48.  
  49. return -1;
  50. }
  51.  
  52. void traverse(int [] arr){
  53. Arrays.sort(arr); //Using QuickSort, sort the elements first before accessing the array
  54.  
  55. char punc; //punctuation mark
  56.  
  57. //loop for accessing array
  58. for(int i = 0; i < arr.length; i++){
  59.  
  60. //condition set for punc
  61. if(arr[i] != 0){
  62. if(i == arr.length - 1){
  63. punc = '.';
  64. } else{
  65. punc =',';
  66. }
  67.  
  68. //print all elements of the container using text formatter
  69. System.out.printf(" %d%c",arr[i], punc);
  70. }
  71. }
  72. }
  73.  
  74.  
  75. public static void main(String[] args) throws IOException{
  76. JavaApplication6 array = new JavaApplication6();
  77. BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
  78. int option;
  79.  
  80. //Simple UI
  81. do{
  82. System.out.print("\n1. Insert a value\n"
  83. +"2. Delete a value\n"
  84. +"3. Traverse\n"
  85. +"4. Exit\n"
  86. + "Selection: ");
  87. option = Integer.parseInt(buff.readLine());
  88.  
  89. switch(option){
  90. case 1:
  91. System.out.print("Insert value: ");
  92. array.item = Integer.parseInt(buff.readLine());
  93. array.insertItem(array.item, array.findLocationInsert());
  94. break;
  95.  
  96. case 2:
  97. System.out.print("Delete value: ");
  98. array.item = Integer.parseInt(buff.readLine());
  99. array.deleteItem(array.findLocationDelete());
  100. break;
  101.  
  102. case 3:
  103. System.out.print("Elements: ");
  104. array.traverse(array.arr);
  105. System.out.println();
  106. break;
  107. default:
  108. option = 4; //exit by default
  109. }
  110. } while(option != 4);
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement