Can Someone please help me with this assignment I neeeeeed it before 3:30 today
ID: 3686867 • Letter: C
Question
Can Someone please help me with this assignment I neeeeeed it before 3:30 today please I am very frustrated of this assignment I realllly neeed helllp please thank you
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)) ;
}
}
Explanation / Answer
SortRunner.java
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(Object s : al)
System.out.println(s.toString());
LetterNumberSorter ls = new LetterNumberSorter();
Collections.sort(al,ls);
System.out.println("Letter-Number order:");
for(Object s : al)
System.out.println(s.toString());
NumberLetterSorter nls = new NumberLetterSorter();
Collections.sort(al,nls);
System.out.println("Number-Letter order:");
for(Object s : al)
System.out.println(s.toString());
// Add code to search for the string "bbbw".
}
}
ReverseWordSorter.java
import java.util.Comparator;
public class ReverseWordSorter implements Comparator
{
public int compare(Object a, Object b)
{
return (a.toString().compareTo(b.toString())) ;
}
}
LetterNumberSorter.java
import java.util.Comparator;
public class LetterNumberSorter implements Comparator
{
public int compare(Object a, Object b)
{
String tempa = a.toString().substring(0,4) + a.toString().charAt(6) + a.toString().substring(3,6);
String tempb = b.toString().substring(0,4) + b.toString().charAt(6) + b.toString().substring(3,6);
return tempa.compareTo(tempb);
}
}
NumberLetterSorter.java
import java.util.Comparator;
public class NumberLetterSorter implements Comparator
{
public int compare(Object a, Object b)
{
String tempa = a.toString().substring(0,3) + a.toString().charAt(6) ;
String tempb = b.toString().substring(0,3) + b.toString().charAt(6) ;
return (tempa.compareTo(tempb)) ;
}
}
Output:
ReverseWord order:
aaa333q
aaa555q
aaa888a
aaa888p
aaa888q
aaa888q
bbb111z
bbb222w
ccc777a
eee888b
ppp111f
ppp333e
zzz000c
zzz666d
Letter-Number order:
aaa888a
aaa888p
aaa333q
aaa555q
aaa888q
aaa888q
bbb222w
bbb111z
ccc777a
eee888b
ppp333e
ppp111f
zzz000c
zzz666d
Number-Letter order:
aaa888a
aaa888p
aaa333q
aaa555q
aaa888q
aaa888q
bbb222w
bbb111z
ccc777a
eee888b
ppp333e
ppp111f
zzz000c
zzz666d
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.