Question

In: Computer Science

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 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

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


Related Solutions

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...
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