Question

In: Computer Science

C++ language: Class Song The class will have the following private attributes: Title (string) Artist (string)...

C++ language:

Class Song

The class will have the following private attributes:

  • Title (string)
  • Artist (string)
  • Album (string)
  • PlayTime (integer)
  • Year (integer)

It will also have the following public member functions:

  • default constructor setting the playtime to 0 and year to 0.
  • a constructor using Title, Artist, Album, Year, and PlayTime as arguments
  • accessors and mutators for all private attributes
  • a void function 'Play' that will print out the information about the song in the following format (provided)
Playing *Title* by  *Artist*  *playtime* seconds
  • a comparison operator == that uses title, artist and album (but not year and playtime) for comparing two songs.

Implement the missing functions using the template provided.

Class MusicLibrary

MusicLibrary has the following private attributes

  • int maxSongs
  • int numSongs
  • Song * mySongs
  • int numSongsPlayList
    • Song** playList;

Note that mySongs is a pointer that will be used to initialize a dynamic array of songs. You need to handle this dynamic array correctly (e.g. shallow copies, memory deallocation etc.)

playList is a dynamic array of pointers to Songs in the mySongs array.

The class also includes the following public member functions:

  • bool function addSong taking title, artist, album, playtime, year (provided)
  • readSongsFromFile (provided)
    • some accessors (provided)

This class is partially implemented. Complete the following:

  • implement a constructor taking the number of songs as an argument
  • implement a copy constructor
  • implement the public function 'playRandom' which plays all songs of the library ( by invoking the play function of each song) in pseudo-random fashion by alternating songs from the beginning and from the end of the list, until all songs have been played. For example, if the library has 7 songs, the songs will be played in the following order: 0, 6, 1, 5, 2, 4, 3
    • implement the public function addSongToPlayList which stores a pointer to a song in the library in the array of Song pointer. The input to this method is an integer denoting the position of the song in the MusicLibrary

If the Playlist is full, print the following error message

Could not add Song to PlayList. PlayList is full

If the integer denoting the position of a song in the MusicLibrary is invalid, print the following error message

Invalid song
  • implement the public function 'playPlaylist' which plays the songs in the playlist in the same order as the songs have been added to the playlist.

A sample main file has been provided showing the utilization of the functions, as well as a sample input file.

IMPORTANT

  • Classes and methods names must match exactly for unit testing to succeed.
  • Submissions with hard coded answers will receive a grade of 0.

main.cpp:

#include <iostream>

#include <string>

#include "Song.h"

#include "MusicLibrary.h"

using namespace std;

int main()

{

string filename;

int numsongs;

cout << "Enter number of Songs " << endl;

cin >> numsongs;

cout << "Enter filename with information about the songs" << endl;

cin >> filename;

MusicLibrary mylibrary(numsongs);

mylibrary.readSongsFromFile(filename);

mylibrary.playRandom();

for (int i = numsongs-1; i >= 0; i--) {

mylibrary.addSongToPlayList(i);

}

mylibrary.playPlaylist();

return 0;

}

musicLibrary.cpp:

#include <iostream>

#include <fstream>

#include <sstream>

#include <string>

#include "MusicLibrary.h"

MusicLibrary::MusicLibrary(int maxsongs)

{

   // implement constructor

}

MusicLibrary::MusicLibrary(MusicLibrary& other)

{

   // implement copy constructor

}

MusicLibrary::~MusicLibrary()

{

delete[] mySongs;

delete[] playList;

}

int MusicLibrary::getnumSongs()

{

   return numSongs;

}

int MusicLibrary::getmaxSongs()

{

   return maxSongs;

}

int MusicLibrary::getnumSongsPlayList()

{

   return numSongsPlayList;

}

bool MusicLibrary::addSong(string title, string artist, string album, int year, int time)

{

if (numSongs == maxSongs) {

cout << "Could not add song to library. Library is full" << endl;

return false;

}

mySongs[numSongs].setTitle(title);

mySongs[numSongs].setArtist(artist);

mySongs[numSongs].setAlbum(album);

mySongs[numSongs].setYear(year);

mySongs[numSongs].setPlayTime(time);

numSongs++;

return true;

}

bool MusicLibrary::addSong(Song& song)

{

if (numSongs == maxSongs) {

cout << "Could not add Ssong to library. Library is full" << endl;

return false;

}

mySongs[numSongs] = song;

numSongs++;

return true;

}

void MusicLibrary::readSongsFromFile(string filename)

