Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// inside the Music class, you DON\'T NEED THIS (but use as a model): public int

ID: 3843420 • Letter: #

Question

// inside the Music class, you DON'T NEED THIS (but use as a model):

public int compareTo(Music param)

{

     int result = composer.compareToIgnoreCase(param.composer);

     if( result == 0 )

          result = title.compareToIgnoreCase(param.title);

     return result;

}

public class LList<T extends Comparable<T>> implements ListInterface<T>

{

// DECLARE A Comparator<T> private instance variable HERE!

// ADD TO THE LList CONSTRUCTOR, A PARAMETER THAT'S A

//    Comparator<T>, and assign it to the instance var. HERE

   public LList(_______________________)

   {

      ­­­­­­­­________________________________

      initializeDataFields();

   } // end default constructor

// rest is the same

     public T getSmallest()

     {

          if( firstNode == null )

              return null;

          T smallestT = firstNode.getData();

          Node currNode = firstNode.getNextNode();

          while( currNode != null )

          {

     // CHANGE BELOW TO USE THE Comparator's compare method!

              if( currNode.getData().compareTo(smallestT) < 0 )

                   smallestT = currNode.getData();

              currNode = currNode.getNextNode();

          }

          return smallestT;

   }

}

// rest of the LList class is the same as before

ADD A NEW CLASS, call it MusicComparator that implements Comparator<Music> (similar to the Java Comparator Example) that compares 2 Music objects the same way you wrote compareTo for the Music class (but it's NOT an instance method in the Music class), but in your MusicComparator class!

If time, change Lab Exercise 1.3 so main will call getSmallest (display it).

// only part of main:

          LList<Music> musicList

           = new LList<Music>(______________);

          Music tempMusic;

      // rest of main is the same as in 1.3

         tempMusic = musicList.getSmallest();

         if( tempMusic != null )

            System.out.println("Smallest in the list is: "+tempMusic);

Explanation / Answer

note:

Implemented few of the method with the given time bound as per the question

public class LList<T extends Comparable<T>> implements ListInterface<T>

{

public T getSmallest()

{

if(firstNode==Null)

return null;

T smallestT = firstNode.getData();

Node currNode = firstNode.getNextNode();

while(currNode!=null)

{

if(currNode.getData().compareTo(smallestT) <0)

smallestT =currNode.getData();

currNode=currNode.getNextNode();

}

retutn smallestT;

}

}

class MusicComparator implemets Comparator<Music>

{

public int compare(Music l,Music r)

{

int value=l.getComposer().compareToIgnoreCase(r.getComposer());

if(value ==0)

value=l.getTitle().compareToIgnoreCase(r.getTitle());

return value;

}

}

LList<Music> musicList

           = new LList<Music>(new MusicComparator());

          Music tempMusic;

tempMusic =musicList.getSmallest();

if( tempMusic != null )

System.out.println("Smallest in the list is: "+tempMusic);