OOP Lab04 InheritancePolymorphism
OOP Lab04 InheritancePolymorphism
0. Assignment Submission
For this lab class, you will have to turn in your work twice, specifically:
Right after the class: for this deadline, you should include any work you have done within the lab
class.
10PM three days after the class: for this deadline, you should include the source code of all
sections of this lab, into a branch namely “release/lab04” of the valid repository.
After completing all the exercises in the lab, you have to update the use case diagram and the class
diagram of AIMS project.
Each student is expected to turn in his or her own work and not give or receive unpermitted aid.
Otherwise, we would apply extreme methods for measurement to prevent cheating. Please note that you
need to write down answers for all questions into a text file named “answers.txt” and submit within it
within your repository.
Page 1 of 11
Figure 1. Import existing project
Page 2 of 11
length 0 or less, the system must notify the user that the track, the DVD or the CD of that track cannot be
played.
● Instead of typing the accessor methods for these fields, you may use the Generate Getter and
Setter option in the Outline view pop-up menu (i.e., Right Click -> Source -> Generate Getters
and Setters...). Note that in reality, not all attributes need to have getter and setter. We only create
this when necessary. Getter and setter generator of Eclipse also let you decide with attribute will
get getter or setter or both.
● Next, create addAuthor(String authorName) and removeAuthor(String
authorName) for the Book class
o The addAuthor(...) method should ensure that the author is not already in the
ArrayList before adding
o The removeAuthor(...) method should ensure that the author is present in the
ArrayList before removing
o Reference to some useful methods of the ArrayList class
Page 3 of 11
4. Creating the abstract Media class
At this point, the DigitalVideoDisc and the Book classes have some fields in common namely id,
title, category and cost. Here is a good opportunity to create a common superclass between the two, to
eliminate the duplication of code. This process is known as refactoring. You will create an abstract class
called Media which contains these fields and their associated get and set methods.
Create the Media class in the project
- In the Package Explorer view, right click to the project and select New -> Class. Adhere to the
following specifications for the new class:
● Package: hust.soict.dsai.aims.media
● Name: Media
● Access Modifier: public, abstract
● Superclass: java.lang.Object
● Constructors from Superclass: Check
● public static void main (String[] args): do not check
● All other boxes: Do not check
- Add fields to the Media class
● To store the information common to the DigitalVideoDisc and the Book classes, the Media
class requires four private fields: int id, String title, String category and float
cost
● You will want to make public accessor methods for these fields (by using Generate Getter and
Setter option in the Outline view pop-up menu)
- Remove fields and methods from Book and DigitalVideoDisc classes
● Open the Book.java in the editor
● Locate the Outline view on the right-hand side
● Select the fields id, title, category, cost and their accessors & mutators (if exist)
● Right click the selection and select Delete from the pop-up menu
● Save your changes
- Do similarly for the DigitalVideoDisc class and move it to the package
hust.soict.dsai.aims.media. Remove the package hust.soict.dsai.aims.disc.
● After doing that you will see a lot of errors because of the missing fields
● Extend the Media class for both Book and DigitalVideoDisc
o public class Book extends Media
o public class DigitalVideoDisc extends Media
● Save your changes.
5.2. Create the Track class which models a track on a compact disc and will
store information incuding the title and length of the track
- Add two fields: String title and int length
- Make these fields private and create their getter methods as public
- Create constructor(s) for this class.
- Save your changes
Page 5 of 11
● For the Track class, insert the keywords implements Playable after the keywords public
class Track
- Implement play() for DigitalVideoDisc and Track
● Add the method play() to these two classes
● In the DigitalVideoDisc, simply print to screen:
public void play() {
System.out.println("Playing DVD: " + this.getTitle());
System.out.println("DVD length: " + this.getLength());
}
● Similar additions with the Track class
- Implement play() for CompactDisc
● Since the CompactDisc class contains a ArrayList of Tracks, each of which can be played
on its own. The play() method should output some information about the CompactDisc to
console
● Loop through each track of the arraylist and call Track's play() method
Page 6 of 11
f. To create this field, type the following code in the Cart class, in place of the
itemsOrdered array declaration that you deleted:
private ArrayList<Media> itemsOrdered = new
ArrayList<Media>();
- Note that you should import the java.util.ArrayList in the Cart class
g. A quicker way to achieve the same affect is to use the Organize Imports feature within
Eclipse
h. Right-click anywhere in the editor for the Cart class and select Source -> Organize Imports
(Or Ctrl+Shift+O). This will insert the appropriate import statements in your code.
i. Save your class
- Create addMedia() and removeMedia() to replace addDigitalVideoDisc() and
removeDigitalVideoDisc()
- Update the totalCost() method
Page 7 of 11
e==null:o.equals(e)). So the contains() method actually use equals() method to
check equality.
- Please override the boolean equals(Object o) of the Media and the Track class so
that two objects of these classes can be considered as equal if:
+ For the Media class: the title is equal
+ For the Track class: the title and the length are equal
When overriding the equals() method of the Object class, you will have to cast the Object
parameter obj to the type of Object that you are dealing with. For example, in the Media class,
you must cast the Object obj to a Media, and then check the equality of the two objects’ attributes
as the above requirements (i.e. title for Media; title and length for Track). If the passing
object is not an instance of Media, what happens?
Note: We can apply Release Flow here by creating a topic branch for the override of equals()
method.
Page 8 of 11
Note: The Comparator interface is a comparison function, which impose a total ordering on some
collection of objects. Comparators can be passed to a sort method (such as Collections.sort())
to allow precise control over the sort order.
Please open the Java docs to see the information of this interface.
- Create two classes of comparators, one for each type of ordering
- Implement the compare() method of each comparator class to reflect the ordering that we want,
either by title then cost, or by cost then title. You may utilize the method
Comparator.thenComparing()to sort using multiple fields.
- Add the comparators as attributes of the Media class:
Question: Alternatively, to compare items in the cart, instead of using Comparator, we can use the
Comparable interface and override the compareTo()method. You can refer to the Java docs to see
the information of this interface.
Suppose we are taking this Comparable interface approach.
- What class should implement the Comparable interface?
- In those classes, how should you implement the compareTo()method be to reflect the ordering that we
want?
- Can we have two ordering rules of the item (by title then cost and by cost then title) if we use this
Comparable interface approach?
- Suppose the DVDs has a different ordering rule from the other media types, that is by title, then
decreasing length, then cost. How would you modify your code to allow this?
Page 9 of 11
13. Create a complete console application in the Aims class
In the main method of Aims, you will now implement a complete console application, by first create an
instance of the Store class and then, provide a list of functionalities through a menu that the user can
interact with. For the home interface, you will create the main menu as following:
public static void showMenu() {
System.out.println("AIMS: ");
System.out.println("--------------------------------");
System.out.println("1. View store");
System.out.println("2. Update store");
System.out.println("3. See current cart");
System.out.println("0. Exit");
System.out.println("--------------------------------");
System.out.println("Please choose a number: 0-1-2-3");
}
From the main menu, if the user choses option “View store”, the application will display all the
items in the store, and a menu as following:
public static void storeMenu() {
System.out.println("Options: ");
System.out.println("--------------------------------");
System.out.println("1. See a media’s details");
System.out.println("2. Add a media to cart");
System.out.println("3. Play a media");
System.out.println("4. See current cart");
System.out.println("0. Back");
System.out.println("--------------------------------");
System.out.println("Please choose a number: 0-1-2-3-4");
}
o The option “See a media’s details” will ask the user to enter the title of the media and
display the information of that media. Please remember to check the validity of the title.
Under the information display, the system also shows the following menu (note that the
“Play” option is only available to CD and DVD type.
public static void mediaDetailsMenu() {
System.out.println("Options: ");
System.out.println("--------------------------------");
System.out.println("1. Add to cart");
System.out.println("2. Play");
System.out.println("0. Back");
System.out.println("--------------------------------");
System.out.println("Please choose a number: 0-1-2");
}
o The option “Add a media to cart” will ask the user to enter the title of the media that
he/she sees on the screen (the list of medias in store), then add the media to cart. Please
remember to check the validity of the title. After adding a DVD to cart, the system will
display the number of DVDs in the current cart.
o The option “Play a media” will ask the same input from the user as option 2. You should
again check the validity of the title.
From the main menu, if the user choses option “Update store”, the application will allow the
user to add a media to or remove a media from the store
Page 10 of 11
From the main menu, if the user choses option “See current cart”, the application will display
the information of the current cart, and a menu as following:
Page 11 of 11