Skip to main content.

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

Objectives: In this assignment, you will

Due: Tuesday, Oct 19 at 11:59 p.m.

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. There are multiple versions of Eclipse, but you should download the Eclipse IDE for Enterprise Java and Web Developers (the second one on the list) 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.

Make sure that you're in the "Java Perspective" by going to Window --> Perspective --> Open Perspective --> Java

If you have used Eclipse before, create a new workspace for this course: go to File --> Switch Workspace, choose Other and then make a new workspace directory (e.g., cs209_workspace).

Set Up

GitHub Classroom

Accept the assignment and copy the clone link.

Configure Git in Eclipse

Go into Eclipse. Eclipse has a plugin for Git called EGit. Configure EGit to use your GitHub username and password.

  1. Go to the "Preferences" menu. On a Mac, look under the "Eclipse" menu. On Windows, click on the ‘Window’ menu bar option, then choose ‘Preferences’.
  2. Type “git” in the search bar, then choose that path ‘Team > Git > Configuration’.
  3. If the following entries don't already exist, click ‘Add Entry...’. Enter user.name as the Key, and your GitHub username as the Value, then add another entry with user.email as the Key and your corresponding email as the Value.

Clone the Repository in Eclipse

  1. Open the Git Repositories view by going to Window --> 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.
  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 repository. (Look at the icons available.) Hopefully, you can keep using 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 repository, although we haven't talked about a bunch of these terms explicitly.
  2. 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".
  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 a name that includes assignment 6.
  4. Expand the project to see its (limited) contents.

You may see errors. They are likely okay at this point. You haven't set anything up yet. They should clear up soon.

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.

Committing

You'll see the README.md file has an asterisk 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. See below for more information.

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 saw on the project folder.

With the project selected, 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" should go away after you save the file (when the file gets recompiled).

State

All media items have the following characteristics:

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; 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 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

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

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:

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.

Back to our regularly scheduled programming...

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 (you can think of this as an album) 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 CD.

DVD

Create a DVD class. You can think of this as a movie, documentary, or a TV series. DVDs 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 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 Assignment6 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: