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.
- 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
// 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
ArrayList
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started