Do
• public static String sort(String inputString)
o This method returns the sorted version of the input string.
The sorting must be accomplished using an insertion sort
o You are not allowed to use Arrays.sort, you need to
implement
the insertion sort yourself.
o We do not care about lower/upper case. Hint: Java's builtin
String method toLowerCase will come in handy when
sorting
o return null if the input string is null or empty.
Check empty or null string and if string is sorted correctly.
Don't do
1. You must not change the file names, class names, package
names.
2. You must not change the signature of any of these methods (name,
parameters,
…).
package lab06;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
public class AnagramUtil {
public static String[] readFile(String filename)
{
ArrayList<String> results = new
ArrayList<String>();
try
{
BufferedReader input = new BufferedReader(new
FileReader(filename));
while(input.ready())
{
results.add(input.readLine());
}
input.close();
}
catch(Exception e)
{e.printStackTrace();}
String[] retval = new String[1];
return results.toArray(retval);
}
/* ignore these methods
public static String[] getLargestAnagramGroup(String
filename){
Array
return null; // placeholder
}
public static String[] getLargestAnagramGroup(String[]
stringList){
return null; // placeholder
}
public static boolean areAnagrams(String inputString1, String
inputString2){
return false; // placeholder
}
*/
//this is the method for above
public static String sort(String inputString){
return null; // placeholder
}
public static void insertionSort(String[] inputList){
}
}
Since you have not provided the content of the file with which the function sort() will be invoked, I am providing the definitions o the methods as per my understanding.
CODE
//this is the method for above
public static String sort(String inputString){
if (inputString == null || inputString.length() == 0) {
return null; // placeholder
}
String[] list = inputString.split("");
String res = "";
for (int i=0; i<list.length; i++) {
res += list[i];
}
return res;
}
public static void insertionSort(String[] inputList){
int n = inputList.length;
for (int i = 1; i < n; ++i) {
String key = inputList[i];
int j = i - 1;
while (j >= 0 && inputList[j].toLowerCase().compareTo(key.toLowerCase()) > 0) {
inputList[j + 1] = inputList[j];
j = j - 1;
}
inputList[j + 1] = key;
}
}
Get Answers For Free
Most questions answered within 1 hours.