Question: In Java. Write a method maxToEnd that takes an ArrayList of String as a parameter and that moves the largest string in the list to

In Java.

  1. Write a method maxToEnd that takes an ArrayList of String as a parameter and that moves the largest string in the list to the tail, otherwise preserving the relative order of the elements. Assume there is at least one element in the list. If there are multiple copies of the maximum value, move only the first copy.

Strings should be compared lexicographically with compareTo() of String class. If needed, review ch3.13 String comparisons.

// move the largest string (lexicographical order) to the tail,

// otherwise preserve the order of elements

// assume at least one element

public static void maxToEnd(ArrayList list) {

// ADD code

}

Example Input | Expected Output (list updated)

{"the", "best", "day", "ever"} | {"best", "day", "ever", "the"}

{"one", "Value"} | {"Value", "one"} {"three", "Two", "three", "four"}| {"Two", "three", "four", "three"}

The main() should unit test the method using assertion. Please follow the example code below and at least test all testing cases listed here.

String[] strArr; // will be used to convert a list of values to arraylist

ArrayList strList;

ArrayList expected;

strArr = new String[] {"the", "best", "day", "ever"};

strList = new ArrayList<>(Arrays.asList(strArr));

System.out.println(strList); // print original

maxToEnd(strList);

expected = new ArrayList<>(Arrays.asList(new String[] {"best", "day", "ever", "the"}));

if ( !strList.equals(expected) )

System.out.println("failed. Result: " + strList);

// reuse the variables to add additional testing cases

...

Arrays.asList(arr) returns an ArrayList object containing the values stored in the array parameter. You need: import java.util.Arrays;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!