Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students also viewed these Databases questions

Question

to make a project accept / reject decision using a decision tree

Answered: 1 week ago

Question

Different types of Grading?

Answered: 1 week ago

Question

Explain the functions of financial management.

Answered: 1 week ago

Question

HOW MANY TOTAL WORLD WAR?

Answered: 1 week ago

Question

Discuss the scope of financial management.

Answered: 1 week ago