Working with Abstraction: Inheritance and Polymorphism
The goal of this lab is to help you to understand how to design abstractions by studying an existing hierarchy and then practice creating your own inheritance hierarchy and using it polymorphically within a collection.
Getting Started
For this lab exercise, work together with someone else in the class (for example your partner for the Game project):
- One person should fork the original lab_inheritance repository into their own account to provide a place for the group to edit, commit, and push changes
- On the resulting project web page, go to
Settings -> Membersto add your partner so both people in the group can access the same repository - Search for your partner's name and give them a
Maintainerrole in the project and chooseAdd to Project - Both students should clone the one forked repository to their individual machines so they can work on their own separate copies
- Import the project into IntelliJ (
New -> Project From Existing Sources)using the folder you just created (and just accept the defaults for all the options by selectingNext)
- The project is essentially empty except for the DISCUSSION markdown template and some standard project files.
Discussion
Use Gitlab's markdown format to summarize the group's discussion, not simply write direct answers to the questions, in the file called DISCUSSION.md that is included with the example code. IntelliJ includes a markdown editor that provides a preview or here is a web-based editor that provides similar functionality and can be shared like Google Docs.
Start by considering this Overview of Java's collection classes, which includes some design goals and links to the documentation as well. If there is something you like or dislike or something your learned about design during the discussion, make sure to back it up with a specific example. During your study, feel free to search the Internet for other resources beyond the Java documentation.
Use the following questions to guide your discussion:
- How many interfaces do specific concrete collection classes implement (such as
LinkedList)? What do you think is the purpose of each interface? - How many different implementations are there for a specific collection class (such as
Set)? Do you think the number justifies it being an interface or not? - How many levels of superclasses do specific concrete collection classes have? What do you think is the purpose of each inheritance level?
- What is the purpose of the utility classes
Collections,Arrays, andObjects? Why not add that functionality directly to the collection types themselves? - Are there any overlapping methods (ones that are in both a specific collection and a utility class)? If so, is there any guidance on which one you should use?
Identify, Implement, and Use Abstraction
Consider the following problem specification:
A parking garage provides a fixed number of parking spaces for vehicles and must maintain information about the vehicles that are currently in the parking garage so it can report the number of spaces available and the current value. A parking garage must provide publicly available functions to be called when a vehicle enters the garage and when a vehicle leaves the garage. Additionally, the garage must provide publicly available functions that report the capacity of the garage, the number of available spaces in the garage, and the total money collected. The parking fee is a flat rate that is collected when a vehicle leaves the parking garage.
Currently, there are three distinct types of vehicles, cars, low-emission cars, cargo trucks. Every vehicle has a license number consisting of letters and/or numbers and the number of spaces it occupies. Cars have a passenger capacity (i.e., the number of passengers). Trucks have a gross vehicle weight it can transport. Cars take up one space, while trucks take up a number of spaces that is their gross weight divided by 10,000. Cars are charged a rate of $8, low-emission cars are charged half that rate, and trucks are charged $10 per 10,000 pounds of gross weight.
The company wants to be able to sort its parking garages based on their current worth.
Note, tracking which specific spaces are occupied within the garage is beyond the scope of this exercise, just assume there is always room to fit another vehicle of any type until the garage runs out of enough available spaces.
Before starting to code, list the classes you think you will need to implement this program along with only their public method signatures (and especially NOT their instance variables). In reality, this is what an abstraction is within your program: a set of methods available to help solve your problem. Given what we know now, that can either be a superclass or interface, but we will continue to learn more ways to model higher level concepts during the semester. For each public method, note a feature in the problem specification you think it will help to implement.
Implement those methods you planned. If you are confident about what will be your super- and subclasses, then feel free to implement them directly. If you feel more comfortable implementing it directly as modes and then refactoring it that is fine as well. Create a main method that creates at least three garages with different sets of cars and prints out the value of each in sorted order.
After you are confident the code is complete, compare the result to your plan. Explain your reasoning for any of the following in your DISCUSSION file:
- any
publicmethods you added during the implementation or ended up not needing - any
protectedmethods you created - any classes you made
abstract
Submission
At the end of class, use Gitlab's Merge Request to submit your group's answers to the discussion questions above and your code to the original organization repository. Make sure the NetIDs of everyone in the group are in the title of your Merge Request and in the markdown file.