Question

In: Computer Science

THIS IS FOR JAVA Given an oversize array of size words and a word to remove,...

THIS IS FOR JAVA

Given an oversize array of size words and a word to remove, write a method that returns the array with each occurrence of the given word removed.

Shift the remaining words in the nonempty part of the array to the left so that each occurrence of the given word is overwritten. (Leave the words in the empty part of the array unchanged.)

Hint: To understand the test cases, note that the size (but not the capacity) of the array shrinks if removeWord appears in the array. In the first test case, for example, "up" is removed and "down" and "left" shift to the left by one element. The size of the array shrinks from 3 to 2, although the capacity is still 3. The third element is now in the empty part of the array. This element is equal to "left", but it is treated as empty space.

here is the code:

import java.util.Arrays;
import java.util.Scanner;
public class RemoveOversize
{
public static void main(String[] args)
{
// Do not edit this code
Scanner keyboard = new Scanner(System.in);
final int SIZE = 20; // for the oversized array
String[] array = new String[SIZE];
// Read in data from keyboard--all array elements are presumed to be on one line
String contents = keyboard.nextLine();
String[] contentsInArray = contents.split(" ");
// Make sure we're in the range of both arrays
int index;
for(index = 0; index <contentsInArray.length && index < array.length; ++index)
{
array[index] = contentsInArray[index];
}
int arraySize = index;
String value = keyboard.nextLine(); // Get the value to remove
int resultSize = removeOversize(array, arraySize, value);
System.out.println("The resulting array contains " + Arrays.toString(array) + " and is of size " + resultSize);
keyboard.close();
}
public static int removeOversize(String[] source, int sourceSize, String removeMe)
{
// Write the code below.
return null;
}
}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: I only completed removeOversize method as requested. Did not touch main method. The output will have null values for empty spaces. This is normal (not any mistake by me), because inside the main method, using Arrays.toString(), you are printing the entire array and not just the filled values. If you need to return an array of exact size, you will need to change the signature of removeOversize method. Right now it is returning the new size of the array, not entirely the new array.

// RemoveOversize.java

import java.util.Arrays;

import java.util.Scanner;

public class RemoveOversize {

      public static void main(String[] args) {

            // Do not edit this code

            Scanner keyboard = new Scanner(System.in);

            final int SIZE = 20; // for the oversized array

            String[] array = new String[SIZE];

            // Read in data from keyboard--all array elements are presumed to be on

            // one line

            String contents = keyboard.nextLine();

            String[] contentsInArray = contents.split(" ");

            // Make sure we're in the range of both arrays

            int index;

            for (index = 0; index < contentsInArray.length && index < array.length; ++index) {

                  array[index] = contentsInArray[index];

            }

            int arraySize = index;

            String value = keyboard.nextLine(); // Get the value to remove

            int resultSize = removeOversize(array, arraySize, value);

            System.out.println("The resulting array contains "

                        + Arrays.toString(array) + " and is of size " + resultSize);

            keyboard.close();

      }

      public static int removeOversize(String[] source, int sourceSize,

                  String removeMe) {

            // looping from last to front (easier to remove)

            for (int i = sourceSize - 1; i >= 0; i--) {

                  // checking if value at i needs to be removed

                  if (source[i].equals(removeMe)) {

                        // looping and shifting every elements on the right to one place

                        // to the left

                        for (int j = i; j < sourceSize - 1; j++) {

                              source[j] = source[j + 1];

                        }

                        // making the last element null

                        source[sourceSize - 1] = null;

                        // updating size

                        sourceSize--;

                  }

            }

            // returning new size

            return sourceSize;

      }

}

/*OUTPUT (user input in green)*/

hello abc hi how abc hello abc this is fun

abc

The resulting array contains [hello, hi, how, hello, this, is, fun, null, null, null, null, null, null, null, null, null, null, null, null, null] and is of size 7


Related Solutions

Write a Java Program that can:​ Remove a particular element from an array.​ Add a new...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new element to an array.​ Change an element with the new one.​ Search for a particular element in the array.​ ​The code must have four separate methods for each task stated above.​ Do not use any pre-defined Java functions.​ You are free to use int or String data-type for the array.​
Write a program to remove an element from an array at the given position k and...
Write a program to remove an element from an array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through keyboard. For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}. Hint: Sequence of moving the element is important....
//   Given an array of size 9, with the values of 1-9, determine if the array...
//   Given an array of size 9, with the values of 1-9, determine if the array //   is valid or not. //   Display a message stating the row is VALId, or state its INVALID and what //   was the index number that determined the data invalid. // //   Use this java code as a start of your code. //   Test the array data by changing the values. //============================================================================= import java.util.*;    public class Soduko_ValidateRow    { public static void main(String...
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...
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 Java program to create an array of a specific size (which is an input...
Write a Java program to create an array of a specific size (which is an input from the user) and fill it with random numbers between 1 and 100. Then sort the array and count how many of these numbers are originally at sorted position. Display that original array, the sorted array, and the count (number of elements originally at sorted position).
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice...
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It doesn't matter what you leave beyond the returned...
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT