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 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.
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 {   ...
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++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT