Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What I have so far from the previous project: ********************************************************************************* Song.java ********************************************************************************* public class Song implements Comparable { private String title; private String artist; private

image text in transcribed

What I have so far from the previous project: ********************************************************************************* Song.java *********************************************************************************

public class Song implements Comparable { private String title; private String artist; private String album;

public Song() { setTitle(""); setArtist(""); setAlbum(""); }

public Song(String title, String artist, String album) { setTitle(title); setArtist(artist); setAlbum(album); }

//Gets the song title.

public String getTitle() { return title; }

// Set and trim the song title.

public void setTitle(String ti) { try{ title = ti.trim(); } catch(NullPointerException nullArg) { title = ""; } }

// Gets the song artist.

public String getArtist() { return artist; }

//Set and trim the song artist.

public void setArtist(String ar) { try{ artist = ar.trim(); } catch(NullPointerException nullArg) { artist = ""; } }

//Gets the song album.

public String getAlbum() { return album; }

// Sets and trims the song album.

public void setAlbum(String al) { try{ album = al.trim(); } catch(NullPointerException nullArg) { album = ""; } }

// Checks if this song's title, artist, and album match that of the argument song.

@Override public boolean equals(Object arg) { if(arg == null) { return false; } else if(this == arg) { return true; } else if(!(arg instanceof Song)) { return false; } Song other = (Song) arg;

if(this.getTitle().equalsIgnoreCase(other.getTitle()) && this.getAlbum().equalsIgnoreCase(other.getAlbum()) && this.getArtist().equalsIgnoreCase(other.getArtist())) { return true; } return false; }

/// Converts the song to a string by combining the title, artist, and album.

@Override public String toString() { return "Title: " + getTitle() + " Artist: " + getArtist() + " Album: " + getAlbum() + " "; }

// Compares this song's album and title lexicographically to those of the argument.

@Override public int compareTo(Song arg) throws NullPointerException { if(arg == null) { throw new NullPointerException(); }

int albumCompResult = this.getAlbum().compareToIgnoreCase(arg.getAlbum());

if(albumCompResult != 0){ return albumCompResult; } else { // albums are equal int titleCompResult = this.getTitle().compareToIgnoreCase(arg.getTitle());

if (titleCompResult != 0) { return titleCompResult; } else { // titles are equal return this.getArtist().compareToIgnoreCase(arg.getArtist()); } } } }

********************************************************************************* SongReader.java *********************************************************************************

import java.util.Scanner; import java.io.File; import java.util.EmptyStackException; import java.util.NoSuchElementException; import java.io.FileNotFoundException; import java.lang.IllegalArgumentException;

public class SongReader {

private static LinkedStack tagStack = new LinkedStack(); private static Song currentSong = new Song();

public static void main(String[] args) { printHeading();

try{ Scanner fileReader = openFile();

String nextLine = fileReader.nextLine().trim(); String previousLine = "";

while(fileReader.hasNextLine()) { if (previousLine.startsWith("") && !(previousLine.startsWith("")) ) { try { addToTagStack(previousLine, nextLine); } catch(IllegalArgumentException nullArgument) { while(!previousLine.equalsIgnoreCase("")) { // Iterates until end of current song previousLine = fileReader.nextLine().trim(); // Moves the scanner down the file } // end of while } // end of catch catch(NullPointerException e) { } } // end of if

if(previousLine.startsWith("") && previousLine.endsWith(">")) { // Checks if previous line was a closing tag closeTag(previousLine); }

previousLine = nextLine; // Saves the value of the current line so it can be used in future iterations try{ nextLine = fileReader.nextLine().trim(); } catch(NoSuchElementException e){ } } // end of while loop } // end of try catch(FileNotFoundException noFile){ System.out.println("There was an error opening or reading from the file."); } }

//Adds the parameter to the stack of tags if it denotes a title, artist, or album, private static void addToTagStack(String previousLine, String nextLine) throws IllegalArgumentException { if(previousLine.equalsIgnoreCase("

")) { tagStack.push(previousLine); <p> if(nextLine.equals("") || nextLine.equalsIgnoreCase("</p>") || nextLine.equals(null)) { throw new IllegalArgumentException("Songs cannot have null fields."); } else { currentSong.setTitle(nextLine); } } if(previousLine.equalsIgnoreCase("")) { tagStack.push(previousLine);

if(nextLine.equals("") || nextLine.equalsIgnoreCase("

") || nextLine.equals(null)) { throw new IllegalArgumentException("Songs cannot have null fields."); } else { currentSong.setAlbum(nextLine); } } if(previousLine.equalsIgnoreCase("")) { tagStack.push(previousLine);

if(nextLine.equals("") || nextLine.equalsIgnoreCase("

") || nextLine.equals(null)) { throw new IllegalArgumentException("Songs cannot have null fields."); } else { currentSong.setArtist(nextLine); } } }

//Removes the previous tag from the top of the stack if it matches the song, title, artist, or album tag parameter.

private static void closeTag(String previousLine) { if(previousLine.equalsIgnoreCase("")) { endSong(); } try { if(previousLine.equalsIgnoreCase("")) { if (tagStack.peek().equalsIgnoreCase("

")) { tagStack.pop(); } else { throw new IllegalArgumentException("Tags improperly closed."); } } if(previousLine.equalsIgnoreCase("")) { if (tagStack.peek().equalsIgnoreCase("<album>")) { tagStack.pop(); } else { throw new IllegalArgumentException("Tags improperly closed."); } } if(previousLine.equalsIgnoreCase("")) { if (tagStack.peek().equalsIgnoreCase("<artist>")) { tagStack.pop(); } else { throw new IllegalArgumentException("Tags improperly closed."); } } } catch(IllegalArgumentException BadTags) { } // No action necessary } <p> //Prints the current song object and resets it for any future songs.</p> <p> private static void endSong() { try { System.out.println(currentSong.toString());</p> <p> if (!tagStack.pop().equalsIgnoreCase("<song>")) { throw new IllegalArgumentException("A tag was left unclosed."); } } // catch(IllegalArgumentException BadTag) { // tagStack.clear(); // } catch(EmptyStackException e) { }</song></p> <p> assert (tagStack.isEmpty());</p> <p> Song blankSong = new Song(); //Resets song data between songs currentSong = blankSong; }</p> <p> // Prompts the user for a filepath, and scans the console for a response.</p> <p> private static String promptForFilePath() { System.out.println("Enter the file path: "); Scanner input = new Scanner(System.in); String f = input.nextLine();</p> <p> input.close(); return f; }</p> <p> //Verify that the file exists, then creates a scanner to read it.</p> <p> private static Scanner openFile() throws FileNotFoundException { File file = new File(""); file = new File(promptForFilePath());</p> <p> return new Scanner(file); }</p> <p> // Prints a heading about the program to the console.</p> <p> private static void printHeading() { System.out.println("Song Reader"); }</p> <p>}</p> <p> ********************************************************************************* LinkedStack *********************************************************************************</p> <p>import java.util.EmptyStackException;</p> <p>public final class LinkedStack<t> implements StackInterface<t> { private Node topNode;</t></t></p> <p> public LinkedStack() { topNode = null; } // end default constructor</p> <p> public void push(T newEntry) { topNode = new Node(newEntry, topNode); // Node newNode = new Node(newEntry, topNode); // topNode = newNode; } // end push</p> <p> public T peek() { if (isEmpty()) throw new EmptyStackException(); else return topNode.getData(); } // end peek</p> <p> public T pop() { T top = peek(); // Might throw EmptyStackException assert (topNode != null); topNode = topNode.getNextNode();</p> <p> return top; } // end pop</p> <p> public boolean isEmpty() { return topNode == null; } // end isEmpty</p> <p> public void clear() { topNode = null; // Causes deallocation of nodes in the chain } // end clear</p> <p> private class Node { private T data; // Entry in stack private Node next; // Link to next node</p> <p> private Node(T dataPortion) { this(dataPortion, null); } // end constructor</p> <p> private Node(T dataPortion, Node linkPortion) { data = dataPortion; next = linkPortion; } // end constructor</p> <p> private T getData() { return data; } // end getData</p> <p>// private void setData(T newData) // { // data = newData; // } // end setData</p> <p> private Node getNextNode() { return next; } // end getNextNode</p> <p>// private void setNextNode(Node nextNode) // { // next = nextNode; // } // end setNextNode } // end Node } // end LinkedStack</p> <p> ****************************************************************** Stack Interface ******************************************************************</p> <p>public interface StackInterface<t> { //Adds a new entry to the top of this stack.</t></p> <p> public void push(T newEntry);</p> <p> //Removes and returns this stack's top entry.</p> <p> public T pop();</p> <p> // Retrieves this stack's top entry.</p> <p> public T peek();</p> <p> // Detects whether this stack is empty.</p> <p> public boolean isEmpty();</p> <p> // Removes all entries from this stack. public void clear(); } // end StackInterface</p> <p> </p> <p> </p> Programming Assignment 5 Note: When you turn in an assignment to be graded in this class you are making the claim that you neither gave nor received assistance on the work you turned in (except, of course, assistance from the instructor) Programming Project 5 builds on the work you completed in Programming Project 4. Begin by writing a subclass of your Song class and call it MySong. The MySong class includes an additional data field, playcount. If the playcount is not present in the input data file, then assign a value of 0 to the instance variable Refactor the songReader class to read Mysong data from the input file. Write the class MusicManager that contains the main method and instantiates a SongReader object and call the methods of the SongReader class to read in the input data file The MusicManager class will provide a variety of functions based on input to the program from command line arguments. The arguments have a very specific ordering. The first argument is the name of the file that contains all of the song data. The format of this file is the same as described for Project 4 with an additional tag for the playcount, <playcount>. When your MusicManager program reads this input file, it will be creating MySong objects The second command line argument is the number that refers to the desired functionality for the MusicManager program, 1, 2, or 3. Here is the specified behavior for each of the three options Display the top 10 songs with the highest playcount ordered from highest to lowest playcount. If the playlist contains less than 10 songs, display all the songs Reads the third command line argument as an artist name and displays a message if the playlist contains any songs by that artist. Note: a multi-word artist name can be passed as a single command line argument if it is enclosed in double quotes. For example, 1. 2. java MusicManager myPlaylist.data 2 "Iron & Wine" the command line argument array is interpreted as String1 args "myPlaylist.txt","2", "Iron & Wine"\; 3. Reads the third command line argument as an artist name and displays all songs by that artist. The songs are grouped by album and for each album the songs are listed in alphabetical order by title. It is expected that your program will be well documented, and you are required to write a private helper method called printHeading in the MusicManager class that outputs the following information to the console in an easy-to-read format: your name, the project number, the course identifier (CMSC 256), and the current semester You will call this method as the first statement in your main method. All files must contain a comment block at the beginning that includes the file name; all of the same information that was specified for the helper method printHeading; and a brief description of the file's purpose You will submit the project files (Song.java, SongReader.java, MySong.java, and MusicManager.java - along with any other Java files needed to run your program) to the assignment link in Blackboard Programming Assignment 5 Note: When you turn in an assignment to be graded in this class you are making the claim that you neither gave nor received assistance on the work you turned in (except, of course, assistance from the instructor) Programming Project 5 builds on the work you completed in Programming Project 4. Begin by writing a subclass of your Song class and call it MySong. The MySong class includes an additional data field, playcount. If the playcount is not present in the input data file, then assign a value of 0 to the instance variable Refactor the songReader class to read Mysong data from the input file. Write the class MusicManager that contains the main method and instantiates a SongReader object and call the methods of the SongReader class to read in the input data file The MusicManager class will provide a variety of functions based on input to the program from command line arguments. The arguments have a very specific ordering. The first argument is the name of the file that contains all of the song data. The format of this file is the same as described for Project 4 with an additional tag for the playcount, <playcount>. When your MusicManager program reads this input file, it will be creating MySong objects The second command line argument is the number that refers to the desired functionality for the MusicManager program, 1, 2, or 3. Here is the specified behavior for each of the three options Display the top 10 songs with the highest playcount ordered from highest to lowest playcount. If the playlist contains less than 10 songs, display all the songs Reads the third command line argument as an artist name and displays a message if the playlist contains any songs by that artist. Note: a multi-word artist name can be passed as a single command line argument if it is enclosed in double quotes. For example, 1. 2. java MusicManager myPlaylist.data 2 "Iron & Wine" the command line argument array is interpreted as String1 args "myPlaylist.txt","2", "Iron & Wine"\; 3. Reads the third command line argument as an artist name and displays all songs by that artist. The songs are grouped by album and for each album the songs are listed in alphabetical order by title. It is expected that your program will be well documented, and you are required to write a private helper method called printHeading in the MusicManager class that outputs the following information to the console in an easy-to-read format: your name, the project number, the course identifier (CMSC 256), and the current semester You will call this method as the first statement in your main method. All files must contain a comment block at the beginning that includes the file name; all of the same information that was specified for the helper method printHeading; and a brief description of the file's purpose You will submit the project files (Song.java, SongReader.java, MySong.java, and MusicManager.java - along with any other Java files needed to run your program) to the assignment link in Blackboard</playcount></playcount></artist></album>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What are ordering costs? Carrying costs? Give examples of each.

Answered: 1 week ago