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

Please, does anyone know how to do this in java? 1. Create a list of songs using

ID: 3902927 • Letter: P

Question

Please, does anyone know how to do this in java?

1. Create a list of songs using an array and after that you should be able to sort the songs based on the different fields.

2. Design a class Song based on the UML diagram (at bottom). Song class contains a list of fields to describe a song. The duration field is a float number giving the length of the song in minutes. The Song constructor will create a song with the fields set to default values (String to “”, float to 0.0, int to 0). The setSong method will assignment the parameter values to the corresponding fields if the parameter value is a valid value. The toString method will return a String representation of the song information (including all the fields) in a formatted way.

3. Design a class SongList based on the UML diagram (at bottom). SongList contains a song array which can hold a MAX (a static final field to specify the length of the songList array to be created) number of songs. The size field is used to keep track of how many songs have been added into the songList so far. For the sorting method you can either modify the sorting algorithm we have discussed or you can implement your own.

4. Create a Project2 class with a static test method to test your song list. First you add a bunch of songs (at least 6) to your list, and then print out all the songs in your list. After that you can call different sort methods and print out the songs after each sort method call. Please also test your findSong() method to make sure it does return the right index of the song you are looking for.

UML Diagram:

Song

- title: String

- writer: String

- singer: String

- duration: float

- genre: String

- year:int

+Song() +setSong(String, String, String, float, Sting, int)

+getTitle(): String

+getWriter(): String

+getSinger(): String

+getDuration(): float

+getGenre(): String

+getYear(): int

+toString():String

UML Diagram:

SongList

- MAX: int

- songList: Song [ ]

- size: int

+SongList():

+addSong(Song): boolean

+findSong(String title): int

+swapSong(int, int):

+sortByWriter():

+sortByDuration():

+sortByGenre():

+sortByYear();

+toString(): String

+getSize(): int

Explanation / Answer

Executable java Code:

Song.java

package project2;
public class Song {
private String title;
private String writer;
private String singer;
private double duration;
private String genre;
private int year;
  
// Constructor
public Song()
{
title="";
writer="";
singer="";
duration=0.0;
genre="";
year=0;
}
public void setSong(String tit, String writ, String sing, double dur, String gen, int ye)
{
title=tit;
writer=writ;
singer=sing;
duration=dur;
genre=gen;
year=ye;
}
// Get and set methods
public String getTitle()
{
return title;
}
public String getWriter()
{
return writer;
}

public String getSinger()
{
return singer;
}
  
public double getDuration()
{
return duration;
}
public String getGenre()
{
return genre;
}
public int getYear()
{
return year;
}
public String toString()
{
String str="";
str="Title: "+title+" ";
str=str+"writer: "+writer+" ";
str=str+"Singer: "+singer+" ";
str=str+"Duration: "+duration+" ";
str=str+"Genre: "+genre+" ";
str=str+"Year: "+year+" ";
return str;
}
}

SongList.java

package project2;
public class SongList {
static final int MAX=10;
private Song[] songList;
private int size;
public SongList()
{
songList=new Song[MAX];
size=0;
}
public boolean addSong(Song sng)
{
// If maximun size reeached
if(size==MAX)
return false;
else
{
songList[size]=sng;
size++;
return true;
}
}
public int findSong(String title)
{
for(int k=0;k<size;k++)
{
if(songList[k].getTitle().equals(title))
return k;
}
return -1;
}
public void swapSong(int ind1, int ind2)
{
Song s=songList[ind1];
songList[ind1]=songList[ind2];
songList[ind2]=s;
}
public void sortByWriter()
{
int i, j;
for (i = 0; i < size-1; i++)   
for (j = 0; j < size-i-1; j++)
{
String s=songList[j].getWriter();
String w=songList[j+1].getWriter();
if (s.compareTo(w)>0)
swapSong(j, j+1);
}
}

public void sortByDuration()
{
int i, j;
for (i = 0; i < size-1; i++)   
for (j = 0; j < size-i-1; j++)
{
double s=songList[j].getDuration();
double w=songList[j+1].getDuration();
if (s>w)
swapSong(j, j+1);
}
}

public void sortByGenre()
{
int i, j;
for (i = 0; i < size-1; i++)   
for (j = 0; j < size-i-1; j++)
{
String s=songList[j].getGenre();
String w=songList[j+1].getGenre();
if (s.compareTo(w)>0)
swapSong(j, j+1);
}
}

public void sortByYear()
{
int i, j;
for (i = 0; i < size-1; i++)   
for (j = 0; j < size-i-1; j++)
{
float s=songList[j].getYear();
float w=songList[j+1].getYear();
if (s>w)
swapSong(j, j+1);
}
}

public String toString()
{
String str="";
for(int k=0;k<size;k++)
{
str=str+songList[k].toString()+" ";
}
return str;
}

public int getSize()
{
return size;
}

}

Project2.java

