Need the most help with the C++ side of things please. I have the Java and it is
ID: 667569 • Letter: N
Question
Need the most help with the C++ side of things please. I have the Java and it is below the question at the bottom.
You are to build an Ant build-able project that contains both Java and C++programs. The project folder should be Assign2 and its structure should be the same as you created in Assign1. Your Ant build file should contain the following targets:
· build.java This target compiles the Java source code to create class files in the Assign2/classes folder.
· execute.java This target depends on build.java and causes your java sample program to be execute. See the sample screen shot below to see sample output from the program.
· build.cpp This target compiles the C++ source code to create an executable image in the Assign2/bin folder. Use the cpptasks cc task to compile withg++.
· execute.cpp Similar to execute.java, but for the C++ program.
· clean This target removes the Assign2/bin and Assign2/classes folders.
· prepare This target creates the Assign2/bin and Assign2/classes folders.
You may want to use javaFraction.jar, and cppFraction.jar as a basis for creating each program. Extract each with:
jar xvf cppFraction.jar
from a folder in which you'll place Ser321 example programs, such as:~/Ser321/Examples/. You may also want to create the folder~/Ser321/Assigns/Assign2) for building this assignment solution.
Program Description Both the Java and C++ programs have the same functionality, as described below. The Java solution will require at least two files:MediaDescription.java and MediaLibrary.java. The C++ solution should consist of five files: MediaLibrary.hpp, MediaLibrary.cpp,MediaDescription.hpp,MediaDescription.cpp, and main.cpp.
MediaDescription is a class that wraps the properties of a music or video file. Place the following properties in your MediaDescription class.
· mediaType Its value should either Music, or Video.
· title The title of the song or the video/movie.
· author The artist of a song or the leading actor/actress of video.
· album Unused for videos, but is used to group related songs.
· genre Used to group videos.
· filename The file name in whic the media is stored. Most commonly the is title.mp3 or title.mp4
Provide constructors accessor and getter methods as appropriate. Provide a toString() method for a MediaDescription object which returns a string suitable for printing.
The MediaLibrary class should provide storage and management for a collection of media. The library is used to store MediaDescription objects. In future assignments, the MediaLibrary will become a server running on the Raspberry Pi. Clients will connect to the server and use the methods provided by theMediaLibrary to create a user-interface for browsing and playing media. You may assume that all titles stored in a library object are unique. YourMediaLibraryclass should provide the following methods.
· public boolean add(MediaDescription aClip); A method to add a new song or video the library. True is returned when the request is successful.
· public boolean remove(String aTitle); A method to remove the namedMediaDescription from the library.
· public MediaDescription get(String aTitle); Returns the media description with title aTitle.
· public String[] getTitles(); Returns an array of strings, which are all of the titles in the library.
· public String[] getMusicTitles(); Returns an array of strings, which are all of the music titles in the library.
· public String[] getVideoTitles(); Returns an array of strings, which are all of the video titles in the library.
For your C++ version of MediaLibrary you may find it easier to return astd::vector<string> rather than an array of strings. Thus the signature ofgetTitleswould be:
std::vector<string> getTitles(); //returns a vector of strings of all media titles in the library
You should provide a simple main program for both the Java and C++ media libraries. The program should demonstrate that all methods in the library work. For example your code in the Java main may initialize a library with code like:
After initializing a MediaLibrary object, the main should demonstrate that all methods work --> not a thorough test, but demonstrate all of the library methods. For example, here is a screen shot of the instructors Java solution. Note that you do not need to print out the Json representation of each MediaDescription, just convert it to a string, which distinguishes each of the fields.
public class main {
public static void main(String[] args) {
MediaLibrary myLib = new MediaLibrary();
boolean addTest = myLib.add(new MediaDescription("Music","Come Monday","Jimmy Buffett","Uptown Special","Island","ComeMonday.mp3"));
addTest = myLib.add(new MediaDescription("Music","Fins","Jimmy Buffett","Greatest Hits","Island","Fins.mp3"));
addTest = myLib.add(new MediaDescription("Video","Banana Song","Minions","","Animation","minionsbananasong.mp4"));
addTest = myLib.add(new MediaDescription("Video","Banana","Minions","","Animation","minionsbanana.mp4"));
System.out.print("Music titles in the library are ");
for (String s: myLib.getMusicTitles()) {
System.out.print(s + ", ");
}
System.out.print(" ");
System.out.print("Video titles in the library are ");
for (String s: myLib.getVideoTitles()) {
System.out.print(s + ", ");
}
System.out.print(" ");
for (MediaDescription m: myLib.getTheMedia()) {
System.out.println(m);
}
boolean removeTest = myLib.remove("Fins");
System.out.print("Titles after removing Fins are ");
for (String s: myLib.getTitles()) {
System.out.print(s + ", ");
}
}
}
/**
* Purpose: Wraps the properties of a music or video file.
*
* SER-321 Foundations of Distributed Applications
* see http://pooh.poly.asu.edu/Ser321
* @author Anthony J Kowal III
* @version Sept 1, 2015
*/
public class MediaDescription {
/**
* A media type of either Music or Video.
*/
private final String mMediaType;
/**
* A title of a song or a video/movie.
*/
private final String mTitle;
/**
* A name for the author/actor/actress of the media.
*/
private final String mAuthor;
/**
* A name for the album of the song.
*/
private final String mAlbum;
/**
* A genre of the video.
*/
private final String mGenre;
/**
* A filename of the media
*/
private final String mFilename;
public MediaDescription() {
mMediaType = null;
mTitle = null;
mAuthor = null;
mAlbum = null;
mGenre = null;
mFilename = null;
}
/**
* MediaDescription constructor.
* @param mediaType a media type of music or video
* @param title the title of the media
* @param author the author of the media
* @param album the album only applying to music
* @param genre the genre of the media
* @param filename the filename of the media
*/
public MediaDescription(String mediaType, String title, String author, String album, String genre, String filename) {
mMediaType = mediaType;
mTitle = title;
mAuthor = author;
mAlbum = album;
mGenre = genre;
mFilename = filename;
}
/**
* Returns a media type of either music or video.
* @return a media type.
*/
public String getMediaType() {
return mMediaType;
}
/**
* Returns a title of a song or video/movie.
*
* @return a title of the media.
*/
public String getTitle() {
return mTitle;
}
/**
* Returns a name for the author of the media or leading actor/actress of video.
*
* @return a name of the author/actor/actress
*/
public String getAuthor() {
return mAuthor;
}
/**
* Returns a name for the album of the song.
*
* @return a name of the album.
*/
public String getAlbum() {
return mAlbum ;
}
/**
* Returns a genre of the video/movie.
*
* @return a genre of the video.
*/
public String getGenre() {
return mGenre ;
}
/**
* Returns a filename of the media file.
*
* @return a filename of the media.
*/
public String getExtras() {
return mFilename ;
}
@Override
public String toString() {
String theFile = ""fileName":"" + mFilename + """;
String theAuthor = ""author":"" + mAuthor + """;
String theAlbum = ""album":"" + mAlbum + """;
String theGenre = ""genre":"" + mGenre + """;
String theType = ""mediaType":" + mMediaType;
String theTitle = ""title":"" + mTitle + """;
return "{" + theFile + "," + theAuthor + "," + theAlbum + "," + theGenre + "," + theType + "," + theTitle + "}";
}
}
import java.util.ArrayList;
import java.util.Iterator;
/**
* Purpose: Provides storage and management for a collection of media.
*
* SER-321 Foundations of Distributed Applications
* see http://pooh.poly.asu.edu/Ser321
* @author Anthony J Kowal III
* @version Sept 1, 2015
*/
public class MediaLibrary{
ArrayList<MediaDescription> theMedia;
int musicCount, videoCount;
public MediaLibrary() {
theMedia = new ArrayList<MediaDescription>();
}
public boolean add(MediaDescription aClip) {
theMedia.add(aClip);
if (aClip.getMediaType() != null && aClip.getMediaType().equals("Music")) {
musicCount++;
} else if (aClip.getMediaType() != null && aClip.getMediaType().equals("Video")) {
videoCount++;
}
System.out.println("Adding: " + aClip.getTitle());
return true;
// A method to add a new song or video the library. True is returned when the request is successful.
}
public boolean remove(String aTitle) {
MediaDescription removal = null;
for (MediaDescription m : theMedia) {
if (m.getTitle() != null && m.getTitle().equals(aTitle)) {
System.out.println("Removing: " + m.getTitle());
removal = m;
break;
}
}
theMedia.remove(removal);
return true;
// A method to remove the named MediaDescription from the library.
}
public MediaDescription get(String aTitle) {
for (MediaDescription m : theMedia) {
if (m.getTitle() != null && m.getTitle().equals(aTitle)) {
return m;
}
}
// Returns the media description with title aTitle.
return null;
}
public String[] getTitles() {
String[] mediaArray = new String[theMedia.size()];
int count = 0;
for (MediaDescription m : theMedia) {
if (m.getMediaType() != null) {
mediaArray[count] = m.getTitle();
count++;
}
}
return mediaArray;
// Returns an array of strings, which are all of the titles in the library.
}
public String[] getMusicTitles() {
String[] musicArray = new String[musicCount];
int count = 0;
for (MediaDescription m : theMedia) {
if (m.getMediaType() != null && m.getMediaType().equals("Music")) {
musicArray[count] = m.getTitle();
count++;
}
}
return musicArray;
// Returns an array of strings, which are all of the music titles in the library.
}
public String[] getVideoTitles() {
String[] videoArray = new String[videoCount];
int count = 0;
for (MediaDescription m : theMedia) {
if (m.getMediaType() != null && m.getMediaType().equals("Video")) {
videoArray[count] = m.getTitle();
count++;
}
}
return videoArray;
// Returns an array of strings, which are all of the video titles in the library.
}
public ArrayList<MediaDescription> getTheMedia() {
return theMedia;
}
}
Need the C++ version of this
Explanation / Answer
MediaDescription.cpp
#include "MediaDescription.hpp"
#include <sstream>
#include <iostream>
MediaDescription::MediaDescription(string mediaType, string title, string author,string album, string genre, string filename):
mediaType(mediaType),
title(title),
author(author),
album(album),
genre(genre),
filename(filename)
{}
MediaDescription::MediaDescription():
mediaType(""),
title(""),
author(""),
album(""),
genre(""),
filename("")
{}
string MediaDescription::str() const
{
return this->toJSON();
}
string MediaDescription::toJSON() const
{
std::stringstream json;
json << "{" <<""fileName": " << """ << valueOrNull(this->filename) << """ <<
", "author": " << """ << valueOrNull(this->author) << """ <<
", "album": " << """ << valueOrNull(this->album) << """ <<
", "genre": " << """ << valueOrNull(this->genre) << """ <<
", "mediaType": " << """ << valueOrNull(this->mediaType) << """ <<
", "title": " << """ << valueOrNull(this->title) << """ <<"}";
return json.str();
}
string MediaDescription::valueOrNull(const string &value) const
{
return string("").compare(value) == 0 ? string("null") : value;
}
void MediaDescription::setMediaType(string mediaType)
{
this->mediaType = mediaType;
}
void MediaDescription::setAuthor(string author)
{
this->author = author;
}
void MediaDescription::setTitle(string title)
{
this->title = title;
}
void MediaDescription::setAlbum(string album)
{
this->album = album;
}
void MediaDescription::setGenre(string genre)
{
this->genre = genre;
}
void MediaDescription::setFilename(string filename)
{
this->filename = filename;
}
string MediaDescription::getMediaType() const
{
return this->mediaType;
}
string MediaDescription::getAuthor() const
{
return this->author;
}
string MediaDescription::getTitle() const
{
return this->title;
}
string MediaDescription::getGenre() const
{
return this->genre;
}
string MediaDescription::getFilename() const
{
return this->filename;
}
MediaDescription.hpp
#include <string>
using namespace std;
class MediaDescription
{
public:
MediaDescription(string, string, string, string, string, string);
MediaDescription();
string str() const;
string toJSON() const;
void setMediaType(string);
void setTitle(string);
void setAuthor(string);
void setAlbum(string);
void setGenre(string);
void setFilename(string);
string getMediaType() const;
string getTitle() const;
string getAuthor() const;
string getAlbum() const;
string getGenre() const;
string getFilename() const;
private:
string valueOrNull(const string&) const;
string mediaType;
string title;
string author;
string album;
string genre;
string filename;
};
MediaLibrary.cpp
#include <iostream>
#include <unordered_map>
#include <vector>
#include "MediaLibrary.hpp"
using namespace std;
MediaLibrary::MediaLibrary():
media(new unordered_map<string, MediaDescription>()),
musicTitles(new std::vector<string>()),
videoTitles(new std::vector<string>()),
DEBUG(true)
{}
MediaLibrary::~MediaLibrary()
{
delete media;
delete musicTitles;
delete videoTitles;
}
bool MediaLibrary::add(MediaDescription aClip)
{
log(string("Adding: ") + aClip.getTitle());
string title = aClip.getTitle();
string mediaType = aClip.getMediaType();
(*media)[title] = aClip;
bool isMusic = string("Music").compare(mediaType) == 0;
bool isVideo = string("Video").compare(mediaType) == 0;
if (isMusic)
{
musicTitles->push_back(title);
} else if (isVideo)
{
videoTitles->push_back(title);
}
else
{ }
return true;
}
bool MediaLibrary::remove(const string& aTitle)
{
log(string("Removing " + aTitle));
MediaDescription title = (*media)[aTitle];
string mediaType = title.getMediaType();
bool isMusic = string("Music").compare(mediaType) == 0;
bool isVideo = string("Video").compare(mediaType) == 0;
if (isMusic)
{
musicTitles->push_back(title.getTitle());
} else if (isVideo)
{
videoTitles->push_back(title.getTitle());
}
media->erase(aTitle);
return true;
}
template <class T>
void MediaLibrary::deleteFromVector(std::vector<T>& vector, T item)
{
typedef typename std::vector<T>::iterator iterator;
for (iterator iter = vector.begin(); iter != vector.end(); ++iter)
{
if (vector->at(iter).compare(item) == 0)
{
vector->erase(iter);
break;
}
}
}
MediaDescription MediaLibrary::get(string aTitle)
{
return (*media)[aTitle];
}
std::vector<string> MediaLibrary::getTitles()
{
std::vector<string> * titleVector = new std::vector<string>();;
for (unordered_map<string, MediaDescription>::iterator keyVal = media->begin(); keyVal != media->end(); ++keyVal)
{
titleVector->push_back(keyVal->first);
}
return *titleVector;
}
std::vector<string> MediaLibrary::getMusicTitles()
{
return *(this->musicTitles);
}
std::vector<string> MediaLibrary::getVideoTitles()
{
return *(this->videoTitles);
}
void MediaLibrary::log(string message) const
{
if (DEBUG)
{
cout << message << " ";
}
}
MediaLibrary.hpp
#include "MediaDescription.hpp"
#include <vector>
#include <string>
#include <unordered_set>
#include <unordered_map>
using namespace std;
class MediaLibrary
{
public:
MediaLibrary();
~MediaLibrary();
bool add(MediaDescription aClip);
bool remove(const string& aTitle);
MediaDescription get(string);
std::vector<string> getTitles();
std::vector<string> getMusicTitles();
std::vector<string> getVideoTitles();
template <class T>
void deleteFromVector(std::vector<T>&, T item);
private:
const bool DEBUG;
void log(string message) const;
unordered_map<string, MediaDescription> * media;
std::vector<string> * musicTitles;
std::vector<string> * videoTitles;
};
main.cpp
#include "MediaLibrary.hpp"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void printTitles(string mediaType, std::vector<string> titles)
{
cout << mediaType << " titles in the library are ";
for (int index = 0; index < titles.size(); ++index)
{
cout << titles.at(index) << ", ";
}
cout << " ";
}
int main()
{
MediaLibrary myLib;
myLib.add(MediaDescription("Music","Come Monday","Jimmy Buffett","Greatest Hits","Island","ComeMonday.mp3"));
myLib.add(MediaDescription("Music","Fins","Jimmy Buffett","Greatest Hits","Island","Fins.mp3"));
myLib.add(MediaDescription("Video","Banana Song","Minions","","Animation","minionsbananasong.mp4"));
myLib.add(MediaDescription("Video","Banana","Minions","","Animation","minionsbanana.mp4"));
printTitles("Music", myLib.getMusicTitles());
printTitles("Video", myLib.getVideoTitles());
cout << "Song Fins in Json is " << myLib.get("Fins").toJSON() << " ";
cout << "Song Come Monday in Json is " << myLib.get("Come Monday").toJSON() << " ";
cout << "Video Banana Song in Json is " << myLib.get("Banana Song").toJSON() << " ";
cout << "Video Banana in Json is " << myLib.get("Banana").toJSON() << " ";
myLib.remove("Fins");
printTitles("All", myLib.getTitles());
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.