{

ifstream input;

input.open(filename);

bool cont = true;

if (input.is_open()) {

string line;

while ( getline(input, line) && cont ) {

string title, artist, album;

string s_year, s_time;

int year;

int time;

istringstream inSS(line);

getline(inSS, title, ',');

getline(inSS, artist, ',');

getline(inSS, album, ',');

getline(inSS, s_year, ',');

getline(inSS, s_time);

year = stoi(s_year);

time = stoi(s_time);

cont = addSong(title, artist, album, year, time);

};

}

else {

cout << "could not open file " << filename << endl;

}

}

void MusicLibrary::playRandom()

{

   // implement this method

}

bool MusicLibrary::addSongToPlayList(int pos)

{

// implement this method

}

void MusicLibrary::playPlaylist()

{

   // implement this method

}

musicLibrary.h:

#pragma once

#include <string>

#include "Song.h"

using namespace std;

class MusicLibrary

{

private:

int maxSongs;

int numSongs; // number of Songs in library

Song* mySongs; // dynamic array storing all Songs

int numSongsPlayList; // number of Songs in Playlist

Song** playList; // dynamic array of pointers to Songs

public:

MusicLibrary(int maxsongs);

MusicLibrary(MusicLibrary& other);

~MusicLibrary();

   int getnumSongs();

   int getmaxSongs();

   int getnumSongsPlayList();

bool addSong(string title, string artist, string album, int year, int time);

bool addSong(Song& song);

void readSongsFromFile(string filename);

bool addSongToPlayList(int pos);

void playRandom();

void playPlaylist();

};

song.cpp:

#include <iostream>

#include "Song.h"

void Song::Play()

{

cout << "Playing "<< Title << " by " << Artist << " " << PlayTime << " seconds" << endl;

}

// Add code for constructors, accessors, mutators, and == operator.

song.h:

#pragma once

#include <string>

using namespace std;

class Song {

private:

string Title;

string Artist;

string Album;

int Year;

int PlayTime;

public:

   // Add declaration for constructors, accessors and mutators

   // Add declaration for overloading the == operator

void Play();

};

songs.txt:

Zoo Station, U2, Achtung Baby, 1991, 203
Youngblood, 5 Seconds of Summer, Youngblood, 2018, 311
Money for Nothing, Dire Straits, Brothers in Arms, 1986, 501
Summer of 69, Bryan Adams, Reckless, 1984, 178
Livin on a Prayer, Bon Jovi, Slippery when Wet, 1986, 241

Solutions

Expert Solution

Program code to copy

main.cpp

#include <iostream>
#include <string>
#include "Song.h"
#include "MusicLibrary.h"
using namespace std;

int main()
{

    string filename;
    int numsongs;

    cout << "Enter number of Songs " << endl;
    cin >> numsongs;

    cout << "Enter filename with information about the songs" << endl;
    cin >> filename;

    MusicLibrary mylibrary(numsongs);
    mylibrary.readSongsFromFile(filename);
    mylibrary.playRandom();

    for (int i = numsongs-1; i >= 0; i--)
    {
        mylibrary.addSongToPlayList(i);
    }
    mylibrary.playPlaylist();
    return 0;

}

===================================================================================

MusicLibrary.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include "MusicLibrary.h"

//Implement a constructor taking the number of songs as an argument.
MusicLibrary::MusicLibrary(int maxsongs)
{
   maxSongs = maxsongs;
   numSongs = 0;
   numSongsPlayList = 0;

   mySongs = new Song[maxSongs];
   playList = new Song*[maxSongs];
}

//Implement a copy constructor.
MusicLibrary::MusicLibrary(MusicLibrary& other)
{
   this->maxSongs = other.maxSongs;
   this->numSongs = other.numSongs;
   this->numSongsPlayList = other.numSongsPlayList;
   mySongs = new Song[maxSongs];
   playList = new Song*[maxSongs];
}

MusicLibrary::~MusicLibrary()
{
   delete[] mySongs;
   delete[] playList;
}

int MusicLibrary::getnumSongs()
{
   return numSongs;
}

int MusicLibrary::getmaxSongs()
{
   return maxSongs;
}

int MusicLibrary::getnumSongsPlayList()
{
   return numSongsPlayList;
}

