Skip to main content.

Assignment 5: Eclipse, Inheritance, Interface, Collections, and Javadocs Practice

Objectives: In this assignment, you will

Due: next Monday before class (October 30)

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.

Eclipse

If you haven't already, go back and set up Eclipse.

Set Up

GitHub Classroom

Accept the assignment and copy the clone link.

Clone the Repository in Eclipse

  1. Go to the Git Repositories view (perhaps already open from the set up; otherwise, go to Window --> Show View --> Other --> Git --> Git Repositories).
  2. Click Clone a Git Repository or click the clone icon.
  3. If Eclipse did not populate the form for you, paste the clone URI into the appropriate field.
  4. Enter your user name and password (i.e., your personal access token).
  5. 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. 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

  1. 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.
  2. Expand Working Tree. This is the "current" version of the code. Expand the Working Tree. Right-click on Assignment5 and select "Import Project". In the dialog box, click "Finish".
  3. 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 Assignment5. You'll also see in square brackets the name of your repository and (if room) the name of the branch you're in.
  4. 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.

Merging

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).

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:

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:

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.

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

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:

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:

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:

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:

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

Save the output in a file called driver.out. The alternatives:

Add the output to your repository.

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.

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 Assignment5 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.

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: