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.
************CODING IN C++ ONLY ******************** Instructions Write a function, remove, that takes three parameters: an array...
************CODING IN C++ ONLY ******************** Instructions Write a function, remove, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, removeItem). The function should find and delete the first occurrence of removeItem in the array. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted. Also, write a program to test the function. Your program should prompt the...
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 java code that takes 2 parameters input and output and and guesses value of...
Write a java code that takes 2 parameters input and output and and guesses value of 3 variables x,y,z for following equation: output/input = (x)/(y*z) and range of x = 2 to 512 and y and z = 2 to 32 for example public int method(int input, int output) and input was 10 and output was 80 it'll return x=32 and y=2 and z=2.
Java Programming Preferably Concepts: Generics Arrays Objects Part I: Write a routine in Java that takes...
Java Programming Preferably Concepts: Generics Arrays Objects Part I: Write a routine in Java that takes an array, the length of the array, and an element and returns the position of the element in the array. For example, given the array with values [2, 4, 6, 8] and element 6, the routine should return 2 (since counting from 0, 6 occurs at position 2 in the array). Your routine should use generics to enable your routine to be reusable for...
in java language Write a method called findNums that takes a two-dimension array of integers and...
in java language Write a method called findNums 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 Question2 {   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT