Assignment 4: Eclipse, Inheritance, Interface, Collections, and Javadocs
Objectives: 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, albums, movies, 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.
- generate Javadocs
Due: Monday before class (October 28)
For timely completion of the assignment, I recommend completing
- Through "Using Eclipse: Creating a Class" by Monday
- Creating one child class by Wednesday
- Through "Creating Child Classes" by Thursday
Overview
You are to use Eclipse to implement four
classes, MediaItem
, Album
,
Movie
,
and AudioBook
. Album
, Movie
,
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.
You will generate Javadocs for your classes that can be viewed by
others using the package of classes you created. Recall that Javadocs
are automatically created from the Javadoc comments on your classes
and methods.
Continue reading to learn how you'll accomplish the above objectives.
Set Up
Eclipse
If you haven't already, set up Eclipse.
GitHub Classroom
Accept the assignment and copy the clone link.
Clone the Repository in Eclipse
- Go to the
Git Repositories
view (perhaps already open from the set up; otherwise, go toWindow --> Show View --> Other --> Git --> Git Repositories
). - Click
Clone a Git Repository
or click the clone icon. - If Eclipse did not populate the form for you, paste the clone URI into the appropriate field.
- Enter your user name and password (i.e., your personal access token).
- Click through the screens using "Next" (the defaults should be
right: you'll checkout the
main
branch) and then click "Finish".
Some students have had trouble with cloning through Eclipse. First, make sure you're using your personal access token for the password. If you're still having trouble, you can clone from the command line and then in Eclipse's Git Repositories view, you can add an existing local repository. (Look at the icons available.) Hopefully, you can use Eclipse to do other git commands.
Java Project in Eclipse
- In the Git Repositories view, expand the repository. You can explore the contents of the repository. We haven't used some of the terms you'll see yet.
- Expand
Working Tree
. This is the "current" version of the code. Right-click onAssignment4
and select "Import Project". In the dialog box, click "Finish". - You should now see a new Java project (a folder marked with a
J) in your Package Explorer view on the left side of Eclipse,
with the name
Assignment4
. You'll also see in square brackets the name of your repository and (if room) the name of the branch you're in. - Expand the project to see its (limited) contents.
Using Git within Eclipse
Everything you did in git
on the command line, you
can do through Eclipse. Hopefully, you're proficient at git, so
learning the Eclipse GUI will be relatively easy.
There are two main ways to use Git within Eclipse: through
the Team
menu and through the Git Staging
view.
If it's not already open, open the Git Staging
view
by going to Window --> Show View --> Other --> Git
Staging
. You can move the view to whichever pane you like. I
tend to put this view in the right pane
with Outline
.
Create a New Branch in Git
Right-click on the project, go to Team -->
Switch --> New Branch
. Name your branch whatever you
want. After creating the branch, you should see the branch name
change in the Package Explorer. (You may need to increase the width
of the Package Explorer view to see the branch name.)
Update the README
Add your name to the README.
Committing
You'll see the README.md file has an asterisk on it in the Package Explorer view because you changed it.
- In the Git Staging View, add README.md to be staged (using the + button)
- Add a commit message in that box.
- Set the author and committer fields to your name, if they haven't already been set.
- Commit (but don't push) those changes.
Merging
- Right-click on the project in the Package Explorer. Under
the
Team
menu, switch to themain
branch. - Merge your new branch into
main
. - Then, right-click on the project, and, under
the
Team
menu,Push
.
I am grading the main
branch only, so make sure
your changes are in there.
Continue your git practices of
branching, commiting, merging, and pushing as you were, using
the Git Staging view or the Team
menu.
Using Eclipse: Creating a Class
With the project selected in the Package Explorer, create a new
class from the File
menu
(File --> New --> Class
).
- For the package, enter
edu.wlu.cs.yourusername
, whereyourusername
is your username. - The class name is
MediaItem
. - Make the class
abstract
. - For interfaces, click "Add" and then type "Comparable",
select
java.lang.Comparable
, and then "OK". - Have Eclipse generate comments for you by selecting that checkbox.
The MediaItem.java
file will be created for you in
the newly created package directory, within the src directory.
There will be a red "X", indicating a compiler error because of the
default Comparable<T>
that Eclipse provided.
Replace the <T>
with <MediaItem>
. The red "X"/compiler error
will remain because you haven't implemented the methods
for Comparable
.
State
All media items have the following characteristics:
- a title
- a variable to track if that item is currently present in a collection (You are not implementing a collection at this point. That is the responsibility of another class.)
- a playing time
- a copyright year
Create appropriate instance variables for this data.
Eclipse may "complain", i.e., give a warning indicated with an exclamation mark in a yellow diamond, that the instance variables aren't being used. You are not done implementing yet, so ignore the warnings.
Constructors
Create two constructors. Consider the benefits of having a
constructor in an abstract class when you can't actually construct an
object of an abstract class. You can automatically generate
constructors from the Source
menu. One
constructor takes all the fields as parameters; the other constructor
takes all parameters except the parameter meaning if the item is
present and defaults to the item being present in the collection. The
constructors should not have duplicate code; have one constructor call
the other one.
Functionality
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 will be useful to get this information in two formats (separately): (1) in minutes and (2) in hours and minutes (e.g., "01:09" for something that runs 69 minutes)
- a
toString
method - a
compareTo
method (as specified in Comparable).
Don't just write these methods. Let Eclipse create the
boilerplate code for you. If your mouse is clicked into the
class, go to the Source
menu and
then Generate Getters and Setters
or Override/Implement Methods
and select the
appropriate methods. There is also an option
to Generate toString
. (See how much easier
that is?) Update the auto-generated comments to make the language
more natural. These comments will be important when you generate the
Javadocs later in the assignment.
It is up to you how to implement toString
, but it
should be something that makes sense. The
auto-generated toString
method is not, let's say, the
most natural way to display an object. You can start with that
implementation, but you should update it.
It is also up to you how you compare the items
in compareTo
, although it should be intuitive: what
would you expect the behavior to be to say that one MediaItem is
"before" another one in a sorted array of MediaItems? You should
document your decision in the Javadoc for that method.
Note that you can't test this class at this point because you can't create an object of an abstract type. You will need to test this class's methods in the concrete child classes.
Notes: Git, Testing, Stubs, and Executing Your Code
Let's pause here for some general notes and hints about using Eclipse and writing good code.
Git
In the Git Staging view, you'll see MediaItem.java marked with a
question mark. That means that git doesn't know about it
(isn't tracking it). You want git to know about it, so add
it to the index. (Use the button or right-click on the file and use
the Team
menu.)
When a tracked file is changed from the last time it was committed, you'll see an asterisk on it.
Delete the Unused_ToBeDeleted.java
file by
right-clicking on it and selecting Delete
and stage it
(to be deleted).
When you're ready, add a commit message, and commit the staged files.
Switching Branches
To switch branches, right-click on the project, go
to Team --> Switch to
and either create a new branch
or select an existing branch, like main
Merging
To merge a branch, right-click on the project, go
to Team --> Merge
and then select the branch that
you want to merge into this branch. Commit the result.
Push
After you're in the main branch, to push your code to GitHub,
right-click on the project, go to Team --> Push Branch
'main'
. You can also push from the Git Staging view.
Troubleshooting
If you're not seeing the menu items I mention, click the project again and then right-click again. You have to be in the right scope for certain menu items to come up.
Stub Methods
Eclipse will make stub methods for you, if you ask it to. After
you implement the methods, remove the TODO
comment.
Eclipse Tricks
- After you have written a method, type /** before the
method, and then hit
enter
and the Javadocs comment template will be automatically generated for you. - Use
command-spacebar
for possible completions. - Use
command-shift-F
to format code to be indented appropriately. - Type syso and then
command-spacebar
to expand that to printing to standard out.
Executing Your Code
Eclipse automatically compiles your code every time you save a Java file. You'll see red Xs on files that don't compile. You won't be able to run those classes or classes that use those uncompiled classes. So, you don't need to worry about explicitly compiling; just focus on running the programs.
You can execute the main
method of a class using
a few different approaches:
- hit the "play" button (which means run) in the top menu bar 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"
The above will only work if the class has a main
method.
Follow one of the above procedures for executing the code you write in the rest of the assignment.
Testing
Test each non-abstract class along the
way. Write one class, then test. It's easier to catch errors
if you test small parts. You also are less likely to
repeat/duplicate errors that you will then need to fix multiple
times. Put your test code in the
main
method of the class.
Back to our regularly scheduled programming...
Creating Child Classes
Albums, Movies, and audio books have all of the characteristics and behaviors of media items. You should leverage the parent class as much as possible.
Album
Create an Album
class. In the "New Java Class" dialog,
enter the name of the parent (i.e., super) class
(MediaItem
). Click the method stubs--specifically main
and "Constructors from superclass". Also, select the "Generate
comments" checkbox.
Albums 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
- override the
toString
method
You should either update the auto-generated constructor or overload the constructor so that it also takes as parameters initial values for the instance variables specific to the Album.
Reminder: Use your newly acquired Eclipse skills to make this task simpler.
Movie
Create a Movie
class. You can think of this as a
movie, documentary, or a TV series. Movies 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 Movie. (Note these
should override
MediaItem
's methods for getting the playing time.) - override the
toString
method
Again, the constructor should include parameters for the (initial) values of the instance variables.
Audio Books
Create an AudioBook
class. 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
- override the
toString
method
You should know what the constructor should do by now.
Another reminder to test all your (concrete) classes before moving on.
Driver Programs
Driver Program Using an Array
To test your classes, you will write a driver program
(called Driver
) that uses the
classes. (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 MediaItem 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
- Sort and display the elements in the array.
Another Library Driver: Using Collections
Create a copy of your driver program,
named Driver
but put it in the
package edu.wlu.cs.yourusername.collections
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
your README.md
file, defend your choice of
collection. Add an appropriate heading. Eclipse will let you
preview the README file as it would be rendered, but it doesn't always
handle all of the markdown well.
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 your final push. Use Eclipse's refactoring and formatting tools and note any warnings too and see if you can clean up your code a bit.
Javadocs
You have been writing Javadoc comments for all of your classes, methods, etc. Now it is time to generate the Javadocs, i.e., web pages that users can view to learn how to use your code--just like you have used to learn the Java API and the Goblin game.
Review the Javadoc comments on your classes, constructors, and
methods. Are they clear? Have you included the appropriate tags,
e.g., @param
and @return
?
Using Eclipse, generate Javadoc comments for all of your classes, including your driver programs. 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.
Using Eclipse, generate the Javadocs for
your edu.wlu.cs.username
package, by first clicking on
the Assignment4
project and then
using File --> Export
and under Java,
select Javadocs. Make sure that all the classes are selected, not
just one class. The default settings should all be fine. If the
destination directory isn't filled in, browse to the location of
your assignment's git repository and then add /doc
as
the directory name. On the last screen, check the button "Open
generated index file in browser" so you can see what the JavaDocs
look like.
Double check that you have comments for all the methods, constructors, and classes. You can regenerate the Javadocs after making changes to the Javadoc comments.
Add the Javadocs to your git repository.
If you want to view your updated Javadocs later after closing the browser, right-click on docs/index.html, select "Open With", and then select "Web Browser".
Submission
Put all of your code, README, Javadocs in your GitHub repository that you and I can access.
Grading (200 pts)
You will be evaluated based on the following criteria:
- (100 pts) Correctness and OO style of your classes
- (35 pts) Implementation, explanation of Collection in CollectionDriver
- (35 pts) Testing, as demonstrated by your
main
methods and the driver programs - (30 pts) Completeness of your Javadocs