Question

In: Computer Science

Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...

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.

Solutions

Expert Solution

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


Related Solutions

art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
art 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...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called 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.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps...
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps the elements at those two positions. The method should not traverse the list twice to find the elements, and it should not create or destroy any nodes.
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns the head reference to a linkedList. We will also need to display the linked list in out main method.
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array...
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array of single        words which are then are edited based on the command        Possible Commands:        remove: for the sent words, remove those from the text        replace: replace the word in an even element of words with a word in an odd element of words        add: add the word given word in the index indicated after...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT