Question

In: Computer Science

Declare a Song struct to store the data for a single song (make it a private member), and you must have anarray of Song structures as a member variable.

USE C++ 

Declare a Song struct to store the data for a single song (make it a private member), and you must have anarray of Song structures as a member variable.

Player(string name, float size) a constructor for the class. ’name' is a name for it
'size' is a maximum capacity in Mb.

addSong(string band, string title, string length, float size) a function for adding a new song.

'band' is a name of the band
'title' is a name of the song
'length' is a time length of that song in a '1:23' format (“mm:ss") 'size' is a size of the song in Mb

Return true if the song was added and false if there was not enough space (memory) for it or there was a song with the same band and title already in that device or if the device already has 1000 songs in it.

Solutions

Expert Solution

// CODE
#include
#include
#include
using namespace std;

class Player {
    public:
    Player(string name, float size) {
        this->name = name;
        this->size_allowed = size; // this is the max Mb allowed to store
        this->current_size = 0; // this is the Mb that is currently filled
        this->current_songs_count = 0; // songs currently present
    }
   
    bool addSong(string band, string title, string length, float size) {
        Song s; // first create a song structure for the data given
        s.band = band;
        s.title = title;
        s.length = length;
        s.size = size;
       
        // now check if the number of songs limit is reached
        if(current_songs_count > MAX_SONGS_ALLOWED) { // if the songs exceeds limit
            return false; // return false
        }
        if(current_size+s.size > size_allowed) { // if adding the song exceeds the memory limit
            return false; // return false
        }
       
        // check if there any duplicat of the current song exists
        for(int i = 0; i < current_songs_count; i++) {
            if((songs[i].title == s.title) && (songs[i].band == s.band)) {
                return false;
            }
        }
       
        songs[current_songs_count] = s; // add song to the player
        current_songs_count++; // update the number of songs
        current_size += s.size; // add the size of the song to the player's size
        return true;
    }
   
    // additional method to print the stats
    void printStats() {
        cout<<"************************************"<         cout<<"    Name        :"<         cout<<"    Total Songs : "<         cout<<"    Total Space : "<         cout<<"    Free Space : "<<(size_allowed-current_size)<         cout<<"************************************"<     }
    private:
    typedef struct Song {
        string band;
        string title;
        string length;
        float size;
    } Song;
   
    static const int MAX_SONGS_ALLOWED = 1000;
    int size_allowed;
    int current_size;
    int current_songs_count;
    Song songs[MAX_SONGS_ALLOWED];
    string name;
};

int main() {
    Player p("My Music Player", 1000); // create a music player with 1Gb space
   
    p.printStats();
    cout<     p.addSong("The Pebbles", "I am Fortunate", "2:23", 20);
   
    p.printStats();
    return 0;
}

// OUTPUT


Related Solutions

Song struct to store the data for a single song (make it a private member), and you must have an array of Song structures as a member variable.
USE C++ Song struct to store the data for a single song (make it a private member), and you must have an array of Song structures as a member variable.Player(string name, float size) a constructor for the class. ’name' is a name for it'size' is a maximum capacity in Mb.addSong(string band, string title, string length, float size) a function for adding a new song.'band' is a name of the band'title' is a name of the song'length' is a time length of...
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: **...
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: ** The head pointer ** The tail pointer */ public class Node { // declare member variables (data and next) // finish these constructors public Node(int data, Node next) {} public Node(int data) {} // HINT: use this() with next = null } // Initialize the linked list (set head and tail pointers) public MyLinkedList() {} @Override public boolean add(Integer item) { return false; }...
6. Why do you need to declare the data type of a variable before you can...
6. Why do you need to declare the data type of a variable before you can use it in Java? Give two (2) reasons 7. Is the World Wide Web and the Internet just two names for the same entity? Explain. 8. Why was it necessary to use the import statement when we used Scanner and Random? 9. Communication was a problem at Target. What would you recommend as an escalation process if someone encounters a threat and wants it...
Can you please describe 3 to 5 characteristics a data store design must have to be...
Can you please describe 3 to 5 characteristics a data store design must have to be considered a proper physical design.
Recall that for a random variable to be a binomial random variable, you must have an...
Recall that for a random variable to be a binomial random variable, you must have an experiment which meets the following three criteria: 1: There are exactly two outcomes for each trial. 2: There are a fixed number (n) of trials. 3: The trials are independent, and there is a fixed probability of success (p) and failure (q) for each trial. For each of the two situations described below, please indicate if the variable X (as defined in each situation)...
1. Define a class counterType to implement a counter. Your class must have a private data...
1. Define a class counterType to implement a counter. Your class must have a private data member counter of type int and functions to set counter to the value specified by the user, initialize counter to 0, retrieve the value of counter, and increment and decrement counter by one. The value of counter must be nonnegative. 2. Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication. Design a class bookType that...
Data Structures Use good style. Make sure that you properly separate code into .h/.cpp files. Make...
Data Structures Use good style. Make sure that you properly separate code into .h/.cpp files. Make sure that you add preprocessor guards to the .h files to allow multiple #includes. Overview You will be writing the classes Grade and GradeCollection. You will be writing testing code for the Grade and GradeCollection classes. Part 1 – Create a Grade Class Write a class named Grade to store grade information. Grade Class Specifications Include member variables for name (string), score (double). Write...
The Encrypt class has an integer private data member 8 element array named digits. The first four elements (0 ~ 3) are to store the original 4 digits integer and the next four (4 ~ 7) are to store the encrypted data
C++ Please Modify the code and create a new header fileA company wants to transmit data over the telephone, but is concerned that its phones could be tapped. All of the data are transmitted as four-digit integers. The company has asked you to write a program that encrypts the data so that it can be transmitted more securely. Your program should read a four-digit integer and encrypt it as follows: Replace each digit by (the sum of that digit plus...
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure...
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure you understood the concept of “the dot” in programming. Go ahead and declare an array of characters with a default length (whichever length you want, it's fine). Next, you want to ask the user for a phrase and store the characters in your array, but before you do that, ask the user for an estimated length of the array. If the length given is...
Write a C program that summarizes an order from a fictitious store. You must have these...
Write a C program that summarizes an order from a fictitious store. You must have these four lines in your program: float bike_cost, surfboard_cost; float tax_rate, tax_amount; int num_bikes, num_surfboards; float total_cost_bikes, total_cost_surfboards; float total_cost, total_amount_with_tax;
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT