logo Practice-It logo

caughtSkipping

Language/Type: Java arrays
Author: Jessica Miller (on 2010/12/28)

In grade school, when a child is caught skipping another child in line, that child gets punished by having to go to the back of the line. Write a static method named caughtSkipping that accepts an array of Strings line, a String skipper, and a second String skipped. If skipper is found in the line array before (at a lower numbered index than) skipped, your method should perform the following actions:

  • The skipper is temporarily pulled out of the line.
  • All elements that were after skipper are shifted over one place (left-shifted to one index lower).
  • The skipper is moved to the end of the line.

Otherwise, if skipper is not found before skipped, no change to the line should be made.

For example, given the line array declared as follows:

String[] line = {"Iva", "Ben", "Brett", "Ally", "Kim", "Jesse"};

If a call were made caughtSkipping(line, "Jesse", "Kim") to see if Jesse skipped Kim, no change would be made to the order of the elements in line. However, if a second call were made caughtSkipping(line, "Ben", "Ally") to see if Ben skipped Ally, after the call the order of the elements in line would be:

{"Iva", "Brett", "Ally", "Kim", "Jesse", "Ben"}

Do not make any assumptions about the length of the array or the range of values it might contain. For example, the array might contain both, just one, or neither skipper and skipped. The line might contain more than one occurrence of the skipper student's name; if so, you should use the latest occurrence of skipper that comes before skipped, if any. You may assume that the line array, the String skipper, and the String skipped are not null.

You may not use any temporary arrays to help you solve this problem. (But you may declare as many simple variables as you like, such as ints.) You also may not use any other data structures or complex types such as Strings, or other data structures that were not taught in CSE 142 such as the ArrayList class from Chapter 10.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.