bool MusicLibrary::addSong(string title, string artist, string album, int year, int time)
{
   if (numSongs == maxSongs)
   {
       cout << "Could not add song to library. Library is full" << endl;
       return false;
   }
   mySongs[numSongs].setTitle(title);
   mySongs[numSongs].setArtist(artist);
   mySongs[numSongs].setAlbum(album);
   mySongs[numSongs].setYear(year);
   mySongs[numSongs].setPlayTime(time);
   numSongs++;

   return true;
}
bool MusicLibrary::addSong(Song& song)
{
   if (numSongs == maxSongs)
   {
       cout << "Could not add Ssong to library. Library is full" << endl;
       return false;
   }
   mySongs[numSongs] = song;
   numSongs++;

   return true;
}

void MusicLibrary::readSongsFromFile(string filename)
{
   ifstream input;
   input.open(filename.c_str());
   bool cont = true;

   if (input.is_open())
   {
       string line;
       while (getline(input, line) && cont)
       {
           string title, artist, album;
           string s_year, s_time;
           int year;
           int time;
           istringstream inSS(line);

           getline(inSS, title, ',');
           getline(inSS, artist, ',');
           getline(inSS, album, ',');
           getline(inSS, s_year, ',');
           getline(inSS, s_time);

           year = atoi(s_year.c_str());
           time = atoi(s_time.c_str());
           cont = addSong(title, artist, album, year, time);
       }
   }  
   else
   {
       cout << "could not open file " << filename << endl;
   }
}

/*
*Implement the public function 'playRandom' which plays all songs of the library
*/
void MusicLibrary::playRandom()
{
   for (int i = 0; i < numSongs / 2; i++)
   {
       mySongs[i].Play();
       mySongs[numSongs - 1 - i].Play();
   }

   if ((numSongs % 2) != 0)
   {
       mySongs[numSongs / 2].Play();
   }
}

/*
*Implement the public function addSongToPlayList which stores a pointer to a song in the library in the array of Song pointer.
*/
bool MusicLibrary::addSongToPlayList(int pos)
{
   if (numSongsPlayList == maxSongs)
   {
       cout << "Could not add Song to PlayList. PlayList is full" << endl;
       return false;
   }

   if ((pos < 0) || (pos >= numSongs))
   {
       cout << "Invalid song" << endl;
       return false;
   }

   playList[numSongsPlayList] = &mySongs[pos];
   numSongsPlayList++;

   return true;
}

//Implement the public function 'playPlaylist' which plays the songs in the playlist in the same order as the songs have been added to the playlist.
void MusicLibrary::playPlaylist()
{
   for (int i = 0; i < numSongsPlayList; i++)
   {
       playList[i]->Play();
   }
}

===================================================================================

MusicLibrary.h

#pragma once

#include <string>
#include "Song.h"

using namespace std;

class MusicLibrary
{
private:
   int maxSongs;
   int numSongs;          //number of Songs in library
   Song* mySongs;         //dynamic array storing all Songs

   int numSongsPlayList; //number of Songs in Playlist
   Song** playList;       //dynamic array of pointers to Songs
  
public:
   MusicLibrary(int maxsongs);
   MusicLibrary(MusicLibrary& other);
   ~MusicLibrary();

    int getnumSongs();
    int getmaxSongs();
    int getnumSongsPlayList();

   bool addSong(string title, string artist, string album, int year, int time);
   bool addSong(Song& song);
   void readSongsFromFile(string filename);
  
   bool addSongToPlayList(int pos);
   void playRandom();
   void playPlaylist();
  
};

===================================================================================Song.cpp

#include <iostream>
#include "Song.h"

//Default constructor setting the playtime to 0 and year to 0.
Song::Song()
{
   Title = "";
   Artist = "";
   Album = "";
   Year = 0;
   PlayTime = 0;
}

//A constructor using Title, Artist, Album, Year, and PlayTime as arguments.
Song::Song(string Title, string Artist, string Album, int Year, int PlayTime)
{
   this->Title = Title;
   this->Artist = Artist;
   this->Album = Album;
   this->Year = Year;
   this->PlayTime = PlayTime;
}

//Accessors and mutators for all private attributes.
void Song::setTitle(string Title)
{
   this->Title = Title;
}

void Song::setArtist(string Artist)
{
   this->Artist = Artist;
}

void Song::setAlbum(string Album)
{
   this->Album = Album;
}

void Song::setYear(int Year)
{
   this->Year = Year;
}

void Song::setPlayTime(int PlayTime)
{
   this->PlayTime = PlayTime;
}

string Song::getTitle()
{
   return Title;
}

string Song::getArtist()
{
   return Artist;
}

string Song::getAlbum()
{
   return Album;
}

int Song::getYear()
{
   return Year;
}

int Song::getPlayTime()
{
   return PlayTime;
}

