STREAM STREAM A software process The Mañana Principle The Mañana Principle Don’t try to...
-
date post
21-Dec-2015 -
Category
Documents
-
view
219 -
download
2
Transcript of STREAM STREAM A software process The Mañana Principle The Mañana Principle Don’t try to...
STREAMSTREAMA software process
The Mañana PrincipleThe Mañana PrincipleDon’t try to everything at
once!
Static MethodsStatic MethodsStateless Utility Functions
S T R E A MS T R E A M
S S tubstubs
T T estsests
R R epresentationepresentation
E E valuationvaluation
A A ttributesttributes
M M ethodsethods
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Example
• Wanted: a class to represent a Wanted: a class to represent a DateDate..
• Must have the following two methods:Must have the following two methods:
- - setToNextDaysetToNextDay : no parameters: no parameters
- - toStringtoString : no parameters, returns String : no parameters, returns String withwith format format “<year>-<month>-“<year>-<month>-<day>”<day>”
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
1. Stubsclass Date {class Date {
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { } }
/** /** * @return A representation of this date * @return A representation of this date * in the format * in the format <year>-<month>-<day><year>-<month>-<day> */ */ public String toString () { public String toString () { return null; return null; } }
}}
class Date {class Date {
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { } }
/** /** * @return A representation of this date * @return A representation of this date * in the format * in the format <year>-<month>-<day><year>-<month>-<day> */ */ public String toString () { public String toString () { return null; return null; } }
}}
This will compile!
This will compile!
This will compile!
This will compile!
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
2. Tests
The The DateDate class can already be compiled. Using class can already be compiled. Using BlueJBlueJ, we can immediately create an instance and , we can immediately create an instance and start testing its methods. Of course, they won’t do start testing its methods. Of course, they won’t do much just yet!much just yet!
The The DateDate class can already be compiled. Using class can already be compiled. Using BlueJBlueJ, we can immediately create an instance and , we can immediately create an instance and start testing its methods. Of course, they won’t do start testing its methods. Of course, they won’t do much just yet!much just yet!
Make a list of some simple tests we would like to Make a list of some simple tests we would like to make.make.Make a list of some simple tests we would like to Make a list of some simple tests we would like to make.make.
To carry out those tests, what else will we need in To carry out those tests, what else will we need in the the DateDate class? class?To carry out those tests, what else will we need in To carry out those tests, what else will we need in the the DateDate class? class?
We’ll need a way We’ll need a way to set a particular dateto set a particular date – only then – only then can we tell if can we tell if setToNextDaysetToNextDay and and toStringtoString work. work.We’ll need a way We’ll need a way to set a particular dateto set a particular date – only then – only then can we tell if can we tell if setToNextDaysetToNextDay and and toStringtoString work. work.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
2. TestsSo, So, just by thinking about testingjust by thinking about testing, we see the need , we see the need for another method. This is cohesion in action!for another method. This is cohesion in action!So, So, just by thinking about testingjust by thinking about testing, we see the need , we see the need for another method. This is cohesion in action!for another method. This is cohesion in action!
class Date {class Date {
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) { (int day, int month, int year) { } }
... setToNextDay method (as before)... setToNextDay method (as before)
... toString method (as before) ... toString method (as before) }}
class Date {class Date {
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) { (int day, int month, int year) { } }
... setToNextDay method (as before)... setToNextDay method (as before)
... toString method (as before) ... toString method (as before) }}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
3. Representations
R1: R1: use three integer fieldsuse three integer fields
- day (1..31), month (1..12), year (1..)- day (1..31), month (1..12), year (1..)
R1: R1: use three integer fieldsuse three integer fields
- day (1..31), month (1..12), year (1..)- day (1..31), month (1..12), year (1..)
R2: R2: use one integer fielduse one integer field
- the number of days since 1- the number of days since 1stst. January, 1970. January, 1970
R2: R2: use one integer fielduse one integer field
- the number of days since 1- the number of days since 1stst. January, 1970. January, 1970
Many others are possible …Many others are possible …Many others are possible …Many others are possible …
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
3. EvaluateR1: R1: use three integer fieldsuse three integer fields
- day (1..31), month (1..12), year (1..)- day (1..31), month (1..12), year (1..)
R1: R1: use three integer fieldsuse three integer fields
- day (1..31), month (1..12), year (1..)- day (1..31), month (1..12), year (1..)
R2: R2: use one integer fielduse one integer field
- the number of days since 1- the number of days since 1stst. January, 1970. January, 1970
R2: R2: use one integer fielduse one integer field
- the number of days since 1- the number of days since 1stst. January, 1970. January, 1970
Implementation Implementation EffortEffort R1R1 R2R2
setToNextDaysetToNextDay
toString
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The TEACHTEACH Scale
difficultydifficulty
TrivialTrivialTrivialTrivial
EasyEasyEasyEasy
AverageAverageAverageAverage
ChallengingChallengingChallengingChallenging
HardHardHardHard
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
3. EvaluateR1: R1: use three integer fieldsuse three integer fields
- day (1..31), month (1..12), year (1..)- day (1..31), month (1..12), year (1..)
R1: R1: use three integer fieldsuse three integer fields
- day (1..31), month (1..12), year (1..)- day (1..31), month (1..12), year (1..)
R2: R2: use one integer fielduse one integer field
- the number of days since 1- the number of days since 1stst. January, 1970. January, 1970
R2: R2: use one integer fielduse one integer field
- the number of days since 1- the number of days since 1stst. January, 1970. January, 1970
Implementation Implementation EffortEffort R1R1 R2R2
setToNextDaysetToNextDay
toString
Challenging
HardTrivial
Trivial
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
5. Attributes (we call them fields)
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year ... setDate (as before)... setDate (as before)
... setToNextDay method (as before)... setToNextDay method (as before)
... toString method (as before) ... toString method (as before) }}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year ... setDate (as before)... setDate (as before)
... setToNextDay method (as before)... setToNextDay method (as before)
... toString method (as before) ... toString method (as before) }}
R1: R1: use three integer fieldsuse three integer fields
- day (1..31), month (1..12), year (1..)- day (1..31), month (1..12), year (1..)
R1: R1: use three integer fieldsuse three integer fields
- day (1..31), month (1..12), year (1..)- day (1..31), month (1..12), year (1..)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
5. Attributes (we call them fields)
class Date {class Date {
private int date; private int date; // days since 1970-01-01// days since 1970-01-01
... setDate (as before)... setDate (as before)
... setToNextDay method (as before)... setToNextDay method (as before)
... toString method (as before) ... toString method (as before) }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01// days since 1970-01-01
... setDate (as before)... setDate (as before)
... setToNextDay method (as before)... setToNextDay method (as before)
... toString method (as before) ... toString method (as before) }}
R2: R2: use one integer fielduse one integer field
- the number of days since 1- the number of days since 1stst. January, 1970. January, 1970
R2: R2: use one integer fielduse one integer field
- the number of days since 1- the number of days since 1stst. January, 1970. January, 1970
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
... setDate and setToNextDay methods... setDate and setToNextDay methods
/** /** * * @return A representation of this date@return A representation of this date * in the format * in the format <year>-<month>-<day><year>-<month>-<day> */ */ public String toString () { public String toString () { } } }}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
... setDate and setToNextDay methods... setDate and setToNextDay methods
/** /** * * @return A representation of this date@return A representation of this date * in the format * in the format <year>-<month>-<day><year>-<month>-<day> */ */ public String toString () { public String toString () { } } }}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 // 1 ≤≤ year year
... setDate and setToNextDay methods... setDate and setToNextDay methods
/** /** * * @return A representation of this date@return A representation of this date * in the format * in the format <year>-<month>-<day><year>-<month>-<day> */ */ public String toString () { public String toString () { return year + "-" + month + "-" + day;return year + "-" + month + "-" + day; } } }}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 // 1 ≤≤ year year
... setDate and setToNextDay methods... setDate and setToNextDay methods
/** /** * * @return A representation of this date@return A representation of this date * in the format * in the format <year>-<month>-<day><year>-<month>-<day> */ */ public String toString () { public String toString () { return year + "-" + month + "-" + day;return year + "-" + month + "-" + day; } } }}
6. Method ImplementationR1R1
Trivial
Trivial
Trivial
Trivial
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setDate and setToNextDay methods... setDate and setToNextDay methods
/** /** * * @return A representation of this date@return A representation of this date * in the format * in the format <year>-<month>-<day><year>-<month>-<day> */ */ public String toString () { public String toString () { } } }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setDate and setToNextDay methods... setDate and setToNextDay methods
/** /** * * @return A representation of this date@return A representation of this date * in the format * in the format <year>-<month>-<day><year>-<month>-<day> */ */ public String toString () { public String toString () { } } }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setDate and setToNextDay methods... setDate and setToNextDay methods
/** /** * * @return A representation of this date@return A representation of this date * in the format * in the format <year>-<month>-<day><year>-<month>-<day> */ */ public String toString () { public String toString () { ... tricky !!!... tricky !!! } } }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setDate and setToNextDay methods... setDate and setToNextDay methods
/** /** * * @return A representation of this date@return A representation of this date * in the format * in the format <year>-<month>-<day><year>-<month>-<day> */ */ public String toString () { public String toString () { ... tricky !!!... tricky !!! } } }}
6. Method ImplementationR2R2
HardHardHardHard
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { } } }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { } } }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { date = date + 1;date = date + 1; } } }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { date = date + 1;date = date + 1; } } }}
6. Method ImplementationR2R2
Trivial
Trivial
Trivial
Trivial
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { } } }}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { } } }}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { day = day + 1;day = day + 1; ... more stuff needed... more stuff needed } } }}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { day = day + 1;day = day + 1; ... more stuff needed... more stuff needed } } }}
6. Method ImplementationR1R1
Challenging
Challenging
Challenging
Challenging
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The Mañana Principle
When – during When – during implementation of a method implementation of a method – we wish we had a certain – we wish we had a certain support method,support method, write our write our
code as if we had itcode as if we had it. . Implement it later.Implement it later.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
When – during When – during implementation of a method implementation of a method – we wish we had a certain – we wish we had a certain support method,support method, write our write our
code as if we had itcode as if we had it. . Implement it later.Implement it later.
Special Case Rule:Special Case Rule:Often, we need to identify Often, we need to identify
and deal with a special case.and deal with a special case. Write the code for this in a Write the code for this in a
separate methodseparate method. . Implement it later.Implement it later.
The Mañana Principle
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { day = day + 1;day = day + 1; ... more stuff needed... more stuff needed } } }}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { day = day + 1;day = day + 1; ... more stuff needed... more stuff needed } } }}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { day = day + 1;day = day + 1; checkDayOverflow ()checkDayOverflow ();; } } }}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { day = day + 1;day = day + 1; checkDayOverflow ()checkDayOverflow ();; } } }}
6. Method ImplementationR1R1
Challenging
Challenging
Challenging
Challenging
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { day = day + 1; day = day + 1; checkDayOverflow ()checkDayOverflow ();; } }
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { day = day + 1; day = day + 1; checkDayOverflow ()checkDayOverflow ();; } }
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { day = day + 1; day = day + 1; checkDayOverflow ()checkDayOverflow ();; } }
/** /** * * Spot and deal with special day caseSpot and deal with special day case */ */ private void checkDayOverflow () { private void checkDayOverflow () { if (day > if (day > 3030) {) { day = 1; day = 1; month = month + 1; month = month + 1; } } } }
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { day = day + 1; day = day + 1; checkDayOverflow ()checkDayOverflow ();; } }
/** /** * * Spot and deal with special day caseSpot and deal with special day case */ */ private void checkDayOverflow () { private void checkDayOverflow () { if (day > if (day > 3030) {) { day = 1; day = 1; month = month + 1; month = month + 1; } } } }
6. Method ImplementationR1R1
Challenging
Challenging
Challenging
Challenging
Incomplete
Incomplete
Incomplete
Incomplete
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * * Spot and deal with special day caseSpot and deal with special day case */ */ private void checkDayOverflow () { private void checkDayOverflow () { if (day > if (day > 3030) {) { day = 1; day = 1; month = month + 1; month = month + 1; } } } }
/** /** * * Spot and deal with special day caseSpot and deal with special day case */ */ private void checkDayOverflow () { private void checkDayOverflow () { if (day > if (day > 3030) {) { day = 1; day = 1; month = month + 1; month = month + 1; } } } }
/** /** * * Spot and deal with special day caseSpot and deal with special day case */ */ private void checkDayOverflow () { private void checkDayOverflow () { if (day > if (day > 3030) {) { day = 1; day = 1; month = month + 1; month = month + 1; checkMonthOverflow ()checkMonthOverflow ();; } } } }
/** /** * * Spot and deal with special month caseSpot and deal with special month case */ */ private void checkMonthOverflow () { private void checkMonthOverflow () { if (month > 12) {if (month > 12) { month = 1; month = 1; year = year + 1; year = year + 1; } } } }
/** /** * * Spot and deal with special day caseSpot and deal with special day case */ */ private void checkDayOverflow () { private void checkDayOverflow () { if (day > if (day > 3030) {) { day = 1; day = 1; month = month + 1; month = month + 1; checkMonthOverflow ()checkMonthOverflow ();; } } } }
/** /** * * Spot and deal with special month caseSpot and deal with special month case */ */ private void checkMonthOverflow () { private void checkMonthOverflow () { if (month > 12) {if (month > 12) { month = 1; month = 1; year = year + 1; year = year + 1; } } } }
R1R1
Incomplete
Incomplete
Incomplete
Incomplete
Complete
Complete
Complete
Complete
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The Mañana Principle
Hard Problem Rule:Hard Problem Rule:when we when we need the answerneed the answer
to a problem we cannot to a problem we cannot immediately solve, immediately solve, defer it a defer it a
separate methodseparate method.. Implement it later.Implement it later.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * * Spot and deal with special day caseSpot and deal with special day case */ */ private void checkDayOverflow () { private void checkDayOverflow () { if (day > if (day > 3030) {) { day = 1; day = 1; month = month + 1; month = month + 1; checkMonthOverflow ()checkMonthOverflow ();; } } } }
/** /** * * Spot and deal with special day caseSpot and deal with special day case */ */ private void checkDayOverflow () { private void checkDayOverflow () { if (day > if (day > 3030) {) { day = 1; day = 1; month = month + 1; month = month + 1; checkMonthOverflow ()checkMonthOverflow ();; } } } }
/** /** * * Spot and deal with special day caseSpot and deal with special day case */ */ private void checkDayOverflow () { private void checkDayOverflow () { if (day > if (day > daysInMonth ()daysInMonth ()) {) { day = 1; day = 1; month = month + 1; month = month + 1; checkMonthOverflow (); checkMonthOverflow (); } } } }
/** /** * * Spot and deal with special day caseSpot and deal with special day case */ */ private void checkDayOverflow () { private void checkDayOverflow () { if (day > if (day > daysInMonth ()daysInMonth ()) {) { day = 1; day = 1; month = month + 1; month = month + 1; checkMonthOverflow (); checkMonthOverflow (); } } } }
/** /** * * Spot and deal with special day caseSpot and deal with special day case */ */ private void checkDayOverflow () { private void checkDayOverflow () { if (day > if (day > daysInMonth ()daysInMonth ()) {) { day = 1; day = 1; month = month + 1; month = month + 1; checkMonthOverflow (); checkMonthOverflow (); } } } }
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { return return 3030;; } }
/** /** * * Spot and deal with special day caseSpot and deal with special day case */ */ private void checkDayOverflow () { private void checkDayOverflow () { if (day > if (day > daysInMonth ()daysInMonth ()) {) { day = 1; day = 1; month = month + 1; month = month + 1; checkMonthOverflow (); checkMonthOverflow (); } } } }
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { return return 3030;; } }
R1R1
Incomplete
Incomplete
Incomplete
Incomplete
Complete
Complete
Complete
Complete
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { return return 3030;; } } }}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { return return 3030;; } } }}
class Date {class Date { private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31}; ... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { return return 3030;; } }
}}
class Date {class Date { private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31}; ... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { return return 3030;; } }
}}
class Date {class Date { private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { return return monthDays[month – 1]monthDays[month – 1];; } } }}
class Date {class Date { private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { return return monthDays[month – 1]monthDays[month – 1];; } } }}
Incomplete
Incomplete
Incomplete
Incomplete
R1R1
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date { ... day, month, year fields... day, month, year fields
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { return return monthDays[month – 1]monthDays[month – 1];; } }
}}
class Date {class Date { ... day, month, year fields... day, month, year fields
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { return return monthDays[month – 1]monthDays[month – 1];; } }
}}
class Date {class Date { ... day, month, year fields... day, month, year fields
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { int int answeranswer = = monthDays[month – 1]monthDays[month – 1];; if (( if ((monthmonth == 2) && == 2) && isLeapYear ()isLeapYear ()) {) { answeranswer = = answeranswer + 1; + 1; } } return return answeranswer;; } } }}
class Date {class Date { ... day, month, year fields... day, month, year fields
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { int int answeranswer = = monthDays[month – 1]monthDays[month – 1];; if (( if ((monthmonth == 2) && == 2) && isLeapYear ()isLeapYear ()) {) { answeranswer = = answeranswer + 1; + 1; } } return return answeranswer;; } } }}
R1R1C
ompl
ete
Com
plet
e
Com
plet
e
Com
plet
e
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The Mañana Principle
Hard Problem Rule:Hard Problem Rule:when we when we need the answerneed the answer
to a problem we cannot to a problem we cannot immediately solve, immediately solve, defer it a defer it a
separate methodseparate method.. Implement it later.Implement it later.
Heavy Functionality Rule:Heavy Functionality Rule:when when some statements orsome statements orexpressions become too expressions become too
long or complex, long or complex, move them move them into a separate methodinto a separate method..
Implement it later.Implement it later.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { int int answeranswer = = monthDays[month – 1]monthDays[month – 1];; if (( if ((monthmonth == 2) && == 2) && isLeapYear ()isLeapYear ()) {) { answeranswer = = answeranswer + 1; + 1; } } return return answeranswer;; } }
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { int int answeranswer = = monthDays[month – 1]monthDays[month – 1];; if (( if ((monthmonth == 2) && == 2) && isLeapYear ()isLeapYear ()) {) { answeranswer = = answeranswer + 1; + 1; } } return return answeranswer;; } }
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { int int answeranswer = = monthDays[month – 1]monthDays[month – 1];; if (( if ((monthmonth == 2) && == 2) && isLeapYear ()isLeapYear ()) {) { answeranswer = = answeranswer + 1; + 1; } } return return answeranswer;; } }
/** /** * * @return Whether the year is a leap year@return Whether the year is a leap year */ */ private b private boolean isLeapYear () {oolean isLeapYear () { returnreturn ((divides divides (4, year) && !(4, year) && !divides divides (100, year))(100, year)) || || divides divides (400, year)(400, year);; } }
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { int int answeranswer = = monthDays[month – 1]monthDays[month – 1];; if (( if ((monthmonth == 2) && == 2) && isLeapYear ()isLeapYear ()) {) { answeranswer = = answeranswer + 1; + 1; } } return return answeranswer;; } }
/** /** * * @return Whether the year is a leap year@return Whether the year is a leap year */ */ private b private boolean isLeapYear () {oolean isLeapYear () { returnreturn ((divides divides (4, year) && !(4, year) && !divides divides (100, year))(100, year)) || || divides divides (400, year)(400, year);; } }
R1R1C
ompl
ete
Com
plet
e
Com
plet
e
Com
plet
e
Com
plet
e
Com
plet
e
Com
plet
e
Com
plet
e
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * * @return Whether the year is a leap year@return Whether the year is a leap year */ */ private b private boolean isLeapYear () {oolean isLeapYear () { returnreturn ((divides divides (4, year) && !(4, year) && !divides divides (100, year))(100, year)) || || divides divides (400, year)(400, year);; } }
/** /** * * @return Whether the year is a leap year@return Whether the year is a leap year */ */ private b private boolean isLeapYear () {oolean isLeapYear () { returnreturn ((divides divides (4, year) && !(4, year) && !divides divides (100, year))(100, year)) || || divides divides (400, year)(400, year);; } }
/** /** * * @return Whether the year is a leap year@return Whether the year is a leap year */ */ private b private boolean isLeapYear () {oolean isLeapYear () { returnreturn ((divides divides (4, year) && !(4, year) && !divides divides (100, year))(100, year)) || || divides divides (400, year)(400, year);; } }
/**/** * @param d The divisor * @param d The divisor * @param n The number being divided * @param n The number being divided * * @return Whether d divides n exactly@return Whether d divides n exactly */ */ private private boolean divides (int d, int n) {boolean divides (int d, int n) { return (n % d) == 0;return (n % d) == 0; } }
/** /** * * @return Whether the year is a leap year@return Whether the year is a leap year */ */ private b private boolean isLeapYear () {oolean isLeapYear () { returnreturn ((divides divides (4, year) && !(4, year) && !divides divides (100, year))(100, year)) || || divides divides (400, year)(400, year);; } }
/**/** * @param d The divisor * @param d The divisor * @param n The number being divided * @param n The number being divided * * @return Whether d divides n exactly@return Whether d divides n exactly */ */ private private boolean divides (int d, int n) {boolean divides (int d, int n) { return (n % d) == 0;return (n % d) == 0; } }
R1R1C
ompl
ete
Com
plet
e
Com
plet
e
Com
plet
e
Com
plet
e
Com
plet
e
Com
plet
e
Com
plet
e
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
2. TestsSo, So, just by thinking about testingjust by thinking about testing, we see the need , we see the need for another method. This is cohesion in action!for another method. This is cohesion in action!So, So, just by thinking about testingjust by thinking about testing, we see the need , we see the need for another method. This is cohesion in action!for another method. This is cohesion in action!
class Date {class Date {
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) { (int day, int month, int year) { } }
... setToNextDay method (as before)... setToNextDay method (as before)
... toString method (as before) ... toString method (as before) }}
class Date {class Date {
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) { (int day, int month, int year) { } }
... setToNextDay method (as before)... setToNextDay method (as before)
... toString method (as before) ... toString method (as before) }}
Recall
Recall
Recall
Recall
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { {
} }
... setToNextDay and toString methods... setToNextDay and toString methods}}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { {
} }
... setToNextDay and toString methods... setToNextDay and toString methods}}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 // 1 ≤≤ year year
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; } }
... setToNextDay and toString methods ... setToNextDay and toString methods }}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 // 1 ≤≤ year year
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; } }
... setToNextDay and toString methods ... setToNextDay and toString methods }}
R1R1
But what if
bad
But what if
bad
values a
re given?
values a
re given?
But what if
bad
But what if
bad
values a
re given?
values a
re given?
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year;
} }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year;
} }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; if (! if (!legalDate ()legalDate ()) {) {
}} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; if (! if (!legalDate ()legalDate ()) {) {
}} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; if (! if (!legalDate ()legalDate ()) {) { this.day = 1;this.day = 1; this.month = 1; this.month = 1; this.year = 1; this.year = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 0001-01-01"); " 0001-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; if (! if (!legalDate ()legalDate ()) {) { this.day = 1;this.day = 1; this.month = 1; this.month = 1; this.year = 1; this.year = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 0001-01-01"); " 0001-01-01"); }} } }
defer defer decision as decision as to what is to what is
legal !!legal !!
Com
plet
e
Com
plet
e
Com
plet
e
Com
plet
eE
asy
Eas
yE
asy
Eas
yR1R1
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/**/** * * @return Whether the date fields are legal@return Whether the date fields are legal */ */ private private boolean legalDate () {boolean legalDate () { return ((1 <= day) &&return ((1 <= day) && (day <= (day <= 3030)) &&)) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && (1 <= year); (1 <= year); } }
/**/** * * @return Whether the date fields are legal@return Whether the date fields are legal */ */ private private boolean legalDate () {boolean legalDate () { return ((1 <= day) &&return ((1 <= day) && (day <= (day <= 3030)) &&)) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && (1 <= year); (1 <= year); } }
/**/** * * @return Whether the date fields are legal@return Whether the date fields are legal */ */ private private boolean legalDate () {boolean legalDate () { return ((1 <= day) &&return ((1 <= day) && (day <= (day <= daysInMonth ()daysInMonth ())) &&)) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && (1 <= year); (1 <= year); } }
/**/** * * @return Whether the date fields are legal@return Whether the date fields are legal */ */ private private boolean legalDate () {boolean legalDate () { return ((1 <= day) &&return ((1 <= day) && (day <= (day <= daysInMonth ()daysInMonth ())) &&)) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && (1 <= year); (1 <= year); } }
BUG:BUG: ifif monthmonth is not in the range 1..12,is not in the range 1..12, daysInMonthdaysInMonth()() will crash.will crash.
BUG:BUG: ifif monthmonth is not in the range 1..12,is not in the range 1..12, daysInMonthdaysInMonth()() will crash.will crash.
R1R1
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date { ... day, month, year fields... day, month, year fields
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { return return monthDays[month – 1]monthDays[month – 1];; } }
}}
class Date {class Date { ... day, month, year fields... day, month, year fields
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { return return monthDays[month – 1]monthDays[month – 1];; } }
}}
class Date {class Date { ... day, month, year fields... day, month, year fields
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { int int answeranswer = = monthDays[month – 1]monthDays[month – 1];; if (( if ((monthmonth == 2) && == 2) && isLeapYear ()isLeapYear ()) {) { answeranswer = = answeranswer + 1; + 1; } } return return answeranswer;; } } }}
class Date {class Date { ... day, month, year fields... day, month, year fields
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { int int answeranswer = = monthDays[month – 1]monthDays[month – 1];; if (( if ((monthmonth == 2) && == 2) && isLeapYear ()isLeapYear ()) {) { answeranswer = = answeranswer + 1; + 1; } } return return answeranswer;; } } }}
Rec
all …
Rec
all …
Rec
all …
Rec
all …
Assumes:Assumes: monthmonth is is inin
the range 1..12.the range 1..12.
Assumes:Assumes: monthmonth is is inin
the range 1..12.the range 1..12.
R1R1
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/**/** * * @return Whether the date fields are legal@return Whether the date fields are legal */ */ private private boolean legalDate () {boolean legalDate () { return ((1 <= day) &&return ((1 <= day) && (day <= (day <= daysInMonth ()daysInMonth ())) &&)) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && (1 <= year); (1 <= year); } }
/**/** * * @return Whether the date fields are legal@return Whether the date fields are legal */ */ private private boolean legalDate () {boolean legalDate () { return ((1 <= day) &&return ((1 <= day) && (day <= (day <= daysInMonth ()daysInMonth ())) &&)) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && (1 <= year); (1 <= year); } }
R1R1
/**/** * * @return Whether the date fields are legal@return Whether the date fields are legal */ */ private private boolean legalDate () {boolean legalDate () { return (1 <= year) &&return (1 <= year) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && ((1 <= day) && ((1 <= day) && (day <= (day <= daysInMonth ()daysInMonth ()));)); } }
/**/** * * @return Whether the date fields are legal@return Whether the date fields are legal */ */ private private boolean legalDate () {boolean legalDate () { return (1 <= year) &&return (1 <= year) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && ((1 <= day) && ((1 <= day) && (day <= (day <= daysInMonth ()daysInMonth ()));)); } }
BUG:BUG: ifif monthmonth is not in the range 1..12,is not in the range 1..12, daysInMonthdaysInMonth()() will crash.will crash.
BUG:BUG: ifif monthmonth is not in the range 1..12,is not in the range 1..12, daysInMonthdaysInMonth()() will crash.will crash.
FIXED:FIXED: operands ofoperands of &&&& are evaluated are evaluated left-to-right. If any operand isleft-to-right. If any operand is falsefalse, ,
the result isthe result is falsefalse and remaining and remaining operands (if any) are not evaluatedoperands (if any) are not evaluated..
FIXED:FIXED: operands ofoperands of &&&& are evaluated are evaluated left-to-right. If any operand isleft-to-right. If any operand is falsefalse, ,
the result isthe result is falsefalse and remaining and remaining operands (if any) are not evaluatedoperands (if any) are not evaluated..
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
S T R E A M
• S S tubstubs• T T estsests• R R epresentationepresentation• E E valuationvaluation• A A ttributesttributes• M M ethodsethods
Our choice of Our choice of representation representation can have great can have great impact on the impact on the complexity of complexity of
the logic.the logic.
Our choice of Our choice of representation representation can have great can have great impact on the impact on the complexity of complexity of
the logic.the logic.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
5. Attributes (we call them fields)
class Date {class Date {
private int date; private int date; // days since 1970-01-01// days since 1970-01-01
... setDate (as before)... setDate (as before)
... setToNextDay method (as before)... setToNextDay method (as before)
... toString method (as before) ... toString method (as before) }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01// days since 1970-01-01
... setDate (as before)... setDate (as before)
... setToNextDay method (as before)... setToNextDay method (as before)
... toString method (as before) ... toString method (as before) }}
R2: R2: use one integer fielduse one integer field
- the number of days since 1- the number of days since 1stst. January, 1970. January, 1970
R2: R2: use one integer fielduse one integer field
- the number of days since 1- the number of days since 1stst. January, 1970. January, 1970
See
n be
fore
See
n be
fore
See
n be
fore
See
n be
fore
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setDate and setToNextDay methods... setDate and setToNextDay methods
/** /** * * @return A representation of this date@return A representation of this date * in the format * in the format <year>-<month>-<day><year>-<month>-<day> */ */ public String toString () { public String toString () { ... tricky !!!... tricky !!! } } }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setDate and setToNextDay methods... setDate and setToNextDay methods
/** /** * * @return A representation of this date@return A representation of this date * in the format * in the format <year>-<month>-<day><year>-<month>-<day> */ */ public String toString () { public String toString () { ... tricky !!!... tricky !!! } } }}
6. Method ImplementationR2R2
HardHardHardHard
See
n be
fore
See
n be
fore
See
n be
fore
See
n be
fore
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { date = date + 1;date = date + 1; } } }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setDate and toString methods... setDate and toString methods
/** /** * Advance the date to the next day * Advance the date to the next day */ */ public void setToNextDay () { public void setToNextDay () { date = date + 1;date = date + 1; } } }}
6. Method ImplementationR2R2
Trivial
Trivial
Trivial
Trivial
See
n be
fore
See
n be
fore
See
n be
fore
See
n be
fore
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setToNextDay and toString methods ... setToNextDay and toString methods
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { {
} } }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setToNextDay and toString methods ... setToNextDay and toString methods
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { {
} } }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setToNextDay and toString methods ... setToNextDay and toString methods
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { ... tricky !!!... tricky !!! } } }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01 // days since 1970-01-01
... setToNextDay and toString methods ... setToNextDay and toString methods
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { ... tricky !!!... tricky !!! } } }}
6. Method ImplementationR2R2
Hard
Hard
Hard
Hard
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; if (! if (!legalDate ()legalDate ()) {) { this.day = 1;this.day = 1; this.month = 1; this.month = 1; this.year = 1; this.year = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 0001-01-01"); " 0001-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; if (! if (!legalDate ()legalDate ()) {) { this.day = 1;this.day = 1; this.month = 1; this.month = 1; this.year = 1; this.year = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 0001-01-01"); " 0001-01-01"); }} } }
R1R1S
een
befo
re
See
n be
fore
See
n be
fore
See
n be
fore
But theBut the R2R2 DateDate has has nono dayday,, monthmonth oror yearyear
fields to set …fields to set …
But theBut the R2R2 DateDate has has nono dayday,, monthmonth oror yearyear
fields to set …fields to set …
… … andand legalDatelegalDate has has no fields on which itno fields on which it
to operateto operate. .
… … andand legalDatelegalDate has has no fields on which itno fields on which it
to operateto operate. .
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { {
} }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { {
} }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (legalDate (day, month, year)legalDate (day, month, year)) {) { ... still tricky !!!... still tricky !!! } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 1970-01-01"); " 1970-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (legalDate (day, month, year)legalDate (day, month, year)) {) { ... still tricky !!!... still tricky !!! } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 1970-01-01"); " 1970-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (legalDate (day, month, year)legalDate (day, month, year)) {) { setLegalDate (day, month, year)setLegalDate (day, month, year) } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 1970-01-01"); " 1970-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (legalDate (day, month, year)legalDate (day, month, year)) {) { setLegalDate (day, month, year)setLegalDate (day, month, year) } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 1970-01-01"); " 1970-01-01"); }} } } defer defer
decision as decision as to what is to what is
legal !!legal !!
defer hard defer hard logic !!logic !!C
ompl
ete
Com
plet
e
Com
plet
e
Com
plet
e
R2R2
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * Set the date to a particular day * Set the date to a particular day * * * @param day (assume: * @param day (assume: 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month (assume: * @param month (assume: 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year (assume: 1 ≤ year) * @param year (assume: 1 ≤ year) * * */ */ private void setLegalDate private void setLegalDate (int day, int month, int year) (int day, int month, int year) { { } }
/** /** * Set the date to a particular day * Set the date to a particular day * * * @param day (assume: * @param day (assume: 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month (assume: * @param month (assume: 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year (assume: 1 ≤ year) * @param year (assume: 1 ≤ year) * * */ */ private void setLegalDate private void setLegalDate (int day, int month, int year) (int day, int month, int year) { { } }
/** /** * Set the date to a particular day * Set the date to a particular day * * * @param day (assume: * @param day (assume: 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month (assume: * @param month (assume: 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year (assume: 1 ≤ year) * @param year (assume: 1 ≤ year) * * */ */ private void setLegalDate private void setLegalDate (int day, int month, int year) (int day, int month, int year) { { ... still tricky !!!... still tricky !!! } }
/** /** * Set the date to a particular day * Set the date to a particular day * * * @param day (assume: * @param day (assume: 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month (assume: * @param month (assume: 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year (assume: 1 ≤ year) * @param year (assume: 1 ≤ year) * * */ */ private void setLegalDate private void setLegalDate (int day, int month, int year) (int day, int month, int year) { { ... still tricky !!!... still tricky !!! } } Har
dHar
dHar
dHar
d
R2R2
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { {
} }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { {
} }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (legalDate (day, month, year)legalDate (day, month, year)) {) { ... still tricky !!!... still tricky !!! } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to " + " ... setting date to " + " 1970-01-01"); " 1970-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (legalDate (day, month, year)legalDate (day, month, year)) {) { ... still tricky !!!... still tricky !!! } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to " + " ... setting date to " + " 1970-01-01"); " 1970-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (legalDate (day, month, year)legalDate (day, month, year)) {) { setLegalDate (day, month, year)setLegalDate (day, month, year) } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 1970-01-01"); " 1970-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (legalDate (day, month, year)legalDate (day, month, year)) {) { setLegalDate (day, month, year)setLegalDate (day, month, year) } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 1970-01-01"); " 1970-01-01"); }} } } defer defer
decision as decision as to what is to what is
legal !!legal !!
defer hard defer hard logic !!logic !!C
ompl
ete
Com
plet
e
Com
plet
e
Com
plet
e
R2R2
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/**/** * @param day ( * @param day (legallegal: : 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month ( * @param month (legallegal: : 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year ( * @param year (legallegal: 1 ≤ year): 1 ≤ year) * * * * @return Whether the parameters are legal@return Whether the parameters are legal * * */ */ private private boolean legalDateboolean legalDate (int day, int month, int year) (int day, int month, int year) { {
} }
/**/** * @param day ( * @param day (legallegal: : 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month ( * @param month (legallegal: : 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year ( * @param year (legallegal: 1 ≤ year): 1 ≤ year) * * * * @return Whether the parameters are legal@return Whether the parameters are legal * * */ */ private private boolean legalDateboolean legalDate (int day, int month, int year) (int day, int month, int year) { {
} }
/**/** * @param day ( * @param day (legallegal: : 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month ( * @param month (legallegal: : 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year ( * @param year (legallegal: 1 ≤ year): 1 ≤ year) * * * * @return Whether the parameters are legal@return Whether the parameters are legal * * */ */ private private boolean legalDateboolean legalDate (int day, int month, int year) (int day, int month, int year) { { return (1 <= year) &&return (1 <= year) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && ((1 <= day) && ((1 <= day) && (day <= (day <= daysInMonth ()daysInMonth ()));)); } }
/**/** * @param day ( * @param day (legallegal: : 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month ( * @param month (legallegal: : 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year ( * @param year (legallegal: 1 ≤ year): 1 ≤ year) * * * * @return Whether the parameters are legal@return Whether the parameters are legal * * */ */ private private boolean legalDateboolean legalDate (int day, int month, int year) (int day, int month, int year) { { return (1 <= year) &&return (1 <= year) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && ((1 <= day) && ((1 <= day) && (day <= (day <= daysInMonth ()daysInMonth ()));)); } }
… … but but daysInMonthdaysInMonth has has no no monthmonth or or yearyear fields fields
on which to operate. on which to operate.
… … but but daysInMonthdaysInMonth has has no no monthmonth or or yearyear fields fields
on which to operate. on which to operate.
R2R2
XX
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/**/** * @param day ( * @param day (legallegal: : 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month ( * @param month (legallegal: : 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year ( * @param year (legallegal: 1 ≤ year): 1 ≤ year) * * * * @return Whether the parameters are legal@return Whether the parameters are legal * * */ */ private private boolean legalDateboolean legalDate (int day, int month, int year) (int day, int month, int year) { { return (1 <= year) &&return (1 <= year) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && ((1 <= day) && ((1 <= day) && (day <= (day <= daysInMonth ()daysInMonth ()));)); } }
/**/** * @param day ( * @param day (legallegal: : 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month ( * @param month (legallegal: : 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year ( * @param year (legallegal: 1 ≤ year): 1 ≤ year) * * * * @return Whether the parameters are legal@return Whether the parameters are legal * * */ */ private private boolean legalDateboolean legalDate (int day, int month, int year) (int day, int month, int year) { { return (1 <= year) &&return (1 <= year) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && ((1 <= day) && ((1 <= day) && (day <= (day <= daysInMonth ()daysInMonth ()));)); } }
/**/** * @param day ( * @param day (legallegal: : 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month ( * @param month (legallegal: : 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year ( * @param year (legallegal: 1 ≤ year): 1 ≤ year) * * * * @return Whether the parameters are legal@return Whether the parameters are legal * * */ */ private private boolean legalDateboolean legalDate (int day, int month, int year) (int day, int month, int year) { { return (1 <= year) &&return (1 <= year) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && ((1 <= day) && ((1 <= day) && (day <= (day <= daysInMonth (month, year)daysInMonth (month, year)));));
} }
/**/** * @param day ( * @param day (legallegal: : 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month ( * @param month (legallegal: : 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year ( * @param year (legallegal: 1 ≤ year): 1 ≤ year) * * * * @return Whether the parameters are legal@return Whether the parameters are legal * * */ */ private private boolean legalDateboolean legalDate (int day, int month, int year) (int day, int month, int year) { { return (1 <= year) &&return (1 <= year) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && ((1 <= day) && ((1 <= day) && (day <= (day <= daysInMonth (month, year)daysInMonth (month, year)));));
} }
… … so we must so we must supply them.supply them.
… … so we must so we must supply them.supply them.
R2R2
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date { ... day, month, year fields... day, month, year fields
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { int int answeranswer = = monthDays[month – 1]monthDays[month – 1];; if (( if ((monthmonth == 2) && == 2) && isLeapYear ()isLeapYear ()) {) { answeranswer = = answeranswer + 1; + 1; } } return return answeranswer;; } } }}
class Date {class Date { ... day, month, year fields... day, month, year fields
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
... other methods... other methods
/** /** * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth () { private int daysInMonth () { int int answeranswer = = monthDays[month – 1]monthDays[month – 1];; if (( if ((monthmonth == 2) && == 2) && isLeapYear ()isLeapYear ()) {) { answeranswer = = answeranswer + 1; + 1; } } return return answeranswer;; } } }}
Assumes:Assumes: monthmonth is is inin
the range 1..12.the range 1..12.
Assumes:Assumes: monthmonth is is inin
the range 1..12.the range 1..12.
R1R1S
een
befo
re
See
n be
fore
See
n be
fore
See
n be
fore
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
no no yearyear field … so field … so we must supply.we must supply.
no no yearyear field … so field … so we must supply.we must supply.
class Date {class Date {
private int date; private int date; // days since 1970-01-01// days since 1970-01-01
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, 31, ... , 31}; {31, 28, 31, 30, 31, 30, 31, ... , 31};
/**/** * @param month ( * @param month (assumeassume: : 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year ( * @param year (assumeassume: : 1 ≤ year1 ≤ year) ) * * @return The number of days in the month@return The number of days in the month */ */ private int daysInMonth (int month, int year) private int daysInMonth (int month, int year) { { int int answeranswer = = monthDays[month – 1]monthDays[month – 1];; if (( if ((monthmonth == 2) && == 2) && isLeapYear (year)isLeapYear (year)) {) { answeranswer = = answeranswer + 1; + 1; } } return return answeranswer;; } } }}
Com
plet
e
Com
plet
e
Com
plet
e
Com
plet
e
R2R2
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * * @return Whether the year is a leap year@return Whether the year is a leap year */ */ private b private boolean isLeapYear () {oolean isLeapYear () { returnreturn ((divides divides (4, year) && !(4, year) && !divides divides (100, year))(100, year)) || || divides divides (400, year)(400, year);; } }
/** /** * * @return Whether the year is a leap year@return Whether the year is a leap year */ */ private b private boolean isLeapYear () {oolean isLeapYear () { returnreturn ((divides divides (4, year) && !(4, year) && !divides divides (100, year))(100, year)) || || divides divides (400, year)(400, year);; } }
R1R1S
een
befo
re
See
n be
fore
See
n be
fore
See
n be
fore
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/**/** * @param year ( * @param year (assumeassume: : 1 ≤ year1 ≤ year) ) * * @return Whether the year is a leap year@return Whether the year is a leap year */ */ private b private boolean isLeapYear (oolean isLeapYear (int yearint year) {) { returnreturn ((divides divides (4, year) && !(4, year) && !divides divides (100, year))(100, year)) || || divides divides (400, year)(400, year);; } }
/**/** * @param year ( * @param year (assumeassume: : 1 ≤ year1 ≤ year) ) * * @return Whether the year is a leap year@return Whether the year is a leap year */ */ private b private boolean isLeapYear (oolean isLeapYear (int yearint year) {) { returnreturn ((divides divides (4, year) && !(4, year) && !divides divides (100, year))(100, year)) || || divides divides (400, year)(400, year);; } }
Com
plet
e
Com
plet
e
Com
plet
e
Com
plet
e
R2R2
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year ... setDate, setToNextDay, toString methods... setDate, setToNextDay, toString methods
private int daysInMonth ()private int daysInMonth () { ... } { ... }
private boolean private boolean isLeapYear isLeapYear ()() { ... } { ... }
private boolean private boolean legalDate legalDate ()() { ... } { ... } }}
class Date {class Date {
private int day; private int day; // 1 ≤ day ≤ daysInMonth// 1 ≤ day ≤ daysInMonth private int month; private int month; // 1 ≤ month ≤ 12// 1 ≤ month ≤ 12 private int year; private int year; // 1 ≤ year// 1 ≤ year ... setDate, setToNextDay, toString methods... setDate, setToNextDay, toString methods
private int daysInMonth ()private int daysInMonth () { ... } { ... }
private boolean private boolean isLeapYear isLeapYear ()() { ... } { ... }
private boolean private boolean legalDate legalDate ()() { ... } { ... } }}
R1R1
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class Date {class Date {
private int date; private int date; // days since 1970-01-01// days since 1970-01-01
... setDate, setToNextDay, toString methods... setDate, setToNextDay, toString methods
private int daysInMonth (int month, int year)private int daysInMonth (int month, int year) { ... } { ... }
private boolean private boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
private boolean private boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... } }}
class Date {class Date {
private int date; private int date; // days since 1970-01-01// days since 1970-01-01
... setDate, setToNextDay, toString methods... setDate, setToNextDay, toString methods
private int daysInMonth (int month, int year)private int daysInMonth (int month, int year) { ... } { ... }
private boolean private boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
private boolean private boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... } }}
R2R2
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
OBSERVATION:OBSERVATION:
R2R2
We could have used the We could have used the fully parametrisedfully parametrised versions: versions:
int daysInMonth (int month, int year)int daysInMonth (int month, int year) boolean boolean isLeapYear isLeapYear (int year)(int year) boolean boolean legalDate legalDate (int day, int month, int year)(int day, int month, int year)
in the version of in the version of DateDate, as well as in version ., as well as in version .R1R1
They do not depend on any chosen They do not depend on any chosen fieldfield representation. representation.
They may, therefore, be worth They may, therefore, be worth factoring outfactoring out into a separate into a separate class, class, DateStuffDateStuff, , which either representation forwhich either representation for DateDate could usecould use..
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class DateStuff {class DateStuff {
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
public int daysInMonth (int month, int year)public int daysInMonth (int month, int year) { ... } { ... }
public boolean public boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
public boolean public boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... }
private private boolean divides (int d, int n)boolean divides (int d, int n) { { ... } ... }
}}
class DateStuff {class DateStuff {
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
public int daysInMonth (int month, int year)public int daysInMonth (int month, int year) { ... } { ... }
public boolean public boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
public boolean public boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... }
private private boolean divides (int d, int n)boolean divides (int d, int n) { { ... } ... }
}}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
But, now that they are But, now that they are publicpublic methods, methods, daysInMonth daysInMonth and and isLeapYearisLeapYear must be protected against misuse. must be protected against misuse.
Note: Note: legalDatelegalDate is already secure – dealing with bad data is, is already secure – dealing with bad data is, after all, its purpose!after all, its purpose!
public int daysInMonth (int month, int year)public int daysInMonth (int month, int year) { ... } { ... }
public boolean public boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
public boolean public boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... }
public int daysInMonth (int month, int year)public int daysInMonth (int month, int year) { ... } { ... }
public boolean public boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
public boolean public boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... }
DateStuffDateStuff
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/**/** * @param month ( * @param month (1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year ( * @param year (1 ≤ year1 ≤ year) ) * * @return The number of days in the month@return The number of days in the month */ */public int daysInMonth (int month, int year)public int daysInMonth (int month, int year){{ if ((month < 1) || (month > 12) || (year < 1)) { if ((month < 1) || (month > 12) || (year < 1)) { System.out.printlnSystem.out.println ("*** daysInMonth: bad argument(s)" + ("*** daysInMonth: bad argument(s)" + " – returning zero"); " – returning zero"); return 0; return 0; }} int int answeranswer = = monthDays[month – 1]monthDays[month – 1];; if (( if ((monthmonth == 2) && == 2) && isLeapYear (year)isLeapYear (year)) {) { answeranswer = = answeranswer + 1; + 1; } } return return answeranswer;;}}
/**/** * @param month ( * @param month (1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year ( * @param year (1 ≤ year1 ≤ year) ) * * @return The number of days in the month@return The number of days in the month */ */public int daysInMonth (int month, int year)public int daysInMonth (int month, int year){{ if ((month < 1) || (month > 12) || (year < 1)) { if ((month < 1) || (month > 12) || (year < 1)) { System.out.printlnSystem.out.println ("*** daysInMonth: bad argument(s)" + ("*** daysInMonth: bad argument(s)" + " – returning zero"); " – returning zero"); return 0; return 0; }} int int answeranswer = = monthDays[month – 1]monthDays[month – 1];; if (( if ((monthmonth == 2) && == 2) && isLeapYear (year)isLeapYear (year)) {) { answeranswer = = answeranswer + 1; + 1; } } return return answeranswer;;}}
DateStuffDateStuff
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/**/** * @param year ( * @param year (1 ≤ year1 ≤ year) ) * * @return Whether year is a leap year @return Whether year is a leap year */ */public boolean isLeapYear (int year)public boolean isLeapYear (int year){{ if (year < 1){ if (year < 1){ System.out.printlnSystem.out.println ("*** isLeapYear: bad argument " + ("*** isLeapYear: bad argument " + "– returning false"); "– returning false"); return false; return false; }} returnreturn ((divides divides (4, year) && !(4, year) && !divides divides (100, year))(100, year)) || || divides divides (400, year)(400, year);; } }}}
/**/** * @param year ( * @param year (1 ≤ year1 ≤ year) ) * * @return Whether year is a leap year @return Whether year is a leap year */ */public boolean isLeapYear (int year)public boolean isLeapYear (int year){{ if (year < 1){ if (year < 1){ System.out.printlnSystem.out.println ("*** isLeapYear: bad argument " + ("*** isLeapYear: bad argument " + "– returning false"); "– returning false"); return false; return false; }} returnreturn ((divides divides (4, year) && !(4, year) && !divides divides (100, year))(100, year)) || || divides divides (400, year)(400, year);; } }}}
DateStuffDateStuff
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/**/** * @param day ( * @param day (legallegal: : 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month ( * @param month (legallegal: : 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year ( * @param year (legallegal: 1 ≤ year): 1 ≤ year) * * * * @return Whether the parameters are legal@return Whether the parameters are legal * * */ */ public public boolean legalDateboolean legalDate (int day, int month, int year)(int day, int month, int year) { { return return (1 <= year) &&(1 <= year) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && ((1 <= day) && ((1 <= day) && (day <= (day <= daysInMonth (month, year)daysInMonth (month, year)))));;
} }
/**/** * @param day ( * @param day (legallegal: : 1 ≤ day ≤ daysInMonth1 ≤ day ≤ daysInMonth)) * @param month ( * @param month (legallegal: : 1 ≤ month ≤ 121 ≤ month ≤ 12)) * @param year ( * @param year (legallegal: 1 ≤ year): 1 ≤ year) * * * * @return Whether the parameters are legal@return Whether the parameters are legal * * */ */ public public boolean legalDateboolean legalDate (int day, int month, int year)(int day, int month, int year) { { return return (1 <= year) &&(1 <= year) && ((1 <= month) && ((1 <= month) && (month <= 12)) && (month <= 12)) && ((1 <= day) && ((1 <= day) && (day <= (day <= daysInMonth (month, year)daysInMonth (month, year)))));;
} }
DateStuffDateStuff
Uncha
nged
Uncha
nged
Uncha
nged
Uncha
nged**
except that nowexcept that now it’s it’s publicpublic.. except that nowexcept that now it’s it’s publicpublic.. **
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class DateStuff {class DateStuff {
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
public int daysInMonth (int month, int year)public int daysInMonth (int month, int year) { ... } { ... }
public boolean public boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
public boolean public boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... }
private private boolean divides (int d, int n)boolean divides (int d, int n) { { ... } ... }
}}
class DateStuff {class DateStuff {
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
public int daysInMonth (int month, int year)public int daysInMonth (int month, int year) { ... } { ... }
public boolean public boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
public boolean public boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... }
private private boolean divides (int d, int n)boolean divides (int d, int n) { { ... } ... }
}}
Just
see
n
Just
see
n
Just
see
n
Just
see
n
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class DateStuff {class DateStuff {
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
public int daysInMonth (int month, int year)public int daysInMonth (int month, int year) { ... } { ... }
public boolean public boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
public boolean public boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... }
private private boolean divides (int d, int n)boolean divides (int d, int n) { { ... } ... }
}}
class DateStuff {class DateStuff {
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
public int daysInMonth (int month, int year)public int daysInMonth (int month, int year) { ... } { ... }
public boolean public boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
public boolean public boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... }
private private boolean divides (int d, int n)boolean divides (int d, int n) { { ... } ... }
}}
class DateStuff {class DateStuff {
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
public public staticstatic int daysInMonth ( ... ) int daysInMonth ( ... ) { ... } { ... }
public public staticstatic boolean boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
public public staticstatic boolean boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... }
private private staticstatic boolean divides (int d, int n)boolean divides (int d, int n) { { ... } ... }
}}
class DateStuff {class DateStuff {
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
public public staticstatic int daysInMonth ( ... ) int daysInMonth ( ... ) { ... } { ... }
public public staticstatic boolean boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
public public staticstatic boolean boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... }
private private staticstatic boolean divides (int d, int n)boolean divides (int d, int n) { { ... } ... }
}}
New
Jav
a
New
Jav
a co
ncep
t
conc
ept
New
Jav
a
New
Jav
a co
ncep
t
conc
ept
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
New Java Concept:New Java Concept:
MethodsMethods, as well as , as well as fieldsfields, may be declared , may be declared staticstatic..
staticstatic methodsmethods may work with may work with fieldsfields of its class, but only if of its class, but only if they are they are staticstatic..
staticstatic methodsmethods may invoke other may invoke other methodsmethods of its class, but of its class, but only if they are only if they are staticstatic..
In In DateStuffDateStuff, the methods , the methods isLeapYearisLeapYear and and dividesdivides work work only with their parameters – no only with their parameters – no fieldsfields..In In DateStuffDateStuff, the methods , the methods isLeapYearisLeapYear and and dividesdivides work work only with their parameters – no only with their parameters – no fieldsfields..
In In DateStuffDateStuff, the methods , the methods daysInMonthdaysInMonth and and legalDatelegalDate work with their parameters and (the work with their parameters and (the staticstatic fieldfield)) monthDaysmonthDays..In In DateStuffDateStuff, the methods , the methods daysInMonthdaysInMonth and and legalDatelegalDate work with their parameters and (the work with their parameters and (the staticstatic fieldfield)) monthDaysmonthDays..
In In DateStuffDateStuff, , daysInMonthdaysInMonth invokes invokes isLeapYearisLeapYear, which , which
invokes invokes dividesdivides. And . And legalDatelegalDate invokes invokes daysInMonthdaysInMonth..In In DateStuffDateStuff, , daysInMonthdaysInMonth invokes invokes isLeapYearisLeapYear, which , which
invokes invokes dividesdivides. And . And legalDatelegalDate invokes invokes daysInMonthdaysInMonth..
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class DateStuff {class DateStuff {
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
public public staticstatic int daysInMonth ( ... ) int daysInMonth ( ... ) { ... } { ... }
public public staticstatic boolean boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
public public staticstatic boolean boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... }
private private staticstatic boolean divides (int d, int n)boolean divides (int d, int n) { { ... } ... }
}}
class DateStuff {class DateStuff {
private static final int[] private static final int[] monthDays = monthDays = {31, 28, 31, 30, 31, 30, {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
public public staticstatic int daysInMonth ( ... ) int daysInMonth ( ... ) { ... } { ... }
public public staticstatic boolean boolean isLeapYear isLeapYear (int year)(int year) { ... } { ... }
public public staticstatic boolean boolean legalDatelegalDate (int day, int month, int year)(int day, int month, int year) { ... } { ... }
private private staticstatic boolean divides (int d, int n)boolean divides (int d, int n) { { ... } ... }
}}
New
Jav
a
New
Jav
a co
ncep
t
conc
ept
New
Jav
a
New
Jav
a co
ncep
t
conc
ept
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
New Java Concept:New Java Concept:
MethodsMethods, as well as , as well as fieldsfields, may be declared , may be declared staticstatic..
staticstatic methodsmethods may work with may work with fieldsfields of its class, but only if of its class, but only if they are they are staticstatic..
staticstatic methodsmethods may invoke other may invoke other methodsmethods of its class, but of its class, but only if they are only if they are staticstatic..
staticstatic fieldsfields and and methodsmethods belong to the belong to the classclass. They are . They are shared by all shared by all objectsobjects (if any) of the class. (if any) of the class.staticstatic fieldsfields and and methodsmethods belong to the belong to the classclass. They are . They are shared by all shared by all objectsobjects (if any) of the class. (if any) of the class.
To invoke a To invoke a staticstatic methodmethod of another class, we don’t need of another class, we don’t need an an objectobject of that other class. We invoke it prefixed with the of that other class. We invoke it prefixed with the name of its owning classname of its owning class (followed by the usual dot). (followed by the usual dot).
To invoke a To invoke a staticstatic methodmethod of another class, we don’t need of another class, we don’t need an an objectobject of that other class. We invoke it prefixed with the of that other class. We invoke it prefixed with the name of its owning classname of its owning class (followed by the usual dot). (followed by the usual dot).
Normally:Normally: we invoke a we invoke a methodmethod on an on an objectobject. We invoke it . We invoke it prefixed with the prefixed with the name of the variable referencing the objectname of the variable referencing the object (followed by the usual dot).(followed by the usual dot).
Normally:Normally: we invoke a we invoke a methodmethod on an on an objectobject. We invoke it . We invoke it prefixed with the prefixed with the name of the variable referencing the objectname of the variable referencing the object (followed by the usual dot).(followed by the usual dot).
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (legalDate (day, month, year)legalDate (day, month, year)) {) { setLegalDate (day, month, year)setLegalDate (day, month, year) } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 1970-01-01"); " 1970-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (legalDate (day, month, year)legalDate (day, month, year)) {) { setLegalDate (day, month, year)setLegalDate (day, month, year) } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 1970-01-01"); " 1970-01-01"); }} } }
See
n be
fore
See
n be
fore
See
n be
fore
See
n be
fore
R2R2
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (legalDate (day, month, year)legalDate (day, month, year)) {) { setLegalDate (day, month, year)setLegalDate (day, month, year) } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 1970-01-01"); " 1970-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (legalDate (day, month, year)legalDate (day, month, year)) {) { setLegalDate (day, month, year)setLegalDate (day, month, year) } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 1970-01-01"); " 1970-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (DateStuffDateStuff..legalDate (day, month, year)legalDate (day, month, year)) {) { setLegalDate (day, month, year)setLegalDate (day, month, year) } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 1970-01-01"); " 1970-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { if ( if (DateStuffDateStuff..legalDate (day, month, year)legalDate (day, month, year)) {) { setLegalDate (day, month, year)setLegalDate (day, month, year) } else { } else { date = 1; date = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 1970-01-01"); " 1970-01-01"); }} } }
R2R2
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; if (! if (!legalDate ()legalDate (){{ this.day = 1;this.day = 1; this.month = 1; this.month = 1; this.year = 1; this.year = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 0001-01-01"); " 0001-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; if (! if (!legalDate ()legalDate (){{ this.day = 1;this.day = 1; this.month = 1; this.month = 1; this.year = 1; this.year = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 0001-01-01"); " 0001-01-01"); }} } }
See
n be
fore
See
n be
fore
See
n be
fore
See
n be
fore
R1R1
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; if (! if (!legalDate ()legalDate (){{ this.day = 1;this.day = 1; this.month = 1; this.month = 1; this.year = 1; this.year = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 0001-01-01"); " 0001-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; if (! if (!legalDate ()legalDate (){{ this.day = 1;this.day = 1; this.month = 1; this.month = 1; this.year = 1; this.year = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 0001-01-01"); " 0001-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; if (! if (!DateStuffDateStuff..legalDate (day, month, year) legalDate (day, month, year) {{ this.day = 1;this.day = 1; this.month = 1; this.month = 1; this.year = 1; this.year = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 0001-01-01"); " 0001-01-01"); }} } }
/** /** * Set the date to a particular day * Set the date to a particular day */ */ public void setDate public void setDate (int day, int month, int year) (int day, int month, int year) { { this.day = day;this.day = day; this.month = month; this.month = month; this.year = year; this.year = year; if (! if (!DateStuffDateStuff..legalDate (day, month, year) legalDate (day, month, year) {{ this.day = 1;this.day = 1; this.month = 1; this.month = 1; this.year = 1; this.year = 1; System.out.printlnSystem.out.println ("*** setDate: Bad parameters" + ("*** setDate: Bad parameters" + " ... setting date to" + " ... setting date to" + " 0001-01-01"); " 0001-01-01"); }} } }
R1R1
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
New Java Concept:New Java Concept:
MethodsMethods, as well as , as well as fieldsfields, may be declared , may be declared staticstatic..
staticstatic methodsmethods may work with may work with fieldsfields of its class, but only if of its class, but only if they are they are staticstatic..
staticstatic methodsmethods may invoke other may invoke other methodsmethods of its class, but of its class, but only if they are only if they are staticstatic..
staticstatic fieldsfields and and methodsmethods belong to the belong to the classclass. They are . They are shared by all shared by all objectsobjects (if any) of the class. (if any) of the class.staticstatic fieldsfields and and methodsmethods belong to the belong to the classclass. They are . They are shared by all shared by all objectsobjects (if any) of the class. (if any) of the class.
To invoke a To invoke a staticstatic methodmethod of another class, we don’t need of another class, we don’t need an an objectobject of that other class. We invoke it prefixed with the of that other class. We invoke it prefixed with the namename of its owning class (followed by the usual dot). of its owning class (followed by the usual dot).
To invoke a To invoke a staticstatic methodmethod of another class, we don’t need of another class, we don’t need an an objectobject of that other class. We invoke it prefixed with the of that other class. We invoke it prefixed with the namename of its owning class (followed by the usual dot). of its owning class (followed by the usual dot).
Classes with Classes with onlyonly staticstatic fieldsfields and and methodsmethods need no objects need no objects constructed from them.constructed from them.Classes with Classes with onlyonly staticstatic fieldsfields and and methodsmethods need no objects need no objects constructed from them.constructed from them.Classes with Classes with onlyonly staticstatic fieldsfields and and methodsmethods need no objects need no objects constructed from them. constructed from them. So … hide the constructor!!So … hide the constructor!!Classes with Classes with onlyonly staticstatic fieldsfields and and methodsmethods need no objects need no objects constructed from them. constructed from them. So … hide the constructor!!So … hide the constructor!!
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
class DateStuff {class DateStuff {
... ... monthDaysmonthDays (private static field)(private static field)
... daysInMonth... daysInMonth,, isLeapYearisLeapYear,, legalDate legalDate (public static methods)(public static methods)
... ... dividesdivides (private static method)(private static method) }}
class DateStuff {class DateStuff {
... ... monthDaysmonthDays (private static field)(private static field)
... daysInMonth... daysInMonth,, isLeapYearisLeapYear,, legalDate legalDate (public static methods)(public static methods)
... ... dividesdivides (private static method)(private static method) }}
class DateStuff {class DateStuff {
... ... monthDaysmonthDays (private static field)(private static field)
private DateStuff () {private DateStuff () { // deliberately empty// deliberately empty }}
... daysInMonth... daysInMonth,, isLeapYearisLeapYear,, legalDate legalDate (public static methods)(public static methods)
... ... dividesdivides (private static method)(private static method) }}
class DateStuff {class DateStuff {
... ... monthDaysmonthDays (private static field)(private static field)
private DateStuff () {private DateStuff () { // deliberately empty// deliberately empty }}
... daysInMonth... daysInMonth,, isLeapYearisLeapYear,, legalDate legalDate (public static methods)(public static methods)
... ... dividesdivides (private static method)(private static method) }}
constructorconstructor
This class doesn’t construct any instances of itself. Its This class doesn’t construct any instances of itself. Its constructorconstructor is is privateprivate, so nobody else can. There can be no , so nobody else can. There can be no DateStuffDateStuff objects. objects.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
OBSERVATION:OBSERVATION:
R2R2
We could have used the We could have used the fully parametrisedfully parametrised versions: versions:
int daysInMonth (int month, int year)int daysInMonth (int month, int year) boolean boolean isLeapYear isLeapYear (int year)(int year) boolean boolean legalDate legalDate (int day, int month, int year)(int day, int month, int year)
in the version of in the version of DateDate, as well as in version ., as well as in version .R1R1
They do not depend on any chosen They do not depend on any chosen fieldfield representation. representation.
They may, therefore, be worth They may, therefore, be worth factoring outfactoring out into a separate into a separate class, class, DateStuffDateStuff, , which either representation forwhich either representation for DateDate could usecould use..
See
n be
fore
See
n be
fore
See
n be
fore
See
n be
fore
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
They may, therefore, be worth They may, therefore, be worth factoring outfactoring out into a separate into a separate class, class, DateStuffDateStuff, , which either representation forwhich either representation for DateDate could usecould use..
DateStuffDateStuff contains a set of utility contains a set of utility functionsfunctions that work just that work just with their with their parametersparameters. They need no persistent state to be . They need no persistent state to be held in fields. Such things are very held in fields. Such things are very reusablereusable..
DateStuffDateStuff can be used with either the can be used with either the R1R1 or or R2R2 choice choicefor representation of a for representation of a datedate in the in the DateDate class. class.
Factoring out these low-level functions into Factoring out these low-level functions into DateStuffDateStuff leaves the leaves the DateDate class ( class (R1R1 or or R2R2) ) shortershorter..
Collections of purely Collections of purely staticstatic methods are how we used to methods are how we used to do things do things – before Object-Orientation– before Object-Orientation. . Don’t make a habit Don’t make a habit of it!of it! Objects carrying their own state are powerful!!Objects carrying their own state are powerful!!
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The Mañana Principle
When – during When – during implementation of a method implementation of a method – we wish we had a certain – we wish we had a certain support method,support method, write our write our
code as if we had itcode as if we had it. . Implement it later.Implement it later.
Review
Review
Review
Review
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The Mañana Principle
When – during When – during implementation of a method implementation of a method – we wish we had a certain – we wish we had a certain support method,support method, write our write our
code as if we had itcode as if we had it. . Implement it later.Implement it later.
Special Case Rule:Special Case Rule:Often, we need to identify Often, we need to identify
and deal with a special case.and deal with a special case. Write the code for this in a Write the code for this in a
separate methodseparate method. . Implement it later.Implement it later.
Review
Review
Review
Review
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The Mañana Principle
Special Case Rule:Special Case Rule:Often, we need to identify Often, we need to identify
and deal with a special case.and deal with a special case. Write the code for this in a Write the code for this in a
separate methodseparate method. . Implement it later.Implement it later.
Hard Problem Rule:Hard Problem Rule:when we when we need the answerneed the answer
to a problem we cannot to a problem we cannot immediately solve, immediately solve, defer it a defer it a
separate methodseparate method.. Implement it later.Implement it later.
Review
Review
Review
Review
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The Mañana Principle
Hard Problem Rule:Hard Problem Rule:when we when we need the answerneed the answer
to a problem we cannot to a problem we cannot immediately solve, immediately solve, defer it a defer it a
separate methodseparate method.. Implement it later.Implement it later.
Heavy Functionality Rule:Heavy Functionality Rule:when when some statements orsome statements orexpressions become too expressions become too
long or complex, long or complex, move them move them into a separate methodinto a separate method..
Implement it later.Implement it later.
Review
Review
Review
Review
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The Mañana Principle
Heavy Functionality Rule:Heavy Functionality Rule:when when some statements orsome statements orexpressions become too expressions become too
long or complex, long or complex, move them move them into a separate methodinto a separate method..
Implement it later.Implement it later.
Nested Loop Rule:Nested Loop Rule:when we when we have a nestedhave a nested
loop, loop, move the inner loop move the inner loop into a separate methodinto a separate method..
Implement it later.Implement it later.
Review
Review
Review
Review
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Nested Loop Rule:Nested Loop Rule:when we when we have a nestedhave a nested
loop, loop, move the inner loop move the inner loop into a separate methodinto a separate method..
Implement it later.Implement it later.
The Mañana Principle
Code Duplication Rule:Code Duplication Rule:when we when we repeat the same repeat the same
code, code, move that code into a move that code into a separate method and repeat separate method and repeat
an invocation insteadan invocation instead.. Implement it later.Implement it later.
Review
Review
Review
Review
Review
Review
Review
Review
Mañana – Specific Forms
• Special Case rule
• Hard Problem rule
• Heavy Functionality rule
• Nested Loop rule
• Code Duplication rule
Review
Review
Review
Review
Utility Classes
publicpublic static static methodsmethods, as well as , as well as privateprivate static static fieldsfields..publicpublic static static methodsmethods, as well as , as well as privateprivate static static fieldsfields..
Utility classes (like Utility classes (like DateStuffDateStuff) have only the above kind of ) have only the above kind of methodsmethods and and fieldsfields..Utility classes (like Utility classes (like DateStuffDateStuff) have only the above kind of ) have only the above kind of methodsmethods and and fieldsfields..
publicpublic static static methodsmethods are invoked via the are invoked via the classclass name – no name – no objectobject is needed. is needed.publicpublic static static methodsmethods are invoked via the are invoked via the classclass name – no name – no objectobject is needed. is needed.
Utility classes (like Utility classes (like DateStuffDateStuff) have only ) have only privateprivate constructors constructors – no – no objectsobjects can be constructed. can be constructed.Utility classes (like Utility classes (like DateStuffDateStuff) have only ) have only privateprivate constructors constructors – no – no objectsobjects can be constructed. can be constructed.