Question

Introduction In this final programming exercise, you'll get a chance to put together many of the...

Introduction

In this final programming exercise, you'll get a chance to put together many of the techniques used during this semester while incorporating OOP techniques to develop a simple song playlist class. This playlist class allows a user to add, remove and display songs in a playlist.

The Base Class, Derived Class and Test Client

Unlike the other PAs you completed in zyLabs, for this PA you will be creating THREE Java files in IntelliJ to submit. You will need to "upload" these into zyLabsfor grading. There will be two class files and a single client file, details of which are described below.

  • SongBase.java - Base class definition for a song.
  • PlaylistDerived.java - Extends the base class definition to add functionality for a playlist of several songs (aka SongEntry objects).
  • PlaylistClient.java - Client program that runs and tests the base/derived class hierarchy.

Building the SongBase Class:

The members of the SongBase class are listed in a diagram below known as a Unified Modeling Language (UML) diagram. You must use the same field names and method names as listed in the diagram. This will insure that the included tests for the program will work for your implemented class.

How to read the UML diagram below This is a simplified version of a class diagram known as a "UML diagram" or "Unified Modeling Language diagram". These types of diagrams are commonly used during the software engineering design process when programmers are first building classes and client files. They are programming language independent. The following are some descriptions of the parts of a UML diagram:
  • The + (plus sign) indicates the field or method is public
  • The - (minus sign) indicates the field or method is private. Use private in place of public when declaring a field or method.
  • The parameter list for a method is listed in parentheses using their proposed type. For this assignment, you should use the names given for any input parameters as specified in the decomposition.
  • The return type is listed after the method (and after the semicolon).

Class Name

SongBase

Instance Variables (fields)

   -  title: String
   -  artist: String
   -  genre : int

Constructors

   + SongBase()
   + SongBase(String title, String artist, int genre)

Instance Methods

   + setTitle(String title) : void
   + setArtist(String artist) : void
   + setGenre(int genre) : void
   + getTitle() : String
   + getArtist() : String
   + getGenre() : int
   + getGenreName() : String
   + toString() : String
   + equals(Object other) : boolean

Decomposition Information for SongBase Class

You may add additional helper methods for a class if you wish, but the methods from the UML, described in more detail below must be included since they will be unit tested.

  1. Constructors - There are two constructors in the base class. The default should initialize the instance variables to either the empty string "" for title and artist. It should initialize the genre to the value -1. The overloaded constructor should use the input parameters for initial values of the instance variables.
  2. Accessor and Mutators - You should provide accessor and mutator methods for each of the instance variables in the base class.
  3. public String getGenreName() - There are (3) genre options for any song -- Holiday, Romance and Party. Internally, the genre will be represented as an integer value (0 = Holiday, 1 = Dance, 2 = Romance). For a better user experience, the base class provides this method which retrieves the designated genre and converts it to its genre string name before returning it. If the genre is -1 this means no genre has been assigned in which case the empty string should be returned.
  4. public String toString() - This method overrides the default toString() method for printing an object. You should customize it to return a string with the appropriate information for a SongBase object. Specifically, it should return a string suitable for printing in the following form
    Title: title
    Artist: artist
    Genre: genre name

    For example, if it were to be called from the client for the song, "White Christmas" by Bing Crosby, it would return a string like this:
     
  5. boolean equals(Object other) - This method overrides the standard equals() method for comparing two song objects. Two song objects are considered equal if they have the same title (ignore case), artist (ignore case) and genre. Otherwise the song objects are not considered equal. Returns a boolean indicating whether or not two song objects are equal.

TIP: Build the Base Class first - You should build the base class first and devise a client that can test the base class much like we did in lecture. Once you feel pretty good about your base class, you can begin developing the PlaylistDerived Class next. You do NOT want to develop the entire application at once unless you have a strong desire to spend several days debugging your program.

Extending the Base Class with the PlaylistDerived Class:

The members of the PlaylistDerived class are listed below in the UML diagram. You must use the same field names and method names as listed in the diagram. This will insure that the included tests for the program will work for your implemented class.

Class Name

PlaylistDerived

Constants and Instance Variables (fields)

  -  MAXSIZE : int constant (max size of playlist is 8)
  -  playlist (an array of song objects) : SongBase[MAXSIZE] 
  -  count (count of songs in the playlist): int

Constructors

   + PlaylistDerived()

Instance Methods

   + getCount() : int
   + addSong(String title, String artist, int genre) : int
   + removeSong(String title, String artist, int genre) : int
   + moveSongsUp(int position) : void
   + findSong(SongBase songtoFind) : int
   + customPlayList(String artist)  : String
   + customPlayList(int genre) : String
   + toString() : String

Decomposition Information for PlayListDerived Class

You may add additional helper methods for a class if you wish, but the methods from the UML, described in more detail below must be included since they will be unit tested.

  1. Constructors There is one default constructor. The default should initialize the instance variable count to the value of 0.
  2. public int getCount() - This accessor method returns the current count of songs in the playlist.
  3. public int addSong(String title, String artist, int genre) - This method adds a new song to the playlist at the next available location in the playlist array and updates the instance variable count by 1. For example, if there are two songs in the playlist, then the new song should be added at location 2 and the count should be incremented to 3 (remember that the first song is in location 0 of the array). This method should return a 0 if the song is successfully added and return a -1 if this song is a duplicate that already exists in the playlist. You should use the helper method findSong() when looking for a duplicate song. Data Validation: If the playlist array is full, throw an ArrayIndexOutOfBoundsException that states "Cannot add: " + title of song + " Maximum playlist size is " + MAXSIZE.
  4. public int removeSong(String title, String artist, int genre) - This method removes a specific song from the playlist and decrements the count of songs in the playlist by 1. Keep in mind that to "remove" a song from the array, we don't actually delete it, we simply move up all the subsequent songs in the playlist by one position and basically overwrite the song that was to be deleted. This method should return a 0 if the song is successfully deleted and returns a -1 if this song is not in the playlist and therefore cannot be deleted. You should use the helper method findSong() when looking for whether or not the song exists to delete and the helper method moveSongsUp() to adjust the playlist after removing a song from it.
  5. public void moveSongsUp(int position) - This helper method helps the removeSongs() method by moving up all the songs by one position in the playlist based on the position of the song that is to be "deleted".
  6. public String customPlayList(String artist) - This method searches the playlist for all songs containing this artist's name (ignore case) in the artist field. Like the customPlayList(int genre), this overloaded method returns a String representing that playlist but is based on the artist's name instead of genre type. Data Validation: If no songs are found by the requested artist, the method should return the string "No songs by this artist found". For more information, see sample run.
  7. public String customPlayList(int genre) - This overloaded method searches the playlist for all songs of a given genre and returns a String representing that playlist. Data Validation: If no songs are found in the requested genre, the method should return the string "No songs in this genre found". For more information, see sample run.
  8. public String toString() - This method overrides the default toString() method for printing an object. You should customize it to return a string with the appropriate information for a complete playlist by repeatedly calling the base class toString() method. Specifically, it should return a string suitable for printing in the following form (see sample run for more examples):
     

TIP: Build this class after the base class - You should build the PlayList class after building the base class and devise a client that can test the PlayList class much like we did in lecture. Once you feel good about your PlayList class, you can start developing the client application.

Building the Client Test Program:

The client program you develop is simply going to be a test driver. In other words, it should thoroughly test your base and derived class. Since you've not had prior experience doing this, we are providing the final test client for you. You may add lines, comment out lines or modify this as you wish to assist you with any testing you perform during your development process and prior to submitting to zyBooks. Of course, zyBooks will also test almost every method to ensure that it follows the specs appropriately.

Sample Run (using the provided test client):

 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SongBase.java

public class SongBase {
private String title, artist;
private int genre;
  
public SongBase()
{
this.title = "";
this.artist = "";
this.genre = -1;
}

public SongBase(String title, String artist, int genre)
{
this.title = title;
this.artist = artist;
  
// if genre is not anyone of the following: 0, 1, 2, by default, it sets the genre to 0
if(genre != 0 && genre != 1 && genre != 2)
this.genre = 0;
else
this.genre = genre;
}

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 int getGenre() {
return genre;
}

public void setGenre(int genre) {
this.genre = genre;
}
  
public String getGenreName()
{
String res;
if(this.genre == -1)
res = "";
else
{
switch(this.genre)
{
case 0:
res = "Holiday";
break;
  
case 1:
res = "Dance";
break;
  
case 2:
res = "Romance";
break;
  
default:
res = "";
}
}
return res;
}
  
@Override
public String toString()
{
return("Title: " + this.title + "\n"
+ "Artist: " + this.artist + "\n"
+ "Genre: " + getGenreName());
}
  
@Override
public boolean equals(Object other)
{
if(other instanceof SongBase)
return (this.title.equalsIgnoreCase(((SongBase) other).getTitle())
&& this.artist.equalsIgnoreCase(((SongBase) other).getArtist())
&& getGenreName().equalsIgnoreCase(((SongBase) other).getGenreName()));
else
return false;
}
}

PlaylistDerived.java

public class PlaylistDerived extends SongBase{
private static final int MAXSIZE = 8;
private SongBase playlist[] = new SongBase[MAXSIZE];
private int count;
  
public PlaylistDerived()
{
this.count = 0;
}
  
public int getCount(){ return this.count; }
  
public int addSong(String title, String artist, int genre)
{
if(count == MAXSIZE)
throw new ArrayIndexOutOfBoundsException("Cannot add: " + title
+ " Maximum playlist size is " + MAXSIZE + ".");
  
SongBase song = new SongBase(title, artist, genre);
if(this.count == 0)
{
this.playlist[count] = song;
this.count += 1;
return 0;
}
else
{
if(findSong(song) == -1) // not duplicate
{
this.playlist[count] = song;
this.count += 1;
return 0;
}
else
return -1;
}
}
  
public int removeSong(String title, String artist, int genre)
{
SongBase song = new SongBase(title, artist, genre);
if(findSong(song) != -1)
{
int position = findSong(song);
moveSongsUp(position);
this.count -= 1;
return 0;
}
else
return -1;
}
  
public String customPlaylist(String artist)
{
String res = "";
  
for(int i = 0; i < count; i++)
{
if(this.playlist[i].getArtist().equalsIgnoreCase(artist))
res += this.playlist[i].toString() + "\n--------------------\n";
}
  
if(res.equals(""))
return "No songs by this artist found.";
else
return res;
}
  
public String customPlaylist(int genre)
{
String res = "";
  
for(int i = 0; i < count; i++)
{
if(this.playlist[i].getGenre() == genre)
res += this.playlist[i].toString() + "\n--------------------\n";
}
  
if(res.equals(""))
return "No songs in this genre found.";
else
return res;
}
  
public int findSong(SongBase songToFind)
{
int index = -1;
  
if(this.count == 0)
index = -1;
else
{
for(int i = 0; i < this.count; i++)
{
if(this.playlist[i].equals(songToFind))
{
index = i;
break;
}
}
}
return index;
}
  
public void moveSongsUp(int position)
{
for(int i = position; i < count - 1; i++)
{
this.playlist[i] = this.playlist[i + 1];
}
}
  
@Override
public String toString()
{
String res = "";
  
if(count == 0)
res = "Playlist is empty\n";
else
{
res = "My Playlist (count of songs = " + getCount() + ")\n\n";
for(int i = 0; i < count; i++)
{
if(i == count - 1)
res += (i + 1) + ". " + this.playlist[i].toString() + "\n";
else
res += (i + 1) + ". " + this.playlist[i].toString() + "\n\n";
}
}
return res;
}
}

SongsTester.java (Client?Driver class)

public class SongsTester {
  
public static void main(String[] args)
{
System.out.println("***** Testing creating new playlist *****");
  
PlaylistDerived playlist = new PlaylistDerived();
System.out.println(playlist);
  
System.out.println("\n***** Testing adding 6 songs *****");
playlist.addSong("White Christmas", "Bing Crosby", 0);
playlist.addSong("White Christmas", "Bedha Skolar", 0);
playlist.addSong("Jingle Java Christmas", "NaDHA Skolar", 0);
playlist.addSong("All I Want For Christmas is For Nadha To Pass", "The Skolar Family", 0);
playlist.addSong("Be My Java Bae", "NAdha Skolar", 1);
playlist.addSong("Sad Sitch", "Stellar Skolar", 0);
  
System.out.println(playlist);
  
System.out.println("\n***** Testing adding duplicate songs *****");
if(playlist.addSong("Be My Java Bae", "nadha SKOLAR", 0) == -1)
System.out.println("ERROR: Duplicate song Be My Java Bae (genre 0) not added");
else
System.out.println("SUCCESS: Song Be My Java Bae (genre 0) successfully added");
  
if(playlist.addSong("Be My Java Bae", "nadha SKOLAR", 0) == -1)
System.out.println("ERROR: Duplicate song Be My Java Bae (genre 0) not added");
else
System.out.println("SUCCESS: Song Be My Java Bae (genre 0) successfully added");
  
if(playlist.addSong("Jingle Java Christmas", "NaDHA Skolar", 0) == -1)
System.out.println("ERROR: Duplicate song Jingle Java Christamas (genre 0) not added");
else
System.out.println("SUCCESS: Song Jingle Java Christamas (genre 0) successfully added");
  
System.out.println("\n" + playlist);
  
  
  
System.out.println("\n***** Testing overflowing the playlist *****");
playlist.addSong("Jingly Java Christmas", "NADHA SKOLAR", 1);
playlist.addSong("AddMe4", "BEDHA SKOLLAR", 0);
  
System.out.println(playlist);
}
}

