In: Computer Science
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 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.
for first exercise 2
import java.util.*;
public class Exercise2
{
public static void main(String[] args)
{
int arr[] ={3,60,35,2,45,320,5};
int r=removeItem(arr,7,2);
System.out.println("After removing
element, the array is");
for(int
i=0;i<arr.length-1;i++)
{
System.out.println(arr[i]);
}
}
public static int removeItem(int[] arr, int n, int
remove)
{
int flag=0;
for(int i=0;i<n;i++)
{
if
(arr[i]==remove)
{
for(int
j=i; j<(n-1); j++)
{
arr[j] =
arr[j+1];
}
flag=1;
break;
}
}
if (flag==0)
return -1;
else
return 0;
}
}
for exercise 2b
import java.util.*;
public class Exercise2b
{
public static void main(String[] args)
{
int arr[] ={3,60,35,2,45,320,5};
int r=removeItem(arr,7,2);
System.out.println("After removing
element, the array is");
for(int
i=0;i<arr.length-1;i++)
{
System.out.println(arr[i]);
}
}
public static int removeItem(int[] arr, int n, int
remove)
{
int flag=0;
int t = 0;
//sorting the array before
removing the element - bubblesort
for(int i=0; i < n; i++)
{
for(int j=1; j < (n-i); j++)
{
if(arr[j-1] > arr[j])
{
t = arr[j-1];
arr[j-1] = arr[j];
arr[j] = t;
}
}
}
for(int i=0;i<n;i++)
{
if
(arr[i]==remove)
{
for(int
j=i; j<(n-1); j++)
{
arr[j] =
arr[j+1];
}
flag=1;
break;
}
}
if (flag==0)
return -1;
else
return 0;
}
}
for execise 2c
import java.util.*;
public class Exercise2c
{
public static void main(String[] args)
{
int arr[] ={3,60,35,2,45,320,5};
int r=removeItem(arr,7,2);
System.out.println("After removing
element, the array is");
for(int
i=0;i<arr.length-1;i++)
{
System.out.println(arr[i]);
}
}
public static int removeItem(int[] arr, int n, int
remove)
{
int flag=0,k;
//sorting the array before
removing the element - Insertsort
for
(int j = 1; j < n; j++)
{
k = arr[j];
int i = j-1;
while ( (i > -1)
&& ( arr [i] > k ) )
{
arr[i+1] = arr
[i];
i--;
}
arr[i+1] = k;
}
for(int i=0;i<n;i++)
{
if
(arr[i]==remove)
{
for(int
j=i; j<(n-1); j++)
{
arr[j] =
arr[j+1];
}
flag=1;
break;
}
}
if (flag==0)
return -1;
else
return 0;
}
}
screenshot for output