Persondid not naturally have a "title()" method. We ended up including it because the "toString()" method, which did naturally belong in class Person, called "title()", and that forced us to put some kind of "title()" function in there. The important point is this: we have no plan to ever have anyone create an instance of class Person; Mid and Prof, yes, but not Person. We have no intention of anyone ever calling the Person version of the method "title()". There's actually a very nice way of expressing this in OOP: we make "title()" a abstract method, and Person an abstract class.
An abstract method is a method that has no body, just a semi-colon. So we would declare "title()" like this:
public abstract String title();It's up to derived classes to provide actual implementations of the method. A class that has one or more abstract methods must itself be declared "abstract". The abstract modifier indicates that the class cannot be instantiated — if it were, we'd have an object on which the "title()" method might be called, but for which no title() body exists. So, an
abstractclass cannot be instantiated. Moreover, no class that extends an abstract class can be instantiated unless it provides defintions for all the abstract methods in the base class. In our Person/Mid/Prof example we are quite happy with these restrictions, since we never had any intentions of instantiating class Person, and Mid and Prof both provide their own definitions of title().
One of the early inspirations for object oriented programming was the needs of programs for doing simulations. In fact, object oriented programming really began with a language called "Simula", which was designed for writing simulations. So, we're going to consider a problem that could be a part of a sophisticated program for doing financial simulations (in the world of person finance ... I don't know anything about business-scale finance, being just a lowly professor). I want a program that will read in a file describing recurring events — like "every 14 days starting on 1/6/2015 I receive a paycheck" — and then simulate the day-by-day passing of time, printing out the events that occur in a day. [Handout Form]
|in0.txt||first 64 lines of output|
every 14 days start 1/6/2015 income Paycheck every 1 months start 1/15/2015 expense Mortgage Due every 1 months start 1/6/2015 expense Cable Due every 3 months start 3/21/2015 expense Water/Sewer Due every 3 months start 3/26/2015 income Stock Dividend every 1 months start 1/23/2015 expense Credit Card Due every 1 months start 1/11/2015 expense Car Payment Due every 1 months start 1/2/2015 expense Car Insurance Due every 1 months start 1/17/2015 expense Cell Phone Due every 1 days start 1/7/2015 prob 0.143 expense Groceries every 1 days start 1/1/2015 prob 0.15 expense Dinner Out every 7 days start 1/3/2015 prob 0.5 expense Breakfast Out every 4 days start 1/1/2015 prob 0.5 expense Gas every 1 days start 1/1/2015 prob 0.01 expense Car Repair every 12 months start 4/15/2015 expense Taxes Due every 1 months for 12 start 1/16/2015 expense Lazy Boy Layaway Payment Due
1/1/2015: Dinner Out, Gas 1/2/2015: Car Insurance Due 1/3/2015: Breakfast Out 1/5/2015: Gas 1/6/2015: Paycheck, Cable Due 1/7/2015: Groceries, Dinner Out 1/9/2015: Gas 1/10/2015: Breakfast Out 1/11/2015: Car Payment Due 1/13/2015: Groceries 1/15/2015: Mortgage Due 1/16/2015: Lazy Boy Layaway Payment Due 1/17/2015: Cell Phone Due, Breakfast Out 1/18/2015: Dinner Out 1/20/2015: Paycheck 1/21/2015: Gas 1/23/2015: Credit Card Due 1/24/2015: Groceries 1/25/2015: Gas 1/29/2015: Gas 2/2/2015: Car Insurance Due, Dinner Out 2/3/2015: Paycheck 2/5/2015: Dinner Out 2/6/2015: Cable Due, Dinner Out, Gas 2/7/2015: Breakfast Out 2/8/2015: Groceries 2/10/2015: Dinner Out 2/11/2015: Car Payment Due, Groceries 2/12/2015: Groceries, Dinner Out 2/14/2015: Breakfast Out 2/15/2015: Mortgage Due ...
Some of the lines in the input file probably deserve some explanation.
every 3 months start 3/26/2015 income Stock Dividend
every 14 days start 1/6/2015 income Paycheck
every 7 days start 1/3/2015 prob 0.5 expense Breakfast Out
every 1 months for 12 start 1/16/2015 expense Lazy Boy Layaway Payment Due