Please modify my code so it outputs the right answer. When you run my following
ID: 3748657 • Letter: P
Question
Please modify my code so it outputs the right answer. When you run my following program, there is a difference between the expected answer and what I receive. all of it must run in multifile and must implement the following main.cpp.
No changes can be made at all to the main.cpp. it should be left as it is.
thank you.
I feel like the basis of my program is right but please check what needs to be fixed in order to get the right answer. and please tell me what was missing in my code. but please do not change the main.cpp file
Here is the answer when my code runs gets
Here is the expected answer and you can see the slight differences in my code. Another little difference to fix is that the last line of my answer outpitus "
here is the expected answer and the right .please modify my code so that it shows this.
main.cpp to use
int main() {
//**********Test Song************//
//instantiate 5 songs
Song song1;
song1.setTitle("title 1");
song1.setAuthor("author 1");
song1.setAlbum("album 1");
Song song2("title 2", "author 2", "album 2");
Song song3("title 3", "author 3", "album 3");
Song song4("title 4", "author 4", "album 4");
Song song5("title 5", "author 5", "album 5");
//output song information
cout << "The first song is: " << song1.getTitle() << ", " << song1.getAuthor() << ", " << song1.getAlbum() << endl;
cout << "The second song is: " << song2.getTitle() << ", " << song2.getAuthor() << ", " << song2.getAlbum() << endl;
cout << "The third song is: " << song3.getTitle() << ", " << song3.getAuthor() << ", " << song3.getAlbum() << endl;
cout << "The fourth song is: " << song4.getTitle() << ", " << song4.getAuthor() << ", " << song4.getAlbum() << endl << endl;
//************* Test PlayList*************//
//instantiate PlayList and add Songs to it
PlayList myPlayList(song1);
myPlayList.addSong(song2);
myPlayList.addSong(song3);
myPlayList.displayPlayList();
cout << "Playlist now holds " << myPlayList.getNumberOfSongs() << " songs ";
myPlayList.addSong(song1);
myPlayList.displayPlayList();
cout << endl;
myPlayList.addSong(song4);
myPlayList.displayPlayList();
cout << endl;
myPlayList.addSong(song5);
myPlayList.displayPlayList();
cout << endl;
myPlayList.removeSong(song2);
myPlayList.displayPlayList();
cout << endl;
myPlayList.removeSong(song3);
myPlayList.displayPlayList();
cout << endl;
myPlayList.clearPlayList();
myPlayList.displayPlayList();
cout << myPlayList.isEmpty() << endl;
return 0;
}
setinterface.h (this is an abstract class and set implements this method)
#ifndef SET_INTERFACE_H_
#define SET_INTERFACE_H_
#include <vector>
template<class ItemType>
class SetInterface
{
public:
/** Gets the current number of entries in this set.
@return The integer number of entries currently in the set. */
virtual int getCurrentSize() const = 0;
/** Checks whether this set is empty.
@return True if the set is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to this set.
@post If successful, newEntry is stored in the set and
the count of items in the set has increased by 1.
@param newEntry The object to be added as a new entry.
@return True if addition was successful, or false if not. */
virtual bool add(const ItemType& newEntry) = 0;
/** Removes a given entry from this set,if possible.
@post If successful, anEntry has been removed from the set
and the count of items in the set has decreased by 1.
@param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtual bool remove(const ItemType& anEntry) = 0;
/** Removes all entries from this set.
@post set contains no items, and the count of items is 0. */
virtual void clear() = 0;
/** Tests whether this set contains a given entry.
@param anEntry The entry to locate.
@return True if set contains anEntry, or false otherwise. */
virtual bool contains(const ItemType& anEntry) const = 0;
/** Fills a vector with all entries that are in this set.
@return A vector containing all the entries in the set. */
virtual std::vector<ItemType> toVector() const = 0;
}; // end SetfInterface
#endif /* SET_INTERFACE_H_ */
set.cpp
#include "Set.h"
#include <cstddef>
template<class ItemType>
Set<ItemType>::Set(): item_count_(0), max_items_(DEFAULT_SET_SIZE)
{
} // end default constructor
template<class ItemType>
int Set<ItemType>::getCurrentSize() const
{
return item_count_;
} // end getCurrentSize
template<class ItemType>
bool Set<ItemType>::isEmpty() const
{
return item_count_ == 0;
} // end isEmpty
template<class ItemType>
bool Set<ItemType>::add(const ItemType& newEntry)
{
bool has_room_to_add = (item_count_ < max_items_);
if (has_room_to_add)
{
items_[item_count_] = newEntry;
item_count_++;
} // end if
return has_room_to_add;
} // end add
template<class ItemType>
bool Set<ItemType>::remove(const ItemType& anEntry)
{
int located_index = getIndexOf(anEntry);
bool can_remove_item = !isEmpty() && (located_index > -1);
if (can_remove_item)
{
item_count_--;
items_[located_index] = items_[item_count_];
} // end if
return can_remove_item;
} // end remove
template<class ItemType>
void Set<ItemType>::clear()
{
item_count_ = 0;
} // end clear
template<class ItemType>
bool Set<ItemType>::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
template<class ItemType>
std::vector<ItemType> Set<ItemType>::toVector() const
{
std::vector<ItemType> bag_contents;
for (int i = 0; i < item_count_; i++)
bag_contents.push_back(items_[i]);
return bag_contents;
} // end toVector
// private
template<class ItemType>
int Set<ItemType>::getIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int search_index = 0;
// If the bag is empty, item_count_ is zero, so loop is skipped
while (!found && (search_index < item_count_))
{
if (items_[search_index] == target)
{
found = true;
result = search_index;
}
else
{
search_index++;
} // end if
} // end while
return result;
} // end getIndexOf
set.h
#ifndef SET_
#define SET_
#include "SetInterface.h"
template<class ItemType>
class Set : public SetInterface<ItemType>
{
private:
static const int DEFAULT_SET_SIZE = 4; // Small size to test for a full bag
ItemType items_[DEFAULT_SET_SIZE]; // Array of bag items
int item_count_; // Current count of bag items
int max_items_; // Max capacity of the bag
// Returns either the index of the element in the array items that
// contains the given target or -1, if the array does not contain
// the target.
int getIndexOf(const ItemType& target) const;
public:
Set();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
std::vector<ItemType> toVector() const;
};
#include "Set.cpp"
#endif
Song.cpp
#include "Song.h"
#include <cstddef>
Song::Song():author_(""), album_(""){
}
Song::Song(const string& title, const string& author, const string& album){
title_ = title;
author_ = author;
album_ = album;
}
void Song::setTitle(string title){ //"set" in setTitle here means "give a value" and has nothing
title_ = title ;
}
void Song::setAuthor(string author){
author_ = author;
}
void Song::setAlbum(string album){
album_ = album;
}
string Song::getTitle() const{
return title_;
}
string Song::getAuthor() const{
return author_;
}
string Song::getAlbum() const{
return album_;
}
bool operator==(const Song& lhs, const Song& rhs){
if(lhs.getTitle()==rhs.getTitle() && lhs.getAlbum()==rhs.getAlbum() && lhs.getAuthor()==rhs.getAuthor()){
return true;
}
return false;
}
song.h
#ifndef SONG_H_
#define SONG_H_
#include <iostream>
#include "SetInterface.h"
using namespace std;
class Song{
private:
std::string title_;
std::string author_;
std::string album_;
public:
Song();
Song(const std::string& title, const std::string& author = "", const std::string& album = "");
void setTitle(std::string title); //"set" in setTitle here means "give a value" and has nothing
// to do with the Set class. Similarly for setAuthor and setAlbum
void setAuthor(std::string author);
void setAlbum(std::string album);
std::string getTitle() const;
std::string getAuthor() const;
std::string getAlbum() const;
friend bool operator==(const Song& lhs, const Song& rhs);
};
#endif
playlist.cpp
#include "PlayList.h"
#include <cstddef>
#include "Set.h"
PlayList::PlayList(){
}
PlayList::PlayList(const Song& a_song){
playlist_.add(a_song);
}
int PlayList::getNumberOfSongs() const{
return playlist_.getCurrentSize();
}
bool PlayList::isEmpty() const{
return playlist_.isEmpty();
}
bool PlayList::addSong(const Song& new_song){
playlist_.add(new_song);
return true;
}
bool PlayList::removeSong(const Song& a_song){
playlist_.remove(a_song);
return true;
}
void PlayList::clearPlayList(){
playlist_.clear();
}
void PlayList::displayPlayList() const{
cout << "Playlist contains:" <<endl;
std::vector<Song> v = playlist_.toVector();
for (vector<Song>::iterator it=v.begin(); it!=v.end(); ++it){
cout << "* Title: " << (*it).getTitle() << " * Author: " << (*it).getAuthor() << " * Album: " << (*it).getAlbum() << " * " <<endl;
}
cout << "End of playlist" <<endl;
}
playlist.h
#ifndef PLAYLIST_H_
#define PLAYLIST_H_
#include "Set.h"
#include "Song.h"
using namespace std;
class PlayList{
private:
Set<Song> playlist_;
public:
PlayList();
PlayList(const Song& a_song);
int getNumberOfSongs() const;
bool isEmpty() const;
bool addSong(const Song& new_song);
bool removeSong(const Song& a_song);
void clearPlayList();
void displayPlayList() const;
};
#endif //end
Thank you
Explanation / Answer
I fixed the add() function in Set.cpp. When ever we add a new item into Set, we must make sure that the item does not already exist in the Set. So I changed the add() function to use the contains() function to check if an item is existing in the Set.
Please do rate the answer if it helped. thank you.
Set.cpp
-------
#include "Set.h"
#include <cstddef>
template<class ItemType>
Set<ItemType>::Set(): item_count_(0), max_items_(DEFAULT_SET_SIZE)
{
} // end default constructor
template<class ItemType>
int Set<ItemType>::getCurrentSize() const
{
return item_count_;
} // end getCurrentSize
template<class ItemType>
bool Set<ItemType>::isEmpty() const
{
return item_count_ == 0;
} // end isEmpty
template<class ItemType>
bool Set<ItemType>::add(const ItemType& newEntry)
{
bool can_add = (item_count_ < max_items_) && !contains(newEntry);
if (can_add)
{
items_[item_count_] = newEntry;
item_count_++;
} // end if
return can_add;
} // end add
template<class ItemType>
bool Set<ItemType>::remove(const ItemType& anEntry)
{
int located_index = getIndexOf(anEntry);
bool can_remove_item = !isEmpty() && (located_index > -1);
if (can_remove_item)
{
item_count_--;
items_[located_index] = items_[item_count_];
} // end if
return can_remove_item;
} // end remove
template<class ItemType>
void Set<ItemType>::clear()
{
item_count_ = 0;
} // end clear
template<class ItemType>
bool Set<ItemType>::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
template<class ItemType>
std::vector<ItemType> Set<ItemType>::toVector() const
{
std::vector<ItemType> bag_contents;
for (int i = 0; i < item_count_; i++)
bag_contents.push_back(items_[i]);
return bag_contents;
} // end toVector
// private
template<class ItemType>
int Set<ItemType>::getIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int search_index = 0;
// If the bag is empty, item_count_ is zero, so loop is skipped
while (!found && (search_index < item_count_))
{
if (items_[search_index] == target)
{
found = true;
result = search_index;
}
else
{
search_index++;
} // end if
} // end while
return result;
} // end getIndexOf
output
------
The first song is: title 1, author 1, album 1
The second song is: title 2, author 2, album 2
The third song is: title 3, author 3, album 3
The fourth song is: title 4, author 4, album 4
Playlist contains:
* Title: title 1 * Author: author 1 * Album: album 1 *
* Title: title 2 * Author: author 2 * Album: album 2 *
* Title: title 3 * Author: author 3 * Album: album 3 *
End of playlist
Playlist now holds 3 songs
Playlist contains:
* Title: title 1 * Author: author 1 * Album: album 1 *
* Title: title 2 * Author: author 2 * Album: album 2 *
* Title: title 3 * Author: author 3 * Album: album 3 *
End of playlist
Playlist contains:
* Title: title 1 * Author: author 1 * Album: album 1 *
* Title: title 2 * Author: author 2 * Album: album 2 *
* Title: title 3 * Author: author 3 * Album: album 3 *
* Title: title 4 * Author: author 4 * Album: album 4 *
End of playlist
Playlist contains:
* Title: title 1 * Author: author 1 * Album: album 1 *
* Title: title 2 * Author: author 2 * Album: album 2 *
* Title: title 3 * Author: author 3 * Album: album 3 *
* Title: title 4 * Author: author 4 * Album: album 4 *
End of playlist
Playlist contains:
* Title: title 1 * Author: author 1 * Album: album 1 *
* Title: title 4 * Author: author 4 * Album: album 4 *
* Title: title 3 * Author: author 3 * Album: album 3 *
End of playlist
Playlist contains:
* Title: title 1 * Author: author 1 * Album: album 1 *
* Title: title 4 * Author: author 4 * Album: album 4 *
End of playlist
Playlist contains:
End of playlist
1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.