Coding activity 1
Coding activity 1
Instructions
Unlike in the exercise from the previous lesson, this method should print the values in the
array (on one line with a single space between values) multiple times: once after each
insertion in the algorithm has been completed (even if a value is inserted in its original
place). Make sure to use the String.compareTo() method when sorting to decide the correct
order of the Strings.
For example, if sortAndPrintReverse method is called with the array arr initialized as {"lock",
"key", "nail", "anvil", "hammer"} then the following should be printed:
Write your sortAndPrintReverse method in the U7_L6_Activity_One class. Use the runner
class to test your method but do not add a main method to your U7_L6_Activity_One.java
file or your code will not be scored correctly.
runner_U7_L6_Activity_One
import java.util.Scanner;
scan.nextLine();
String[] wordList = new String[len];
System.out.println("Enter values:");
wordList[i] = scan.nextLine();
U7_L6_Activity_One.sortAndPrintReverse(wordList);
U7_L6_Activity_One
int n = arr.length;
// Get the next value to be inserted into the partially sorted array
int j;
// If there is a value to the left, check to see if our value should be placed before it
arr[j + 1] = arr[j];
}
// When it no longer is less than the value to the left, insert the value
arr[j + 1] = key;
System.out.print(arr[k]);
if (k < arr.length - 1) {
System.out.print(" ");
System.out.println();