CS 210 – Fundamentals of Programming I
Spring
2007 – Programming Assignment 7
20 points
Out:
April 2 & 3
Due: April 11 & 12
This assignment puts together everything we have covered in this course in preparation for Programming Project 2.
Consider the following specification for an Album class that keeps track of information and rating of an album to be used in a music library application. The Album class models information about a single, distinct album title. The information we need to keep track of includes the artist of the album, the title of the album, the genre of the album, the year the album was recorded, a list of song tracks on the album, and the user's rating of the album (from 0 to 5). In this specification, each Album object is responsible for maintaining all of the data in its care.
Specification for Album class
Attributes - data stored in the object
|
Objects |
Type |
Name |
|
Artist of album |
string |
myArtist |
|
Title of album |
string |
myTitle |
|
Genre of album |
string |
myGenre |
|
Year of recording |
int |
myYear |
|
List of song tracks |
vector<string> |
myTracks |
|
User's rating |
int |
myRating |
Operations - analyses of interface objects only with narrative descriptions of behavior
Default constructor - integer attributes are initialized to 0 (string and vector objects are automatically initialized to empty when created)
Analysis - no objects
Explicit-value constructor - initialize attributes from received values.
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
Initial artist |
string |
variable |
received |
initialArtist |
|
Initial title |
string |
variable |
received |
initialTitle |
|
Initial genre |
string |
variable |
received |
initialGenre |
|
Initial year |
int |
variable |
received |
initialYear |
|
Initial track list |
vector<string> |
variable |
received |
initialTracks |
|
Initial user rating |
int |
variable |
received |
initialRating |
Artist - returns the artist of this album
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
Album artist |
string |
variable |
returned |
myArtist |
Title - returns the title of this album
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
Album title |
string |
variable |
returned |
myTitle |
Genre - returns the genre of this album
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
Album genre |
string |
variable |
returned |
myGenre |
Year - returns the year this album was recorded
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
Year album was recorded |
int |
variable |
returned |
myYear |
Rating - returns the user's rating of this album
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
User's rating of album |
int |
variable |
returned |
myRating |
HasTrack - returns true if the song title is in the track list for this album; false otherwise
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
Song title |
string |
variable |
received |
songTitle |
|
Result of search |
bool |
variable |
returned |
----- |
RandomTrack - returns a random song title from the album's track list
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
Random song title |
string |
variable |
returned |
----- |
An example of using the built-in pseudo-random generator was in Problem 1 of the practical exam. It is defined in the C standard library (<cstdlib>). The RandomTrack operation should assume that the generator has been seeded by the program using Album objects. The function rand() returns a random number between 0 and RAND_MAX. The easiest way to scale the random number to a range for accessing the track list is to compute the remainder of the random number divided by the number of tracks.
ChangeRating - changes the user's rating of this album to newRating
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
New rating |
int |
variable |
received |
newRating |
AddTrack - adds a song title to the track list of this album, does nothing if the song title is already in the track list
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
Song title to add |
string |
variable |
received |
songTitle |
friend operator== - returns true if the left operand has the same artist, title, and year as the right operand
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
Left operand |
Album |
variable |
received |
leftOperand |
|
Right operand |
Album |
variable |
received |
rightOperand |
|
Result of compare |
bool |
variable |
returned |
----- |
friend operator< - returns true if the left operand's user rating is less than the right operand's user rating
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
Left operand |
Album |
variable |
received |
leftOperand |
|
Right operand |
Album |
variable |
received |
rightOperand |
|
Result of compare |
bool |
variable |
returned |
----- |
friend operator>> - Reads artist, title, genre, year, track list ending with sentinel value END_OF_TRACKLIST, and user's rating from an input stream without prompting.
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
Input stream to read from |
istream |
variable |
received, |
inStream |
|
Album to read into |
Album |
variable |
passed back |
theAlbum |
As with all uses of operator>>, the prompting is done by the calling function. To simplify input, we will assume that all string inputs are single words so that they may be entered with spaces or newlines between them using the regular input operator (>>). Multi-word titles, names, and genres will be input with underscores between the words or in camel case, e.g., Born_to_Run or NewAge. Because the track list is of arbitrary size, the sentinel value marking the end of the list must be part of the input, but is not to be stored in the Album object. For example, a prompt might look like:
Please enter the new data in the following order: artist, title, genre, year, track list ending in END_OF_TRACKLIST, and rating separated by spaces or newlines:
and the user might respond with:
Springsteen Born_to_Run Rock 1975 Thunder_Road Born_to_Run Jungleland END_OF_TRACKLIST 5
friend operator<< - Output album information in a nice format
Analysis - what data is received, passed back, returned?
|
Objects |
Type |
Kind |
Movement |
Name |
|
Output stream to write to |
ostream |
variable |
received, |
outStream |
|
Album to output |
Album |
variable |
received |
theAlbum |
For example, the output for the Album object read in above might look like:
Artist: Springsteen Title: Born_to_Run Genre: Rock Year recorded: 1975 Track list: Thunder_Road Born_to_Run Jungleland User rating: 5
Write the implementation of the Album class. The Album class definition should be put in header file album.h with suitable compilation guards. The implementations of the Album class operations should be put in source file album.cpp. [Note: there is no analysis and design portion for this assignment, since the full specification is given.]
A
driver program albumdriver.cpp
suitable for use to test your Album class implementation is available
on the course webpage under today's date. This program is to be
used as is. It should not be changed in any way. In particular,
this means that the names of the Album operations must match exactly
in spelling and case, the order of the parameters and arguments must
match exactly, and the name of the header file must be album.h,
Follow the guidelines in the on-line handout A C++ Programming Style Guideline for CS 210. As stated in the syllabus, part of the grade on a programming assignment depends on how well you adhere to the guidelines. The grader will look at your code listing and grade it according to the guidelines.
Electronically submit the Album class files (album.h and album.cpp) by emailing them as attachments to cs210@csserver.evansville.edu by 4:30pm on the due date. Do not send the driver program. You should not change the driver program in any way. (Make sure you send them to csserver.evansville.edu. If you send to just evansville.edu, it will not be delivered.)
04/02/07