Lab 9 Due Wednesday by 11:59pm Points 3 Submitting a file upload Available Mar 4 at 12am - Mar 17 at 12:59am 13 days Start Assignment CSSSKL 143 Lab: Sorting and Interfaces The lab consists of the...

1 answer below »
please read the attachment file for the assignment


Lab 9 Due Wednesday by 11:59pm Points 3 Submitting a file upload Available Mar 4 at 12am - Mar 17 at 12:59am 13 days Start Assignment CSSSKL 143 Lab: Sorting and Interfaces The lab consists of the following sections: Segment 1: Sorts Bubble Sort Selection Sort Big O of Bubble and Selection Insertion Sort Segment 2: Interfaces Student Class: implements comparable, cloneable, and serializable + answers to serializable questions Quiz Score + Quiz Tracker: implements cloneable A class that implements runnable Application: implements action listener MyWindow: implements mouse listener Lab Submission: *.java files with the lab solutions Files for this lab: Application.java (https://canvas.uw.edu/courses/1541396/files/85904286/download? download_frd=1) , InterfaceDriver.java (https://canvas.uw.edu/courses/1541396/files/85904292/download?download_frd=1) , Multi.java (https://canvas.uw.edu/courses/1541396/files/85904287/download?download_frd=1) , MyArrayList.java (https://canvas.uw.edu/courses/1541396/files/85904295/download?download_frd=1) , SortDiver.java (https://canvas.uw.edu/courses/1541396/files/85904300/download?download_frd=1) , Student.java (https://canvas.uw.edu/courses/1541396/files/85904289/download?download_frd=1) Segment 1: Sorting Summary The purpose of this lab is to practice implementing the sorts we’ve covered in class. We’ll start by building a simple Bubble Sort, which can be accomplished in 9 lines of code (LOC) or less, and then move on to more advanced sorting techniques like the Selection sort and Insertion sort. Also, we'll make https://canvas.uw.edu/courses/1541396/files/85904286?wrap=1 https://canvas.uw.edu/courses/1541396/files/85904286/download?download_frd=1 https://canvas.uw.edu/courses/1541396/files/85904292/download?wrap=1 https://canvas.uw.edu/courses/1541396/files/85904292/download?download_frd=1 https://canvas.uw.edu/courses/1541396/files/85904287/download?wrap=1 https://canvas.uw.edu/courses/1541396/files/85904287/download?download_frd=1 https://canvas.uw.edu/courses/1541396/files/85904295?wrap=1 https://canvas.uw.edu/courses/1541396/files/85904295/download?download_frd=1 https://canvas.uw.edu/courses/1541396/files/85904300/download?wrap=1 https://canvas.uw.edu/courses/1541396/files/85904300/download?download_frd=1 https://canvas.uw.edu/courses/1541396/files/85904289/download?wrap=1 https://canvas.uw.edu/courses/1541396/files/85904289/download?download_frd=1 minor improvements to the sorts to obtain better average-case execution times, but it is left as an exercise to the reader to demonstrate that these optimizations will have no effect on the (worst-case) Big O analysis. Beginning with the Bubble Sort The Bubble Sort is named due to how its sorting mechanism moves data items around during its sorting procedure. Heavy items “sink” to the bottom while smaller items “bubble” their way to the top by comparing neighbors and checking for inversions – that is, two elements out of order that require a swap. By comparing successive neighbors, we are guaranteed the max element in the last place after the first pass, so we are able to optimize on this search by only inspecting neighbors in the unsorted part of the array (the unsorted partition). This sort grows the sorted partition by one element (the largest) each pass and shrinks the unsorted partition accordingly. 1. Download the provided MyArrayList class, and inside of it declare a method called intBubbleSort() a. Note that MyArrayList has 3 arrays as its class fields; int[] , char[] , and String[] b. public void intBubbleSort() //needs no input, as the list is instance data i. See the pseudocode in the book or the slides for help on how to implement the bubble sort method ii. Implement a helper method called “ public void swapInts ( int[] intList, int j ) ” to call from your intBubbleSort() to swap the elements for you 2. Download the SortDiver.java (https://canvas.uw.edu/courses/1541396/files/85904300/download? download_frd=1) which contains the main method to test your code. a. The arrays get populated with random values in constructor. b. Print the list out using toString(); it should appear unsorted. c. In driver, uncomment the call to your intBubbleSort () method and output the list, this time in sorted order. Be sure to get the correct output. 3. Now, change your code so that it sorts an ArrayList of Comparable objects. To do this, you’ll need to declare the ArrayList class as “ public class MyArrayList implements Comparable ” and implement the compareTo() method to determine if one object is “less than” a second object. 4. In your compareTo() use the following logic to compare the objects: if(this.IntList[0] < other.intlist[0])="" return="" -1;="" else="" if(this.intlist[0]=""> other.IntList[0]) return 1; else return 0; Explain in your code what does each one of these return values mean? Why 1, or -1, or 0? 5. Uncomment the driver, and test your compareTo() . 6. Optimizations a. Can you reduce the inner loop relative to the outer loop? Try it b. Can you rewrite the outer loop so that it never executes additional times if the list is sorted “early”? https://canvas.uw.edu/courses/1541396/files/85904300/download?wrap=1 https://canvas.uw.edu/courses/1541396/files/85904300/download?download_frd=1 i. For example, the list {3 1 2} will need the outer loop to execute only one time, and not n(==3) times 7. Now, refer to the method headings in MyArrayList class and implement bubble sort for chars and Strings, as well. Do we need a separate swap() for Strings, since Strings are compared differently than primitive types? 8. Uncomment the driver, and test these newly created methods too The Selection Sort The selection sort makes progress by growing a sorted partition and shrinking the unsorted remainder. It does so by traversing the unsorted partition looking for the least element in that partition. The first pass simply looks for the least element and swaps that with the element at the head of the list (which is now considered sorted). The second pass would scan the remainder n-1 elements looking for the next minimum element, which then becomes the second element to the right of the head (now a 2-element sorted list), and so on until sorted. a. Use the following pseudo code to implement your selection sort inside MyArrayList class : Construct an outer for loop that traverses the array up to (n-1) element { 1. Construct an inner for loop that traverses the array up to the last element (n) 2. Does the inner loop iterator start at 0 or 1? Why? 3. Inside your loop check to see element at array[j] < array[i];="" 4.="" assign="" the="" index="" number="" for="" the="" smallest="" value="" to="" a="" variable.="" 5.="" keep="" updating="" the="" variable="" that="" holds="" the="" index="" for="" the="" smallest="" element="" as="" you="" continue="" traversing="" the="" array="" 6.="" when="" inner="" loop="" is="" completed,="" you="" will="" have="" the="" index="" for="" the="" smallest="" element="" in="" the="" array="" 7.="" now,="" all="" you="" have="" to="" do="" it="" use="" the="" swap()="" and="" swap="" array[smallest="" index]="" with="" array[i]="" 8.="" your="" loop="" will="" now="" go="" back="" to="" outer="" loop="" and="" will="" continue="" its="" work="" b.="" now,="" use="" the="" following="" pseudo="" code="" to="" implement="" the="" two="" functions="" that="" the="" selection="" sort="" will="" need="" to="" iterate:="" 1.="" void="" swap(arr[],="" int="" index1,="" int="" index2)="" 2.="" int="" findsmallest(arr[],="" int="" begin,="" int="" end)="" returns="" the="" index="" of="" the="" smallest="" element="" i.="" minindex="begin;" hint="" ii.="" for="" i="start" to="" end="" c.="" finally,="" let’s="" make="" an="" improvement="" to="" the="" selection="" sort.="" in="" findsmallest,="" since="" we’re="" walking="" the="" length="" of="" the="" unsorted="" partition,="" why="" not="" track="" both="" the="" smallest="" and="" the="" largest="" items="" we’ve="" seen?="" it’s="" a="" few="" more="" compares="" per="" iteration,="" but="" we="" know="" from="" our="" big="" o="" series="" that="" this="" won’t="" make="" the="" time="" complexity="" any="" worse.="" in="" our="" new="" version,="" we="" may="" need="" to="" change="" the="" outermost="" loop="" so="" that="" it="" calls="" findsmallest()="" and="" findlargest()="" .="" d.="" now="" compare="" the="" two="" sorts.="" the="" improvement="" we="" made="" in="" (3)="" should="" speed="" up="" the="" average-case="" execution="" of="" this="" algorithm,="" but="" does="" this="" improve="" the="" big="" o="" for="" this="" algorithm?="" build="" a="" big="" o="" estimate="" using="" the="" estimation="" techniques="" covered="" in="" class,="" tracking="" the="" number="" of="" compares="" the="" new="" algorithm="" uses="" versus="" the="" standard="" selection="" sort.="" insertionsort="" in="" this="" section,="" we="" will="" explore="" a="" new="" sorting="" implementation="" that="" grows="" a="" sorted="" partition="" by="" one="" at="" each="" step.="" as="" we="" expand="" our="" sorted="" partition="" to="" include="" our="" “nearest="" neighbor”,="" our="" partition="" becomes="" unsorted="" until="" we="" put="" the="" newest="" neighbor="" in="" the="" correct="" spot.="" so,="" our="" strategy="" will="" be="" outlined="" and="" developed="" below="" by="" our="" pseudocode="" below;="" if="" you="" think="" you’ve="" got="" a="" handle="" on="" the="" mechanism,="" try="" to="" implement="" the="" code="" after="" looking="" at="" just="" the="" first="" iteration="" of="" the="" pseudocode="" below.="" look="" at="" the="" second="" iteration="" (spoiler="" alert!)="" if="" stumped.="" insertionsort="" pseudocode,="" iteration="" 1="" starting="" with="" the="" first="" node,="" ending="" with="" the="" last="" get="" its="" neighbor="" (i+1)="" while="" the="" neighbor="" is="" out="" of="" place,="" move="" it="" backwards="" through="" the="" sorted="" partition="" once="" the="" neighbor="" is="" in="" the="" right="" place,="" we’re="" (locally)="" sorted="" and="" its="" time="" to="" repeat="" this="" process="" insertionsort="" implementation="" 1.="" in="" the="" same="" arraylist="" class,="" declare="" a="" method="" called="" insertionsort()="" ;="" 2.="" implement="" the="" above="" (or="" below)="" pseudocode="" logic="" in="" your="" code="" 3.="" in="" your="" main="" test="" driver,="" change="" the="" code="" so="" that="" it="" invokes="" the="" insertionsort.="" a.="" test="" your="" code="" insertionsort="" pseudocode,="" iteration="" 2="" for(int="" a="0;" a="">< length="" –="" 1;="" a++)="" {//note="" -1="" here="" since="" we’re="" dealing="" with="" neighbors="" (a,="" a+1)="" int="" current="data[a];" int="" hole="a;" while(="" hole="">0 && data[hole-1] < current="" )="" {="" while="" “out="" of="" place”="" slide="" data="" to="" the="" left="" moving="" the="" “hole”="" left="" }="" }="" segment="" 2:="" interfaces="" summary="" this="" lab="" is="" designed="" to="" introduce="" you="" to="" some="" frequently="" encountered="" interfaces="" in="" java,="" and="" then="" to="" get="" familiar="" with="" writing="" your="" own="" interfaces.="" when="" a="" class="" implements="" the="" comparable="" interface,="" we="" are="" able="" to="" sort="" objects="" of="" this="" set;="" if="" a="" class="" implements="" cloneable,="" then="" we="" can="" call="" clone()="" to="" make="" copies="" of="" such="" an="" object.="" we’ll="" start="" with="" an="" example="" using="" students="" and="" the="" {comparable,="" cloneable}="" interfaces,="" then="" move="" on="" to="" a="" brief="" introduction="" to="" multithreading="" (also="" using="" interfaces).="" implementing="" the="" comparable="" interface="" for="" sorting="" we="" start="" by="" building="" a="" student="" class="" that="" tracks="" their="" name="" and="" gpa,="" and="" then="" turn="" to="" implementing="" the="" comparable="" interface="" for="" this="" class.="" the="" idea="" is="" that="" we="" should="" be="" able="" to="" compare="" two="" students="" by="" their="" gpa="" and="" then="" sort="" a="" collection="" of="" students="" in="" order="" relative="" to="" their="" gpas.="" 1.="" build="" a="" student="" class="" with="" only="" two="" data="" items:="" a="" string="" name="" and="" a="" double="" gpa.="" 2.="" modify="" the="" class="" definition="" so="" that="" it="" “implements="" comparable”="" 3.="" when="" designing="" the="" compareto()="" method,="" use="" the="" gpa="" to="" determine="" if="" student1=""> student2 a. Consider returning the difference between the two students as the magnitude of difference (either positive or negative) 4. Build main to test your Students, and in this main build a list of 10 Students with different GPAs 5. Download and run the InterfaceDriver to test comparing students (comparableDemo in code) Implementing the Cloneable Interface There are two examples in this section for the Cloneable interface. We’ll do the first one together, which is making students cloneable. 1. Add “implements Cloneable” to your Student class definition 2. Define the clone method using “@Override” , make it public and return a new student object a. Build a copy constructor and then the clone is just one line of code i. “return new Student(this);”
Answered 3 days AfterMar 08, 2022

Answer To: Lab 9 Due Wednesday by 11:59pm Points 3 Submitting a file upload Available Mar 4 at 12am - Mar 17 at...

Shweta answered on Mar 12 2022
99 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here