//A void function 'Play' that will print out the information about the song in the following format (provided)
void Song::Play()
{
   cout << "Playing "<< Title << " by " << Artist << " " << PlayTime << " seconds" << endl;
}

//A comparison operator == that uses title, artist and album (but not year and playtime) for comparing two songs.
bool Song::operator==(const Song& other)
{
   if (Title.compare(other.Title) == 0)
   {
       if (Artist.compare(other.Artist) == 0)
       {
           if (Album.compare(other.Album) == 0)
           {
               return true;
           }
           else
           {
               return false;
           }
       }
       else
       {
           return false;
       }
   }
   else
   {
       return false;
   }
}

===================================================================================

Song.h

#pragma once

#include <string>
using namespace std;

class Song {
private:
   string Title;
   string Artist;
   string Album;
   int Year;
   int PlayTime;

public:
   Song();
   Song(string Title, string Artist, string Album, int Year, int PlayTime);
   void setTitle(string Title);
   void setArtist(string Artist);
   void setAlbum(string Album);
   void setYear(int Year);
   void setPlayTime(int PlayTime);
   string getTitle();
   string getArtist();
   string getAlbum();
   int getYear();
   int getPlayTime();
   void Play();
   bool operator==(const Song& other);
};

===================================================================================songs.txt

Zoo Station, U2, Achtung Baby, 1991, 203
Youngblood, 5 Seconds of Summer, Youngblood, 2018, 311
Money for Nothing, Dire Straits, Brothers in Arms, 1986, 501
Summer of 69, Bryan Adams, Reckless, 1984, 178
Livin on a Prayer, Bon Jovi, Slippery when Wet, 1986, 241

===================================================================================Program Screenshot

Main.cpp

================================================================================

MusicLibrary.cpp

===================================================================================

MusicLibrary.h

===================================================================================

Songs.cpp

================================================================================

Song.h

==================================================================================

songs.txt

===================================================================================Sample output


Related Solutions

Create a class, called Song. Song will have the following fields:  artist (a string) ...
Create a class, called Song. Song will have the following fields:  artist (a string)  title (a string)  duration (an integer, recorded in seconds)  collectionName (a string) All fields, except for collectionName, will be unique for each song. The collectionName will have the same value for all songs. In addition to these four fields, you will also create a set of get/set methods and a constructor. The get/set methods must be present for artist, title, and duration....
Add the following private attributes: String publisher String title String ISBN String imageName double price Create...
Add the following private attributes: String publisher String title String ISBN String imageName double price Create getter/setter methods for all data types Create a constructor that takes in all attributes and sets them Remove the default constructor Override the toString() method and return a String that represents the book object that is formatted nicely and contains all information (attributes) In the main class: Create a main method that throws FileNotFoundException Import java.io libraries Import java.util.Scanner Create a loop to read...
Assume you have created the following data definition class: public class Book {    private String title;...
Assume you have created the following data definition class: public class Book {    private String title;    private double cost;       public String getTitle() { return this.title; }    public double getCost() { return this.cost; }       public void setTitle(String title) {       this.title = title;    } // Mutator to return true/false instead of using exception handling public boolean setCost(double cost) {       if (cost >= 0 && cost < 100) {          this.cost = cost;          return true;       }       else {          return false;       }...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
Programming Language: C# Person Class Fields - password : string Properties + «C# property, setter private»...
Programming Language: C# Person Class Fields - password : string Properties + «C# property, setter private» IsAuthenticated : bool + «C# property, setter absent» SIN : string + «C# property, setter absent» Name : string Methods + «Constructor» Person(name : string, sin : string) + Login(password : string) : void + Logout() : void + ToString() : string Transaction Class Properties + «C# property, setter absent » AccountNumber : string + «C# property, setter absent» Amount : double + «C#...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string • speed : float Create a FigherPlane class that inherits from the Airplane class and adds the following attributes: • numberOfMissiles : short
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: name - "John Doe" address - use the default constructor of Address one constructor with all (two) parameters one input parameter for each attribute Methods public String toString() returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as...
this won't compile package com.test; public class CatalogItem { private String title; private double price; public...
this won't compile package com.test; public class CatalogItem { private String title; private double price; public CatalogItem(String title, double price) { super(); this.title = title; this.price = price; } public String getTitle() { return title; } public double getPrice() { return price; } } //Book.java package com.test; public class Book extends CatalogItem { private String author; private int ISBN; public Book(String title, double price, String author, int iSBN) { super(title, price); this.author = author; ISBN = iSBN; } public String...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT