Question

Do • public static String sort(String inputString) o This method returns the sorted version of the...

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){

}

}

Homework Answers

Answer #1

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;

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine...
Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. (b) What is the big-O complexity of your method in terms of the list size n.
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...
* _Example commands for running this file_ * Compilation: javac Assignment1.java * Execution: java Assignment1 <...
* _Example commands for running this file_ * Compilation: javac Assignment1.java * Execution: java Assignment1 < input.txt * * Reads in a text file and for each line verifies whether the word has * unique characters. * * % cat input.txt * Hello * World * * % java Assignment1 < input.txt * False * True * ******************************************************************************/ import java.util.*; public class SortInput { /* a function for checking uniqueness of characters in a word */ private static boolean isUniqueChar(String...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static void readFromFile() throws IOException { BufferedReader bufReader = new BufferedReader(new FileReader("processes.txt")); ArrayList<String> listOfLines = new ArrayList<>(); String line = bufReader.readLine(); while (line != null) { listOfLines.add(line); line = bufReader.readLine(); } bufReader.close(); System.out.println("Content of ArrayLiList:"); // split by new line int num = 0; for (String line1 : listOfLines) { String line2[] = line1.split(","); // int burstTime = Integer.parseInt(line2[3].trim()); // String retrival = listOfLines.get(0); System.out.println("...
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort...
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort algorithm. The Merge step merges n elements which takes O(n) time. Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Any code that is found to exceed linear time will fail the tests. Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] import java.util.Scanner; import java.util.ArrayList; public class Solution...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT