Question: 1) The main program below creates an ArrayList of String objects that are sorted using the Collections.sort method. This method takes two parameters: 1) the

1) The main program below creates an ArrayList of String objects that are sorted using the Collections.sort method. This method takes two parameters: 1) the ArrrayList, and 2) A Comparator object that is used to determine how objects are compared during the sort procedure. The Comparator class, ReverseWordSorter, is provided below, and sorts the Strings in reverse lexicographic order by negating the return value of a standard compareTo method. The Comparator class, LetterNumberSorter, compares Strings as if all the digits in the String occur at the beginning of the String followed by all the characters is also provided below. For example zzz123z is less than than aaa999a.

In this lab, we will examine strings composed of 3 alphabetic characters followed by 3 numeric characters and lastly, by a single alphabetic character.

Uncomment the comments in the main program and create the NumberLetterSorter Comparator. This comparator compares Strings as if all the characters in the String occur at the beginning of the String followed by all the digits. For example aaa123z is greater than aaa999a.

Test you code by running the main method.

2) At the end of main is a comment to search for the string bbbw. Add the appropriate code to perform a binary search for that string and print the location where it was found. You will need to implement a third comparator, AlphaFinder, for this purpose.

Hint: You need to use the substring and charAt methods in the String class.

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

public class SortRunner

{

public static void main(String[] args) {

ArrayList al = new ArrayList();

al.add("aaa888q");

al.add("aaa888q");

al.add("aaa555q");

al.add("aaa333q");

al.add("aaa888p");

al.add("aaa888a");

al.add("bbb111z");

al.add("bbb222w");

al.add("ccc777a");

al.add("eee888b");

al.add("zzz000c");

al.add("zzz666d");

al.add("ppp333e");

al.add("ppp111f");

ReverseWordSorter rws = new ReverseWordSorter();

Collections.sort(al,rws);

System.out.println("ReverseWord order:");

for(String s : al)

System.out.println(s);

LetterNumberSorter ls = new LetterNumberSorter();

Collections.sort(al,ls);

System.out.println("Letter-Number order:");

for(String s : al)

System.out.println(s);

// NumberLetterSorter nls = new NumberLetterSorter();

// Collections.sort(al,nls);

// System.out.println("Number-Letter order:");

// for(String s : al)

// System.out.println(s);

// Add code to search for the string "bbbw".

}

}

import java.util.Comparator;

public class ReverseWordSorter implements Comparator

{

public int compare(String a, String b)

{

return -1 * (a.compareTo(b)) ;

}

}

import java.util.Comparator; 
public class LetterNumberSorter implements Comparator 
{ 
 public int compare(String a, String b) 
 { 
 String tempa = a.substring(0,4) + a.charAt(6) + a.substring(3,6); 
 String tempb = b.substring(0,4) + b.charAt(6) + b.substring(3,6); 
 return tempa.compareTo(tempb); 
 } 
} 

That what it says on the description I supposed to make it to have the number in order and the letter in order with all the digits and characters if aaa123z is greater than aaa999a for the numberLetterSorter comparator and aaa123z is less than aaa999a for the letterNumberSorter and then to find the bbbw we need to create a third comparator to use the AlphaFinder to find the character bbbw to use

the binary search

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!