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 an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
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.
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class HomeworkA { public static...
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.
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and returns a boolean. The method should return true if both arrays contain the same values (case sensitive), otherwise returns false. Note - the two arrays must be the same length and the values must be in the same order to return true. Part  2 Write a method named generateArray that accepts an int size as its parameter and returns an int[] array where each element...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT