Java will be the programming language Assume a class named T

Java will be the programming language

Assume a class named Tag that contains the tags associated with a digitized audio file. Two of the fields in the Tag class are track and album; both fields are Strings. The class also defines two accessory (getter) methods: getTrack() and getAlbum(). Write a single Comparator compare method that orders the audio files alphabetically by album and alphabetically by track within each album. For example: album a, track a album a, track b album a, track c album b, track a album b, track b album b, track c

Solution

Hi, Please find my code.

I have implemented Tag, TagComparator and TagTest class:

Please let me know in case of any issue.

########### Tag.java ################

public class Tag{

  

// instance variables

   private String track;

   private String album;

  

// constructor

   public Tag(String album, String track){

       this.album = album;

       this.track = track;

   }

  

// getter method

   public String getTrack(){

       return track;

   }

  

   public String getAlbum(){

       return album;

   }

  

}

########### TagComparator.java ###############

import java.util.Comparator;

public class TagComparator implements Comparator<Tag> {

  

// overriding compare method

       @Override

       public int compare(Tag track1, Tag track2) {

           // if bith track belongs to same album then sort based on track

           if(track1.getAlbum().equals(track2.getAlbum())){

               return track1.getTrack().compareTo(track2.getTrack());

           }else// if both belongs to different album, then sort based on album

               return track1.getAlbum().compareTo(track2.getAlbum());

       }

      

}

######## TagTest.java #############

import java.util.Arrays;

public class TagTest {

   public static void main(String[] args) {

      

       // creating Tag array

       Tag[] tags = {

               new Tag(\"a\", \"b\"),

               new Tag(\"b\", \"a\"),

               new Tag(\"a\", \"c\"),

               new Tag(\"b\", \"c\"),

               new Tag(\"c\", \"c\"),

               new Tag(\"a\", \"a\"),

               new Tag(\"b\", \"b\")

       };

      

       // sorting

       Arrays.sort(tags, new TagComparator());

      

       // printing sorted tags

       for(Tag g: tags){

           System.out.println(\"album \"+g.getAlbum()+\", track \"+g.getTrack());

       }

      

      

   }

}

/*

Sampe run:

album a, track a

album a, track b

album a, track c

album b, track a

album b, track b

album b, track c

album c, track c

*/

Java will be the programming language Assume a class named Tag that contains the tags associated with a digitized audio file. Two of the fields in the Tag class
Java will be the programming language Assume a class named Tag that contains the tags associated with a digitized audio file. Two of the fields in the Tag class
Java will be the programming language Assume a class named Tag that contains the tags associated with a digitized audio file. Two of the fields in the Tag class

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site