Assignment 7: Eclipse, Inheritance, Interface, Collections, and Javadocs Practice
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, 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.
- generate Javadocs
Due: Before next Wednesday's class.
Overview
You are to use Eclipse 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.
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.
Eclipse
Eclipse is a commonly used Java IDE. It can greatly improve your productivity, especially as you learn to use it well. There are other IDEs. Learning one IDE will make learning other IDEs easier.
Installing Eclipse
If you're not using a lab machine, you'll need to install Eclipse. Download the Eclipse IDE for Enterprise Java Developers so that you have the same version that is on the lab machines.
Using Eclipse
Start Eclipse. If you're on a lab machine, run Eclipse on the command line by typing eclipse.
Once Eclipse opens, if necessary, close the Welcome window to get to the typical view of Eclipse.
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
).
Make sure that you're in the "Java Perspective" by going
to Window --> Perspective --> Open Perspective
--> Java
Set Up
GitHub Classroom
Accept the assignment and copy the clone link.
Git Repository in Eclipse
Go into Eclipse.
- Open the
Git Repositories
view by going toWindow --> Show View --> Other --> Git Repositories
. You can move the view to whichever pane you like. I tend to put the view in the bottom pane. - 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.
- Click through the screens using "Next" (the defaults should be
right: you'll checkout the
master
branch) and then click "Finish".
Java Project in Eclipse
- In the Git Repositories view, expand the repository. You can explore the contents of repository, although we haven't talked about a bunch of these terms explicitly.
- Expand
Working Tree
. This is the "current" version of the code. Right-click on Working Tree 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 a name that includes assignment 7.
- 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.
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.
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. When you open the file, you will likely be in the preview Markdown mode. At the bottom of the editor, click on the "Markdown Source" tab rather than Preview tab.
Using Eclipse: Creating a Class
With the project selected, create a new folder from
the File
menu (File
--> New Folder
) called src
. This should get
rid of the error (the red exclamation point) that you seeing on
the project folder.
With the project selected, create a new class
from the File
menu (File --> New --> Class
).
- For the package, enter
edu.wlu.cs.yourusername
. - The class name is
MediaItem
. - Make the class
abstract
. - For interfaces, click "Add" and then type "Comparable",
select
java.lang.Comparable
, and then "OK". - You can have Eclipse generate comments for you if you select that checkbox, which I recommend.
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" should go away
after you save the file (when the file gets recompiled).
State
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.
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; see if you can 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
- a
toString
method - a
compareTo
method.
Don't just write these methods. Let Eclipse do the
heavylifting for you. If your mouse is clicked into the
class, go to Source
--> Generate Getters and Setters
or Override/Implement Methods
and select the
appropriate methods. (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.
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?
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
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. 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 menu.)
You'll also see the README.md file with an asterisk because you changed it. Add it to the index.
Set the author and committer fields to your name, if they haven't already been set.
When you're ready, add a commmit 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 master
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 master branch, to push your code to GitHub,
right-click on the project, go to Team --> Push Branch
'master'
Troubleshooting
If you're not seeing the menu items I mention, click the project again and then right-click again.
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.
Stub Methods
Eclipse will make stub methods for you, if you ask it to. After
you implement the methods, remove the TODO
comment.
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 code has a main
method.
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. You should leverage the parent class as much as possible.
CD
Create a CD
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.
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
- 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 CD.
DVD
Create a DVD
class. 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.) - 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 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 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.
Save the output in a file
called driver.out
. The alternatives:
- Copy/paste the output from the console into a new file
called
driver.out
within your project (but not within the source code directories) OR - Go to
Run --> Run Configurations
. The Name displayed should beDriver
. In the Common tab, check the Output File box. Click the "Workspace" box, select your project, and then add "/driver.out" to the file path.
Another Library Driver: Using Collections
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
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.
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 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
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 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.
Using Eclipse, generate the Javadocs for your edu.wlu.cs.username
package, by first clicking on the Assignment7
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 /docs
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.
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, output files - (30 pts) Completeness of your Javadocs