The goal of this lab is to continue your introduction to basic
concepts in the C++ programming language. Concepts you will be
familiar with after this lab include:
- file input/output
- Object-Oriented Programming (OOP) in C++
- inheritance and polymorphism
- working with a partner to develop a piece of software
- defensive programming (e.g., detecting improper input)
- designing and compiling multi-line programs
For this assignment, you will be working with a partner.
In future assignments, you will be able to select your partner.
However, for this lab, we have assigned partners for you.
Both partners should be present and working on the
code together. You will both be responsible for understanding
all concepts, so dividing and conquering is not an option.
The academic integrity
policy applies to the entire pair; you cannot work or share code with
anyone outside your partner.
You and your lab partner will share the same git repository, which is named lab02-<user1>-<user2>.git
Please access your lab by cloning the appropriate git repository, e.g.:
$ git clone git@github.swarthmore.edu:CS35-s17/lab01-jbrody1-adanner1.git
We've given you a skeleton version of the program.
Introduction
In this lab, you will create several classes representing different
types of media, including text, pictures, and videos. All of these
classes will be subclasses of an abstract Media class.
This Media class will encapsulate all functionality common to
all types of media. Your subclasses will add additional unique data
as well as implement inherited methods in a way specific to that
media. For example, "opening" a text file should be different from
"opening" a picture or video.
In addition, you will create a MediaLibrary class which
acts as a container for up to 50 Media objects (think of how
iTunes stores all of your eBooks, music, movies, etc.). You will
also build capability in the MediaLibrary class for
obtaining information about your different media objects and for
playing the media itself.
Below is an overview of the files required for submitting this
week's lab. Those highlighted in blue will require
implementation on your part. Those
in black are complete and should
not be modified except for comments.
- Makefile - contains scripts for
compiling. You should not change this file but are responsible
for understanding its contents.
- example1.library - small test file of
media items.
- example2.library - input file for loading
all media objects. You do not need to modify this except to add
more examples (optional).
- media.h - declares the Media
class, an abstract parent class of all media types.
- text.h/.cpp - declares and defines
the Text class, a subclass of Media. Some
of this implementation has been provided for you.
- image.h/.cpp - declares and defines
the Image class, a subclass of Media.
- video.h/.cpp - declares and defines
the Video class, a subclass of Media.
- mediaLibrary.h
and mediaLibrary.cpp - declares and defines
the MediaLibrary class. This class acts as the
overall container for all Media items.
- main.cpp - This is the main function for your
program.
- manualTests.cpp - This has another main
function that you can use to experiment with your
program. Graders will look at this file to see how you
tested your program.
- README.md - complete this when your lab is
finished and you are ready to make your final git push to hand
in the assignment.
Program Requirements
The
Media class
contains
virtual methods which are common to all media
objects. See the method comments on the declarations fo those classes
for more information about the behavior you need to implement.
This class keeps
track of all
Media objects in a library. It allows a user
to ask how many
Media objects are in the library and gives
them access to them on request. Its constructor takes in a single
string which represents the name of the file containing the media
library. In the
MediaLibrary constructor, you should open
this file and create
Media objects accordingly.
The Media Library File
When the program
starts, you should ask the user for the name of a media library file.
$ ./iswamedia
Welcome to the iSwat Media Library!
What is the name of your media file? example1.library
1. [image] Brown-Flames
2. [image] Aliens
3. [text] Gettysburg-Address
0. (quit)
What would you like to open?
0
Goodbye!
The first line of the media library file should indicate the number of
media objects it contains. Each item is represented by several lines
depending on what kind of media it is:
- For a text file, the next line will be text. The
following line will contain the name of the text file
e.g. (e.g. /usr/local/doc/GettsburgAddress); the next
line will contain the document's title
(e.g. Gettysburg-Address).
- For an image, the next line will be image. The
following line will contain the name of the image file
(e.g. /usr/swat/pics/brownflames.gif); the next line will
contain the image's title (e.g. Brown-Flames).
- For a video, the next line will be video. The
following line will be the URL for the video
(e.g. https://vimeo.com/28241047). The line after that
will be the title of the video (e.g. Running-Gerbil)
followed by a line containing the video length as a string
(e.g. (0:29))
You can assume that each line of the media library file contains a
single element
with no whitespace.
For example, the following is a valid
media library file.
The Application
As mentioned above, your main
function should ask the
user for the name of a media library file. Using that file,
it should display a menu listing the media files (in order) and
allow the user to pick a media file to open. Media files should be
listed by positive integers (that is, the first media file
you show should be selected by the input 1
). After
opening that media file, the menu should be repeated again; the
program should continue in this fashion until zero is entered, after
which it should terminate.
To display the media in a menu, you need to have a way of
representing it. The getDescription
method
is virtual
and unimplemented; each Media
subclass implements it in a different way.
- The
getDescription
method on Text
should give the string "[text] "
followed by the name
of the document (e.g. "[text] Gettysburg-Address"
).
- The
getDescription
method on Image
should do likewise but with the prefix "[image]"
(e.g. "[image] Brown-Flames"
).
- The
getDescription
method on Video
uses the prefix "[video]"
and suffixes the video
length in parentheses (e.g.
"[video] Running-Gerbil (0:29)"
).
The act of opening a media file is also performed by the
particular Media
subclass. The open
method of each Media
object should behave as
follows:
- The
open
method on Text
should open the file, read it line by line, and
print each line it reads.
- The
open
method on Image
should open the image viewer gpicview
to
display the image to the user. You can accomplish this by using the
system
function discussed below.
- The
open
method on Video
should open the URL of the video using
firefox
. You should use system
for this as well.
The system
Function
The library cstdlib
contains a function system
which can run commands for
you. You pass a C-style string to the system
function and it will run it in
much the same way as the shell we use to e.g. compile our programs. For
instance, the following program will open Firefox to the Google home page.
After it does that, it will open a text editor.
#include <cstdlib>
#include <string>
using namespace std;
int main(int argc, char** argv) {
string cmd1 = "firefox http://www.google.com";
string cmd2 = "gedit";
system(cmd1.c_str());
system(cmd2.c_str());
return 0;
}
In larger applications, we avoid using the system
function in favor of more
precise ways to launch other programs from within our own code. For this lab,
however, the system
function suits us quite well.
Getting Started
We recommend implementing your program in 5 parts:
- Initial file I/O -- be able to read in an example media
library from a file. For now, don't worry about
actually using this information; just print the contents
of the file to the screen so you know your file I/O is working.
- Text class -- be able to view a text file.
- Image and Video classes -- be able to
open an image or play a video.
- Media Library -- build and test a MediaLibrary
class which holds up to 50 media objects and lets the user
access the objects.
- Finish the main program -- once you have your media objects
and MediaLibrary set up, complete the program. When
completed, your program should ask the user for a media library,
present a menu of media objects, and allow the user to select and
open the media.
Your program should use good design, and you should use defensive
programming as you develop your code. Your program will be tested
on your understanding on how to define C++ classes and use
inheritance. In addition, you should implement small test functions
in between each phase.
Do not use your MediaLibrary to test and
debug your Media objects.
Compiling Your Program
Your code in this assignment is broken in to several different
files. To aid in the compilation process, we've created
a Makefile which should make compilation easy. To compile
your test program, just type:
$ make manualTests
Compiling your main program is even easier. Just enter:
$ make
Extra Challenges
There are many ways of
extending this lab. If you're looking for an extra challenge,
consider implementing one of the following:
- Songs. Design an implement a new media subclass to
represent songs. Represent the songs as mp3 files. When a user
"opens" a song, fire off a system call that plays the mp3.
- Design Changes. Are there any design changes you'd
make to the lab? i.e., are there any aspects of the lab design
that you weren't a fan of? If so, add a paragraph in your
README file describing which aspects/features you would have
done differently, and implement your changes.
- Additional Functionality. The current program provides
very basic functionality---given a library, your program will
only play selected files until the user decides to quit. Add
some additional functionlity to your media library program, such
as giving the user the ability to add or delete objects from the
library.
These additional features are optional -- we'll look at them, but
they won't be for credit. Make sure you complete
and push
the main lab before starting any extra challenge, and note in your
README file that you've done extra challenges so the graders know
which version of your lab to grade. (There's a chance your added
features will break our grading scripts; we'll grade the base
version of your lab to ensure you don't get penalized in the
off-chance your extra features break our grading scripts)
Survey
When you have completed your lab, answer a few short survey questions
in the file
README.md
Summary
To summarize the requirements of this lab:
- Your program must be able to read in and open text, image, and
video files.
- Your program must store media items in
a MediaLibrary object.
- You must incrementally test your program and save your tests
in manualTests.cpp
- You must complete the survey in the file README.md.
Submit
Once you are satisfied with your code, hand it in using git.
Remember the add, commit, push development cycle. You can push
as many times as you like, but only the most recent submission will be
graded. You may want to run git status to confirm all modifications have been pushed.