******************************************************************* SCREENSHOT ****************************************************

Add a comment
Know the answer?
Add Answer to:
Introduction In this final programming exercise, you'll get a chance to put together many of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objectives: GUI Tasks: In Lab 4, you have completed a typical function of music player --...

    Objectives: GUI Tasks: In Lab 4, you have completed a typical function of music player -- sorting song lists. In this assignment, you are asked to design a graphic user interface (GUI) for this function. To start, create a Java project named CS235A4_YourName. Then, copy the class Singer and class Song from finished Lab 4 and paste into the created project (src folder). Define another class TestSongGUI to implement a GUI application of sorting songs. Your application must provide the...

  • (1) Create two files to submit: Song.java - Class definition MainClass.java - Contains main() method (2)...

    (1) Create two files to submit: Song.java - Class definition MainClass.java - Contains main() method (2) Build the Song class in Song.java with the following specifications: Private fields String songTitle String songArtist int secDuration Initialize String fields to "Not defined" and numeric to 0. Create overloaded constructor to allow passing of 1 argument (title), 2 (title, artist) and 3 arguments (title, artist, duration). Create public member method printSongInfo () with output formatted as below: Title: Born in the USA Artist:...

  • java With the incredible royalties on your Jukebox-management code, to become a DJ. You realize that you can adap...

    java With the incredible royalties on your Jukebox-management code, to become a DJ. You realize that you can adapt the Jukebox code to create some sophistica software for your new career. Consider the slightly simplified versio As a DJ, you are mainly interested in the BPM (beats per minute) and rating of the songs. you decide to indulge your dream n of the book's Song class below. public class Song private String title private String artist; private int rating private...

  • JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the...

    JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers Johnson Title: The Dude Length: 337 Artist: Quincy Jones...

  • Create two classes. Song. Should include the name of the song and the artist Playlist. Should...

    Create two classes. Song. Should include the name of the song and the artist Playlist. Should include a list of song objects from the above class, a title for the list and a description of the list. Specific method requirements below. Functionality You should be able to print both a song and a playlist. Formatting should follow the below examples EXACTLY. I should be able to print a so11ng or a playlist by calling the standard python print function. Format...

  • You will be building a linked list. Make sure to keep track of both the head and tail nodes.

    Ch 8 Program: Playlist (Java)You will be building a linked list. Make sure to keep track of both the head and tail nodes.(1) Create two files to submit.SongEntry.java - Class declarationPlaylist.java - Contains main() methodBuild the SongEntry class per the following specifications. Note: Some methods can initially be method stubs (empty methods), to be completed in later steps.Private fieldsString uniqueID - Initialized to "none" in default constructorstring songName - Initialized to "none" in default constructorstring artistName - Initialized to "none"...

  • Task #3 Arrays of Objects 1. Copy the files Song java (see Code Listing 7.1), Compact...

    Task #3 Arrays of Objects 1. Copy the files Song java (see Code Listing 7.1), Compact Disc.java (see Code Listing 7.2) and Classics.txt (see Code Listing 7.3) from the Student Files or as directed by your instructor. Song.java is complete and will not be edited. Classics.txt is the data file that will be used by Compact Disc.java, the file you will be editing. 2. In Compact Disc.java, there are comments indicating where the missing code is to be placed. Declare...

  • I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...

    I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...

  • public class Song { private String title; private String artist; private int duration; public Song() {...

    public class Song { private String title; private String artist; private int duration; public Song() { this("", "", 0, 0); } public Song(String t, String a, int m, int s) { title = t; artist = a; duration = m * 60 + s; } public String getTitle() { return title; } public String getArtist() { return artist; } public int getDuration() { return duration; } public int getMinutes() { return duration / 60; } public int getSeconds() { return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT