CS226 -- Day 07 -- Spring 2013 JAVA REVIEW: Class Relationships Composition & Aggregation: These are HAS-A relationships between classes where one class contains members of another class. Composition is for individual members (Person HAS-A String for name). Aggregation is for collections of members (Deck HAS-A group of Cards). Inheritance: A (derived, sub) class can be defined to EXTEND an existing (base, super) class, inheriting all its data and methods. This creates an IS-A relationship between classes where one subclass is derived from a more general superclass. For example: Sedan IS-A Automobile, CheeseSteak IS-A Sandwich, Student IS-A Person. Interface: This is a collection of method headers, no definitions, possibly also some static data. Interfaces may have many different concrete implementations. If a class IMPLEMENTS an interface it must contain a definition for every method in the interface. An interface can be used as a reference variable, but you can't create an actual object of an interface type. Instead the reference variable holds an object from a class that IMPLEMENTS the interface. Phase 1: Problem Definition Suppose you want to create a program to keep a log of how you spend your time - attending classes, doing homework, working, hobbies, etc. It would provide ways to enter, edit and perhaps delete information, as well as search for things like how much time you spent on a particular course in a given week. Phase 2: Design Solution What classes would you create in designing an object oriented solution? Now think about similar programs that others may need: keeping a work timesheet, various forms of exercise that comprise a fitness plan, flight info for an airline, etc. Create a class system that could solve all these related problems, not just the original one. Where would inheritance and interfaces come in handy? Make a list of classes and interfaces, and the relationships between them. If you're familiar with UML diagrams, draw one to demonstrate the elements of your design. Comparable interface Time interface extends Comparable TimeMilitary implements Time: use 24 hour clock TimeDate interface extends Time: date & time, compareTo, addHours, addDays Activity: name, start time, end time, getDuration Event extend Activity: location Database Interface: (work on collection of something), add, delete, find Schedule Interface extends Database: (work on collection of Activities), can add/delete/sort, getTotalTime(activity type) ScheduleTable implements Schedule: HAS Activity Flight extends Activity: fromAirport, toAirport WorkTask extends Activity: client, billingRate Exercise extends Activity: caloriesBurned See activities.pdf for UML diagram of above classes & interfaces. Now let's design some tests so that we will know if each interface is properly implemented. We have interface Time.java, implementation TimeMil.java, and starter JUnit test file TimeTest.java. See junit.txt for an explanation of junit and how to run in a command-line environment. See on-line tutorials for how to use in Eclipse.