package project2;
public class Project2 {
public static void main(String[] args) {
  
Song s1=new Song();
Song s2=new Song();
Song s3=new Song();
Song s4=new Song();
Song s5=new Song();
Song s6=new Song();
s1.setSong("Abb","ghr","fvg",3.5,"pop",1992);
s2.setSong("Bfj","bhd","mpr",3.7,"rock",1993);
s3.setSong("Taer","fgh","tiz",3.8,"pop",1986);
s4.setSong("Hbc","tkr","nal",3.3,"rock",1988);
s5.setSong("Mine","ees","big",3.2,"pop",1990);
s6.setSong("To allow","vgo","rat",3.1,"rock",1995);
SongList slist=new SongList();
slist.addSong(s1);
slist.addSong(s2);
slist.addSong(s3);
slist.addSong(s4);
slist.addSong(s5);
slist.addSong(s6);
slist.sortByDuration();
System.out.println(slist.toString());
System.out.println("------------------------------");
System.out.println(slist.toString());
System.out.println("------------------------------");
slist.sortByGenre();
System.out.println(slist.toString());
System.out.println("------------------------------");
slist.sortByWriter();
System.out.println(slist.toString());
System.out.println("------------------------------");
slist.sortByYear();
System.out.println(slist.toString());
System.out.println("------------------------------");
System.out.println("index pa Abb = "+slist.findSong("Abb"));
}
  
}

Output:

run:
Title: To allow
writer: vgo
Singer: rat
Duration: 3.1
Genre: rock
Year: 1995

Title: Mine
writer: ees
Singer: big
Duration: 3.2
Genre: pop
Year: 1990

Title: Hbc
writer: tkr
Singer: nal
Duration: 3.3
Genre: rock
Year: 1988

Title: Abb
writer: ghr
Singer: fvg
Duration: 3.5
Genre: pop
Year: 1992

Title: Bfj
writer: bhd
Singer: mpr
Duration: 3.7
Genre: rock
Year: 1993

Title: Taer
writer: fgh
Singer: tiz
Duration: 3.8
Genre: pop
Year: 1986


------------------------------
Title: To allow
writer: vgo
Singer: rat
Duration: 3.1
Genre: rock
Year: 1995

Title: Mine
writer: ees
Singer: big
Duration: 3.2
Genre: pop
Year: 1990

Title: Hbc
writer: tkr
Singer: nal
Duration: 3.3
Genre: rock
Year: 1988

Title: Abb
writer: ghr
Singer: fvg
Duration: 3.5
Genre: pop
Year: 1992

Title: Bfj
writer: bhd
Singer: mpr
Duration: 3.7
Genre: rock
Year: 1993

Title: Taer
writer: fgh
Singer: tiz
Duration: 3.8
Genre: pop
Year: 1986


------------------------------
Title: Mine
writer: ees
Singer: big
Duration: 3.2
Genre: pop
Year: 1990

Title: Abb
writer: ghr
Singer: fvg
Duration: 3.5
Genre: pop
Year: 1992

Title: Taer
writer: fgh
Singer: tiz
Duration: 3.8
Genre: pop
Year: 1986

Title: To allow
writer: vgo
Singer: rat
Duration: 3.1
Genre: rock
Year: 1995

Title: Hbc
writer: tkr
Singer: nal
Duration: 3.3
Genre: rock
Year: 1988

Title: Bfj
writer: bhd
Singer: mpr
Duration: 3.7
Genre: rock
Year: 1993


------------------------------
Title: Bfj
writer: bhd
Singer: mpr
Duration: 3.7
Genre: rock
Year: 1993

Title: Mine
writer: ees
Singer: big
Duration: 3.2
Genre: pop
Year: 1990

Title: Taer
writer: fgh
Singer: tiz
Duration: 3.8
Genre: pop
Year: 1986

Title: Abb
writer: ghr
Singer: fvg
Duration: 3.5
Genre: pop
Year: 1992

Title: Hbc
writer: tkr
Singer: nal
Duration: 3.3
Genre: rock
Year: 1988

Title: To allow
writer: vgo
Singer: rat
Duration: 3.1
Genre: rock
Year: 1995


------------------------------
Title: Taer
writer: fgh
Singer: tiz
Duration: 3.8
Genre: pop
Year: 1986

Title: Hbc
writer: tkr
Singer: nal
Duration: 3.3
Genre: rock
Year: 1988

Title: Mine
writer: ees
Singer: big
Duration: 3.2
Genre: pop
Year: 1990

Title: Abb
writer: ghr
Singer: fvg
Duration: 3.5
Genre: pop
Year: 1992

Title: Bfj
writer: bhd
Singer: mpr
Duration: 3.7
Genre: rock
Year: 1993

Title: To allow
writer: vgo
Singer: rat
Duration: 3.1
Genre: rock
Year: 1995


------------------------------
index pa Abb = 3
BUILD SUCCESSFUL (total time: 0 seconds)