|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
We have seen that an object can
take in an objects as a parameter:
In lecture we wrote a
method for PizzaServers,
namely
We can also write methods where we return an objects1. For example, let's write a function for a PizzaServer which takes in two possible work-Dates, and decides which one they prefer. For example, given the choice of working 2007.Feb.21 and 2007.Feb.28, a particular PizzaServer might have a preference for 2007.Feb.21.
Let's write this method together, using the steps of The Design Recipe.
In addition to solving this particular problem about choosing a shift,
what we're really trying to teach is the recipe.
The same recipe guides you through all problems, even those which are
much more complicated than the one we work on here.
See the lect07b-soln.html,
for how each step corresponds to our problem at hand.
The body of the function. This is the only non-automatic part of the process. Reflect on the test cases you worked through by hand: how did you get from the input to the output? What is the general case?
Things to help you on your way:
Often, the body of the function is just a return statement, although you might also name partial results using local variables, and use an if statement which contains a return statement or names partial results.
Write a method nextDay. Use the recipe above. Note that steps I and II are already done; those steps apply to classes, and don't need to be repeated for further methods of a class.
First get a method working which may occasionally return an invalid date, but otherwise is correct.
Then, get a method which
handles the difficult cases.
Hint:
Make the obvious candidate, and see if it's valid.
If not, then make a second candidate, and check if that's valid.
If not, then a third try should do the trick.
This will be
Java's if-else is a statement -- it is on its own line(s), and it will happen before or after other statements, but it can't be combined as a sub-part of larger statements. This is different than expressions, like “Math.sqrt(16)” or “(a && b)”, which might occur as sub-expressions of the larger expression “(Math.sqrt(16) > x) || (a && b))”.
It turns out, there is a statement form of if-else, using the punctuation “?” and “:”.
conditonal-operator ::= condition ? expressiontrue : expressionfalseFor example, if a Dates have a predicate isLeapYear, and d is an instance of Date, then the following expression evaluates to the number of days in February:
d.isLeapYear() ? 29 : 28 |
d.getDay() <= (d.isLeapYear() ? 29 : 28) |
// ...inside isValid: else if (this.getMonth()==2) { return this.getDay() <= (this.isLeapYear() ? 29 : 28) } else // ... consider other months besides February |
Note that the conditional operator must contain answer-expressions for both condition-being-true and condition-being-false. (This is different from if, which can legally be written without an else.)
We can use the conditional operator to make the above code more concise:
Date whichDayToWork( Date d1, Date d2 ) { if (this.getBalance() < 100) { return (d1.comesBefore(d2)) ? d1 : d2; } else { /* balance >= 100 */ return (d1.comesBefore(d2)) ? d2 : d1; } } |
Challenge exercise: re-write the above statement starting with the decision of which date comes first, and (in each case) asking about what the balance is:
Date whichDayToWork( Date d1, Date d2 ) { if (d1.comesBefore(d2)) { return ? : ; } else { /* d1 doesn't come before d2 */ return ? : ; } } |
Although most Java programmers tend to use if most of the time, the conditional expression is actually extremely handy, and you are encouraged to use it if you're comfortable with it.
1 Okay yes, the Constructor is already a method which returns an object, although we didn't actually write the return statement ourself. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2008, Ian Barland, Radford University Last modified 2008.Mar.03 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |