For practice with Eclipse, let's make a Code Template so we can type "sopl" to automatically insert System.out.println();
In Eclipse, go to Window → Preferences → Java → Editor → Templates
Scroll down the list to "sysout" and copy the code template syntax for "sysout". It looks like this:
System.out.println(${word_selection}${});${cursor}
Next, click the New button to make a new template. Enter this information:
Name: sopl (This is the auto-insert command you can use later)
Context: Java statements (This is the section or scope of code your command will work in)
Automatically Insert: Check the box if not already checked
Description: This is my custom command for println!
Pattern: System.out.println(${word_selection}${});
${cursor}
Click OK and go back to your code. Try typing "sopl" then hit Ctrl+Space. Type something and then hit ENTER to exit the code template. Notice that your cursor goes to the next line because that's what your code template says to do.
Create a SelectionSort class in Eclipse. Put it in the same project as your BubbleSort.
Add a public static void function named selectionSort1 that takes in an array of ints and uses Selection Sort to sort the list into ascending order by swapping largest elements to the right.
Test your method by creating several test arrays in the main method and then passing them into your bubble sort function. In order to verify that you are "bubbling" the right elements in the right direction, modify your method to print the array after each completion of the outer loop. This is how the autograder will know that your code has the correct intermediate steps and it's also helpful for you to see what's happening. Remember that you can print an array using the Arrays helper class which has to toString() method.
Add a println statement at the end of each outer loop, like this:
for (int outer ...) {
for (int inner ...) {
...
}
swap(...)
// Print at end of each outer loop
System.out.println(Arrays.toString(myArray));
}
Here is an example of output with the printing after each iteration of the outer loop:
Starting array: [9, 5, 1, 3, 7, 4] (printed in main method)
[4, 5, 1, 3, 7, 9] (printed in selection sort)
[4, 5, 1, 3, 7, 9] (printed in selection sort)
[4, 3, 1, 5, 7, 9] (printed in selection sort)
[1, 3, 4, 5, 7, 9] (printed in selection sort)
[1, 3, 4, 5, 7, 9] (printed in selection sort)
Add a public static void function named selectionSort2 that takes an array of Strings and sorts the list into descending order by swapping largest elements to the left. Whether your inner loop scans the unsorted portion of the list from left to right or right to left is up to you.
Remember that to compare Strings, you must use the compareTo() function to determine which String is "smaller."
Test your method thoroughly and include a println statement at the end of each outer loop.
Let's add your final Spider Solitaire project to Eclipse.
In Eclipse, choose (File → New → Java Project) from the menu.
Uncheck the box for "Use default location" and then click the Browse button. Navigate to the folder where you have your Final Spider Solitaire project and select that folder as the project folder.
Click the Finish button. Now you can edit your Spider Solitaire code in BlueJ and Eclipse since both projects point to the same code.
Add a sort() method to the Deck class. In the sort() method, implement a selection sort algorithm that puts the deck into ascending order by swapping largest Cards to the right. Since a Deck is just an ArrayList of Card objects, this comes down to applying selection sort to an Arraylist of Cards. You don't need a parameter for your method because you are sorting your Deck's ArrayList attribute.
As a reminder: In order to sort a list of Card objects, the Card class itself must implement the Comparable interface, which means two things:
class Card implements Comparable<Card>
compareTo(Card other)
method to compare Cards the way it wants
5. After writing the sort function in Deck, go to your DeckTester class and verify that your sorting algorithm works by making a Deck of Cards, shuffling it, printing it, sorting it, then printing it again.
For the purpose of grading, copy your Deck's sorting method to your SelectionSort class and modify the method signature as shown below so that it accepts an ArrayList of Cards instead of relying on your Deck's attribute.
public static void selectionSort(ArrayList<Card> arr) {
// your code here
}
Test your method by making an ArrayList of Cards in the SelectionSort main method and then sorting the list using your new selection sort method.
Note that you will need to copy the Card class into your selection sort project. Do this by dragging Card.java from your computer onto your Selection Sort project. When Eclipse gives you a choice of Copy Files vs Link To Files, choose Copy. This will make a copy of your Card class and put the copy in your SelectionSort project. Otherwise, Link To Files would leave Card.java in the original folder and only make a shortcut to it in your SelectionSort project.
Finally, please comment out any code you have inside your main() method because the autograder uses its own main method and it's possible that you call custom Card or Deck methods the autograder will not know about. Do NOT submit your Card class or declare your Card class in the file you submit as that will also create compiler errors. Your sorting method will be tested with a Card class provided by the autograder.
This lab will be graded by a cruel, unforgiving, merciless autograder, so be sure to follow the directions exactly, including naming your methods precisely as directed and using the correct access modifiers and other key words as required.
On top of the autograder, it will also be further inspected by a human who will complain if your algorithm is executing unnecessary operations (i.e. your loop is performing more iterations than strictly required) or if you did not follow the directions exactly (i.e. you got the items in the right order, but didn't use the correct pairs for comparison). Make sure your code meets all requirements and performs exactly the operations required to guarantee the job is done - no more and no less. Think carefully and be concise. Don't just guess or pad your algorithm with extra steps "just in case".
PX_LastName_FirstName_SelectionSort
code belowYou must Sign In to submit to this assignment
Last modified: January 09, 2023
Back to Lab 17.1 Bubblesort