Question

In: Computer Science

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 100

Music_collection();     

// sets max to n

Music_collection( int n );

// overloaded copy constructor

Music_collection( const Music_collection &m);

// returns true if add was successful,

// returns false if not enough room to add

bool add_tune( const Tune &t );

// sets the Tune at position index in collection to t,

// returns true if index < number   

bool set_tune( int index, const Tune &t );

// overloaded assignment operator

Music_collection & operator=( const Music_collection &m );

// Destructor

~Music_collection();

// overloaded stream insertion operator

// outputs the title of each Tune in the collection on a separate line

friend ostream & operator<<( ostream &out, const Music_collection &m );

};

Testing

Now let’s give your code a test run.

You should build a test harness that uses the following pseudo-code:

main()

{

//create a few Tune objects to use later on;

Music_collection A;

//add a few tunes to A;

Music_collection B(A);

//change a Tune in B using set_tune function;

Music_collection C;

C = B;

//add a Tune to B;

//change a Tune in C using set_tune function;

//print A,B,C;

}

After you test your work, comment out the assignment operator and copy constructor in .h and .cpp files of your Music_collection class implementation. re-compile and run your code. Compare the results with the previous run. (Ignore seg. fault and memory corruption errors, on terminal just check the lines right after you call your binary executable, i.e a.out)

Pay careful attention to the "boundary" or special cases: e.g. what happens if you try to assign an object to itself? (When could this happen?)

Remember to free the memory of the left-hand-side object of the assignment operator.

Remember to make deep copies, not shallow copies. (Make sure you can explain exactly what this means!)

What to submit

submit the following files (case sensitive):

  • music_collection.h (this file also contains Tune class)

  • music_collection.cpp

  • main.cpp

Programming language: C++

Requirement: Please tightly follow up the requirement as demonstrated above.

1. comment out the assignment operator and copy constructor in .h and .cpp files of your Music_collection class implementation. Re-complie and run your code

2. complete Music_collection.cpp file

3. provide illustration/comments with output result

Solutions

Expert Solution

#include<bits/stdc++.h>
using namespace std;

class Tune {
private:
        string title;
public:
        Tune();
        Tune( const string &n ) {
                title = n;
        }
        const string & get_title() const {
                return title;
        }
};

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 100
        Music_collection() {
                number = 0;
                max = 100;
                collection = (Tune*)malloc(max * (sizeof(Tune)));
        }

// sets max to n

        Music_collection( int n ) {
                number = 0;
                max = n;
                collection = (Tune*)malloc(max * (sizeof(Tune)));
        }

// overloaded copy constructor

        Music_collection( const Music_collection &m) {
                max = m.max;
                number = m.number;
                collection = (Tune*)malloc(max * (sizeof(Tune)));
                for (int i = 0; i < number; ++i) {
                        collection[i] = m.collection[i];
                }
        }

// returns true if add was successful,

// returns false if not enough room to add

        bool add_tune( const Tune &t ) {
                if (number == max) {
                        return false;
                }
                collection[number] = t;
                number++;
        }

// sets the Tune at position index in collection to t,

// returns true if index < number

        bool set_tune( int index, const Tune &t ) {
                if (index < number) {
                        collection[index] = t;
                        return true;
                }
                return false;
        }

// overloaded assignment operator

        Music_collection & operator=( const Music_collection &m ) {
                max = m.max;
                number = m.number;
                collection = (Tune*)malloc(max * (sizeof(Tune)));
                
                for (int i = 0; i < number; ++i) {
                        collection[i] = m.collection[i];
                }
                return *this;
        }

// Destructor

        // ~Music_collection();

// overloaded stream insertion operator

// outputs the title of each Tune in the collection on a separate line

        friend ostream & operator<<( ostream &out, const Music_collection &m );

};


std::ostream& operator<<(std::ostream& out, const Music_collection &m)
{
        for (int i = 0; i < m.number; ++i) {
                cout << m.collection[i].get_title() <<",";
        }
        cout<<endl;
        return out;
}

// Testing

// Now let’s give your code a test run.

// You should build a test harness that uses the following pseudo - code:

main()

{


//create a few Tune objects to use later on;

        Music_collection A;

//add a few tunes to A;
        Tune a("California");
        Tune b("Betles");
        Tune c("Dance");
        Tune d("Flying");

        A.add_tune(a);
        A.add_tune(b);
        A.add_tune(c);
        A.add_tune(d);



        Music_collection B(A);


//change a Tune in B using set_tune function;

        B.set_tune(2,Tune("Kamli"));

        Music_collection C;

        C = B;

//add a Tune to B;

        B.add_tune(Tune("Zamana"));

//change a Tune in C using set_tune function;

        C.set_tune(3,Tune("Loward"));


        cout<<A;
        cout<<B;
        cout<<C;

//print A,B,C;

}

OUTPUT:


Related Solutions

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...
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;       }...
public class Book{     public String title;     public String author;     public int year;    ...
public class Book{     public String title;     public String author;     public int year;     public String publisher;     public double cost;            public Book(String title,String author,int year,String publisher,double cost){        this.title=title;         this.author=author;         this.year=year;         this.publisher=publisher;         this.cost=cost;     }     public String getTitle(){         return title;     }         public String getAuthor(){         return author;     }     public int getYear(){         return year;     }     public String getPublisher(){         return publisher;...
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 +...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
public class GroceryShopping {    //declared variable    private String vegetableName;    private String fruitName;   ...
public class GroceryShopping {    //declared variable    private String vegetableName;    private String fruitName;    private double vegetablePrice;    private double fruitPrice;    private double vegetableOrdered;    private double fruitOrdered;       //declared constants    public static final double SERVICE_RATE =0.035;    public static final double DELIVERY_FEE=5;       public GroceryShopping( String vegetableName, String fruitName, double vegetablePrice, double fruitPrice)    {        this.vegetableName = vegetableName;        this.fruitName = fruitName;        this.vegetablePrice = vegetablePrice;        this.fruitPrice...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /**...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /** * Constructor for objects of class Classroom */ public Classroom() { this.capacity = 0; }    /** * Constructor for objects of class Classroom * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Classroom(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT