home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
lect07a
solidifying our knowledge
lect07a
We will review make sure we understand:
- The notion of state, for objects.
(How do they differ from local variables?)
- Reviewing the purpose of constructors,
along with the syntax for (a) writing and (b) calling constructors.
- Functions which take a class as an argument
- brainstorming alternate
approaches/possibilities for designing a Date class
(see below)
brainstorming Dates
Making examples of the data.
Other info a Date might have?
-
day-of-week (field, or method?)
-
isHoliday (field, or method?)
-
famousPeopleBornOn -- Why not?
-
Appointments -- how to represent? What info is involved w/ an appointment?
model vs. view
Let's look at a different language than Java -- fire up a spreadsheet (like Excel).
Spreadsheet programming is also programming (although it's not object-oriented).
- We can call a function to create a date: =date(2007,4,3).
-
Separately, we control how that date is displayed.
(Note how you can display it as year-month-day, or as month-day-year,
but the arguments to the function date(int, int, int)
can't be changed.)
-
How do you think the date is stored internally?
As an object with three fields?
Let's made a second cell which just refers to the contents of the first.
But this time we won't tell Excel to format the output in any particular way --
we'll change the formatting just to “general”.
-
Aha! The function date(int, int, int) returns an int!
More fairly “Excel uses a single int as its representation
of the date”.
No problem there; they use different toPrettyString methods
which take a date and display it differently.
-
Strengths and weaknesses:
-
Some operations become easy -- comparing if one date comes before another,
or computing n days after a given date.
-
Some operations become more difficult: each of that list of display functions;
getMonth, etc..
-
Some operations become impossible: isValid.
If a user enters Feb.30th, the computer can't catch their error;
the computer will blissfully compute away,
compounding a possible error at a billion operations per second.
-
Excel's approach violates the Zero'th
Law of Programming
(the one which underpins all the others):
The Zero'th Law of Programming:
The structure of your data and of your program should reflect
how you (as a human) conceive of the problem at hand.
(To be fair, Excel does do a good job of separating the
abstract data type (Date) from its representation (int).
Though if it did a perfect job, we'd have no way of figuring out their
internal representation.1)
This topic is discussed more in ITEC220 (CS2).
We'll also say that it separates the model (the date) from the view
(the many ways of formatting the Date).)
Note that there's even a view menu, which controls
how the spreadsheet (the model) is displayed.
Changing the view doesn't actually change the underlying spreadsheet at all.
-
Task (optional):
play around with Excels Time constructor2
and see if you can figure out Excel's representation of time-of-day.
In fact, Excel just has one way to
represent moments in time,
which is what it uses for both dates and for time-of-day
-- clever clever!
Perhaps their representation is not so bad after all -- it is
reflecting how physicists view time, and thus arguably is not
actually a lapse of the zero'th law.
1In fact, are we really certain
that Excel represents dates as an int? ↩
2Well,
spreadsheet programming -- that is, what we type in as a user to computer
our answers -- isn't object-oriented, and hence date(int,int,int)
isn't actually a “contructor”, but it is the function which creates
the internalized representation -- close enough. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs