In: Computer Science
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not sort the array.
Part B) Re-do Exercise #2 and name it Exercise2b. This time use a Bubble Sort to sort the array and then remove the item with the array sorted. Create a method for the BubbleSort.
Part C) Re-do Exercise #2 and name it Exercise 2c. This time use an insertion sort as given in this chapter to sort the array and then remove the item with the array sorted. Create a method for the insertion sort. Make sure to test your array with a small array of 5 integers and a larger array of at least 25 integers. When testing, test the removal of the first item in the array, somewhere in the middle and the last item in the array.
All the Java Programs are given below in their respective tables. You can test the functions implemented using different array sizes in the main() method.
Exercise 2 Java Code |
public class Exercise2 { // remove item method public static int[] removeItem(int[] array, int len, int valueToRemove) { int index = -1; if(array.length == 0 || len == 0) { System.out.println("Sorry! The size of array is 0."); return array; } for(int i = 0; i < len; i++) { if(array[i] == valueToRemove) { index = i; break; } } if(index == -1) { System.out.println("Value to remove is not present in the array."); return array; } int newSize = len - 1; int[] newArray = new int[newSize]; int newArray_index = 0; for(int i = 0; i < len; i++) { if(array[i] != valueToRemove) { newArray[newArray_index] = array[i]; newArray_index++; } } return newArray; } public static void main(String args[]) { int array[] = {10, 20, 9, 3, 12, 34, 50}; int value2remove = 34; System.out.println("Original array."); for(int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println("\nValue to remove: " + value2remove); int newArray[] = removeItem(array, 7, value2remove); System.out.println("\nAfter calling removeItem() method."); for(int i = 0; i < newArray.length; i++) { System.out.print(newArray[i] + " "); } } } |
Exercise2b Java Code |
public class Exercise2b { public static void main(String args[]) { int array[] = {10, 20, 9, 3, 12, 34, 50, 34, 10, 20, 34, 45, 70, 23, 10}; int len = array.length; int value2remove = 10; System.out.println("Original array."); for(int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println("\nSorting the array using bubble sort."); bubbleSort(array); System.out.println("Array after sorting is."); for(int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println("\nValue to remove: " + value2remove); int newArray[] = removeItem(array, len, value2remove); System.out.println("\nAfter calling removeItem() method."); for(int i = 0; i < newArray.length; i++) { System.out.print(newArray[i] + " "); } } public static int[] removeItem(int[] array, int len, int valueToRemove) { int index = -1; if(array.length == 0 || len == 0) { System.out.println("Sorry! The size of array is 0."); return array; } for(int i = 0; i < len; i++) { if(array[i] == valueToRemove) { index = i; break; } } if(index == -1) { System.out.println("Value to remove is not present in the array."); return array; } int newSize = len - 1; int[] newArray = new int[newSize]; int newArray_index = 0; boolean flag = true; for(int i = 0; i < len; i++) { if(array[i] == valueToRemove && flag == true) { flag = false; } else { newArray[newArray_index] = array[i]; newArray_index++; } } return newArray; } // bubble sort method public static void bubbleSort(int[] array) { for (int i = 0; i < array.length; i++) { for (int j = 1; j < array.length-i; j++) { if (array[j-1] > array[j]) { int temp = array[j-1]; array[j-1] = array[j]; array[j] = temp; } } } } } |
Exercise2c Java Code |
public class Exercise2c { public static void main(String args[]) { int array[] = {10, 20, 9, 3, 12, 34, 50, 34, 10, 20, 34, 45, 70, 23, 10}; int len = array.length; int value2remove = 70; System.out.println("Original array."); for(int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println("\nSorting the array using insertion sort."); insertionSort(array); System.out.println("Array after sorting is."); for(int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println("\nValue to remove: " + value2remove); int newArray[] = removeItem(array, len, value2remove); System.out.println("\nAfter calling removeItem() method."); for(int i = 0; i < newArray.length; i++) { System.out.print(newArray[i] + " "); } } public static int[] removeItem(int[] array, int len, int valueToRemove) { int index = -1; if(array.length == 0 || len == 0) { System.out.println("Sorry! The size of array is 0."); return array; } for(int i = 0; i < len; i++) { if(array[i] == valueToRemove) { index = i; break; } } if(index == -1) { System.out.println("Value to remove is not present in the array."); return array; } int newSize = len - 1; int[] newArray = new int[newSize]; int newArray_index = 0; boolean flag = true; for(int i = 0; i < len; i++) { if(array[i] == valueToRemove && flag == true) { flag = false; } else { newArray[newArray_index] = array[i]; newArray_index++; } } return newArray; } // insertion sort method public static void insertionSort(int[] array) { for (int top = 1; top < array.length; top++) { // copy the value into temp variable int temp = array[top]; int pos = top - 1; while (pos > -1 && array[pos] > temp) { // move items that are bigger than temp value up one position array[pos+1] = array[pos]; pos--; } // place temp into last unfilled position array[pos+1] = temp; } } } |
OUTPUT