Write a Java class called Song to represent a song in a music collection. The da
ID: 3586509 • Letter: W
Question
Write a Java class called Song to represent a song in a music collection. The data members of the Song class are String objects representing the song title, artist’s name, and the album where the song can be found. These instance variables for the class are to have private access modifiers, so you will need to write the appropriate methods to allow a client class to access and modify the data values. In accordance with good programming practice, you are to override the equals and toString methods inherited from Object. In addition, this class is going to be used in a future project collection that is to be sorted, so you’ll need to implement the Comparable interface.
A second class is to be written called SongReader that contains a main method that reads in a file name via the command line. The file is a listing of the songs that have been formatted using angle-bracketed tags. For example,
<song>
<title>
The Trapeze Swinger
</title>
<artist>
Iron and Wine
</artist>
<album>
The Trapeze Swinger - Single
</album>
</song>
Each line is either a tag (opening or closing) or data. Tags are matched pairs of single words surrounded in angle brackets. Opening tags begin with an angle bracket (<) and closing tags begin with an angle bracket and forward slash (</). The tags can appear in any order, but they must be properly nested inside song tags. Any incorrectly formatted song items should be ignored by the SongReader class. Data that is not specifically enclosed in title, artist, or album tags is to be ignored. In the sample input file, playlist.data, the first five song items are valid but the last one is invalid and should be rejected by the SongReader program.
The SongReader class will open and read the input file to create Song objects by parsing the file data. You are required to use an implementation of the given StackInterface<E> interface in your project for matching opening and closing tags. You may not use any external parsing libraries. After reading the input, the SongReader class will display a list of the Song objects.
Explanation / Answer
Please copy the below code in equilent classes
SongReader.class
package com.song;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
public class SongReader {
public static <E, T> void main(String[] args) throws IOException {
if (0 < args.length) {
String filename = args[0];
Stack<String> a1=new Stack();
File file = new File(filename);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
a1.add(line.trim());
stringBuffer.append(line.trim());
stringBuffer.append(" ");
}
fileReader.close();
System.out.println("Contents of file:");
//System.out.println(stringBuffer.toString());
Song songs=new Song();
int count=0;
ArrayList<Song> songList = new ArrayList<Song>();
for(int i=0;i<a1.size();i++){
String tab = a1.get(i);
StringBuilder str = new StringBuilder(tab);
//System.out.println(tab);
if(tab.contains("</")){
}else if(tab.contains("<")){
str.insert(1, "/");
//System.out.println(str);
if(a1.contains(str.toString())){
//System.out.println("Parsed::"+str);
if(tab.equals("<title>")){
String title = a1.get(i+1);
i=i+1;
songs.setTitle(title);
count+=1;
}
if(tab.equals("<artist>")){
String artist = a1.get(i+1);
songs.setArtist(artist);
count+=1;
i=i+1;
}
if(tab.equals("<album>")){
String album = a1.get(i+1);
songs.setAlbum(album);
count+=1;
i=i+1;
}
}
}
if(count==3){
System.out.println("hellloooo"+songs.toString());
songList.add(songs);
songs= new Song();
count=0;
}
///str.insert(1, "/");
}
//list sorting
Collections.sort(songList);//sort((List<T>) songList);
for(int i=0;i<songList.size();i++){
Song songs1 = (Song)songList.get(i);
System.out.println(songs1.toString());
}
}
}
}
Song.java
package com.song;
public class Song implements Comparable{
private String title;
private String artist;
private String album;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
@Override
public String toString() {
return "Song [title=" + title + ", artist=" + artist + ", album=" + album + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((album == null) ? 0 : album.hashCode());
result = prime * result + ((artist == null) ? 0 : artist.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Song other = (Song) obj;
if (album == null) {
if (other.album != null)
return false;
} else if (!album.equals(other.album))
return false;
if (artist == null) {
if (other.artist != null)
return false;
} else if (!artist.equals(other.artist))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}
}
Sample.data
<song>
<title>
The Trapeze Swinger
</title>
<artist>
Iron and Wine
</artist>
<album>
The Trapeze Swinger - Single
</album>
</song>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.