Assignment 6: Eclipse, Inheritance, Interface and Collections Practice
Objective: In this assignment, you will
- create 4 classes that could be used to maintain the storage of media items in a library or that an individual owns. These classes will represent generic media items, CDs, DVDs, and audio books
- write a driver program that uses these classes and illustrates polymorphism.
- modify a class to implement interfaces with the type parameter
- use a Collection
- use the Eclipse IDE and its features to ease the implementation of the classes.
Due: Before next Wednesday's class.
Overview
You are to implement four
classes, MediaItem
, CD
,
DVD
,
and AudioBook
. CD
, DVD
,
and AudioBook
must all
extend MediaItem
. You must provide at least one
constructor for each class that takes parameters to set all instance
variables. You should make all the instance
variables private
in all classes. (Use
the super
mutator methods when appropriate.) You are not
to repeat instance variables from MediaItem
in the
classes that extend it. You will override methods in
MediaItem
in the various child classes, as appropriate.
Using Eclipse
Start Eclipse on the command line by
typing eclipse
.
If necessary, click the arrow button that says "Go to Workbench"
If you have used Eclipse before, create a new workspace for this class:
go to File --> Switch Workspace
, choose Other
and then make a new workspace directory (e.g., cs209_workspace).
Set Up
Create a new Java project. Depending on your Eclipse perspective, you should
be able to select File --> New --> Project
and then
Java
to create a new Java project. Otherwise, you may
need to do a search or something similar to select Java Project
.
Name the project Assign6
. (I believe all the default values are
correct and you can click OK to all the dialog boxes.) The Assign6
project is stored in a directory in your Eclipse workspace
directory, which is located in your home directory
by default.
Creating a class
With the Assign6
project selected, create a new class
from the File
menu. Name it the
MediaItem
class. The package name should
be edu.wlu.cs.yourusername
. Make the
class abstract
. You can have Eclipse create a
default main
method and generate comments for you if you
select those checkboxes, which I recommend.
All media items have the following characteristics:
- a title
- a variable to track if that item is currently present in the collection
- a playing time
- a copyright year
Create appropriate instance variables for this data.
Create a constructor. Consider the benefits of having a
constructor in an abstract class. You can automatically generate
constructors from the Source
menu.
All media items have the following behaviors:
- get the title
- get the copyright year
- get if the item is present in the collection
- set the status of the item, in or out of the collection
- get the playing time; it may be useful to get this information in several formats, for example in minutes or in hours and minutes
- a
toString
method
Don't just write these methods. Use Eclipse to do
the heavylifting for you. Right click on your program, and
select Source
--> Generate Getters and Setters
or Override/Implement Methods
and select the
appropriate methods. (See how much easier that is?)
Make sure you test each class along the way. You can put
your test code in the
main
method of the class. It's easier to catch errors if
you test small parts.
Executing Your Code
You can execute the test code in the main
method using
several different methods:
- hit the "play" button (which means run) in the top bar. This
will only work if the code has a
main
method OR - right-click on the Java file and select "Run as" and then "Java application" OR
- use the Run menu and select "Run as" and then "Java application"
Follow one of the above procedures for executing the code you write in the rest of the assignment.
Creating Child Classes
CDs, DVDs, and audio books have all of the characteristics and behaviors of media items.
Create a CD
class. In the "New Class" window, make
sure you enter the name of the parent (i.e., super) class (MediaItem
).
CDs have the following additional characteristics and behaviors:
- an artist
- a number of tracks
- a way to get the artist
- a way to get the number of tracks
Create classes for DVD
and
AudioBook
DVDs have the following additional characteristics and behaviors:
- a rating, e.g., G, PG PG-13, R, NC-17. These examples are from
the MPAA ratings system. There are different ratings systems in
other countries so use a
String
for the rating. - a length of time for the bonus features
- a way to get the rating
- a way to get the time of the bonus features
- a way to get the total playing time of the DVD. (Note this
should override
MediaItem
's method for getting the playing time.)
Audio books have the following additional characteristics and behaviors:
- an author
- a narrator
- a way to get the author
- a way to get the narrator
A driver program
To test your classes, you will write a driver program that uses them. (Of course, you were testing each of your classes along the way too, right?)
The driver program emulates a library that keeps track of all the media that is in a library. Your driver program will
- Create at least two instances of each type of media
- Store the instances in an array
- Exercise the accessor and mutator methods of the objects (e.g., check item into or out of the library)
- Print out all pertinent information about each item in the library, e.g., if the item is in or out of the library
Save the output in a file. The alternatives:
- Copy/paste the output from the console into a file, within the Assign6 directory so I see it/you submit it. OR
- In the terminal, go into the
bin
directory of your Assign6 project directory and runjava edu.wlu.cs.username.Driver > ../outputfile
The command puts the output file in the parent directory so that you can see it in Eclipse. If you don't see it in Eclipse, refresh the navigator display.Alternatively, the following command may work (students report mixed results)
java edu/wlu/cs/username/Driver > ../outputfile
Using Interfaces with Type Parameter (30)
Modify your MediaItem
class
to implement
the Comparable
interface. It
is up to you how you compare the items, although it should be
something that makes sense.
Let Eclipse do most of the work for you. (Note that this would be a little more straightforward in Eclipse if you added that the class should implement an interface during creation of the class, but I didn't want to give you too much to start.) When you start typing to implement the Comparable interface, use control-spacebar to let Eclipse complete "Comparable", which will automatically add the type parameter. Eclipse will give you an error about unimplemented methods; click on the error and select to implement methods. That will give you the method template--you still need to fill in the method.
In your driver program, sort and display the elements in the array.
Save your output in a file named witharray.out
.
Modifying Library Driver: Using Collections (35 pts)
Create a copy of your driver program, named CollectionDriver
We discussed several different types of collections: lists, sets, and maps. In your new driver program, instead of an array, use an appropriate collection to store your items. Before making your choice, consider how easily you can implement the same functionality, e.g., store and retrieve items, and sort items, and what new functionality the collection will add/allow.
This part is underspecified on purpose. In comments in your driver program, defend your choice of collection.
Save your output in a file
named withcollection.out
.
Cleaning Up Your Code
If I have been giving you feedback about poor variable names, poor method naming, poor coding practices, poor formatting, inefficient code, not encapsulating, etc., clean up your code before submitting. Use Eclipse's refactoring and formatting tools and note any warnings too and see if you can clean up your code a bit.
Javadocs
Using Eclipse, generate template Javadocs for all of your classes, including your driver program. You may find it useful to refer to these while you're developing too. Make sure you fill in the templates with useful information about the method/class.
To generate the Javadoc, use File --> Export
and
under Java, select Javadocs. Make sure that all the classes are selected, not
just one class.
Putting the Javadocs on the Web
We're having some technical difficulties with the new CS dept web server. Some things are set up differently than in the past. Bear with me.
If you do not have a public_html
directory in your home directory, then we need to make the link by running the following command in a terminal in your home directory:
ln -s /home/www/users/MYUSERNAME public_html
From your home directory, go into
your public_html
directory and create
a cs209
directory. Inside
the cs209
directory, create
a assign6
directory.
Copy the Javadocs you generated into
your ~/public_html/cs209/assign6
directory.
OR, export the Javadocs again and make the destination be this
directory.
Open a web browser and point
to http://cs.wlu.edu/~yourusername
You should
see your cs209
directory, if you don't
already have a home page. Click on the link. You should see
your assign6
directory. If you already have
a homepage, you can make a link to your cs209 directory from the web
page. If you're getting permission denied errors, you can navigate directly
to http://cs.wlu.edu/~yourusername/cs209/assign6
(If the above link doesn't work, try http://hydros.cs.wlu.edu/~yourusername/cs209/assign6
)
Load the HTML documentation for your classes up in a browser, by
clicking on the assign6
link. Check
if the Javadocs are complete. If not, go back and update them in
Eclipse and regenerate them. If you want examples of
Javadocs, just look at the Java classes API.
My web pages are not showing up on this server yet because PHP does not seem to be enabled.
Setting Up Your Path
In the hopes of having a smoother submit process while the
server/system set up continues to be updated, I'm going to put some
Bash scripts in our course's shared
directory. We'll set up your PATH
variable so that the shell will find these scripts.
- In a text editor, open
up
.bash_profile
. The file is in your home directory, but you may not be able to see it because it's a hidden file. You can open it using something likejedit ~/.bash_profile
. Below the line that looks something like:PATH=$PATH:$HOME/.local/bin:$HOME/bin
add a line that saysPATH=$PATH:/csdept/local/courses/cs209/shared
- Save the file. In a terminal, run the command
source ~/.bash_profile
, which updates your bash (the shell we use) settings. Then, run the commandecho $PATH
to see if your path now includes the CS209 directory.
Turnin
From the CS Machines
Export the project from Eclipse as a Jar file, as discussed in
class, exporting the source files too. Save the file as assign6.jar
in
your cs209
directory.
Run the script submitJar.sh assign6
. If you get an
error message, make sure that your path is correct.
From Remote
Eclipse Remote System Access instructions.
Export the project from Eclipse as a Jar file, as discussed in
class, exporting the source files too. Save the file as assign6.jar
. Using Eclipse, copy
the assign6.jar
file into your
(remote) cs209
directory.
Open an ssh terminal by right-clicking on Ssh Shells
in the fred.cs.wlu.edu
hierarchy, and
choosing Launch Shell
. In the shell that opens,
run submitJar.sh assign6
. If you get an
error message, make sure that your path is correct.
Grading (200 pts)
You will be evaluated based on the following criteria:
- (70 pts) correctness and OO style of your classes and driver program
- (30 pts) Implementing
Comparable
- (35 pts) Implementation, explanation of new collection
- (30 pts) Testing, as demonstrated by your
main
methods and the driver program - (30 pts) completeness of your Javadocs
- (5 pts) Javadocs are accessible on the Web