RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect07b
Returning objects from methods (pt. I)
the design recipe reminded

We have seen that an object can take in an objects as a parameter: In lecture we wrote a method for PizzaServers, namely boolean hasHigherSalaryThan(PizzaServer rival). Then in lab, we wrote methods for class Date: first boolean isEqual(Date other) and then boolean comesBefore(Date other).

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.

  1. Data analysis — What are the pieces of information at hand, for this problem, and which data types should be used to represent them in the program? (e.g., int, String, boolean, ...?)
  2. Examples of the data. Make a bunch of example instances.
    (If using BlueJ with unit test tools enabled, you can create these these instances by right-clicking on the class; then you can right-click on the test-class and select “Bench to test fixture”.
  3. Signature — for each method (function), specify its name, what input parameters it needs (name and type), and what type of information it will return. (See our mantra for signatures)
  4. Comments (written in javadoc). Describe what each parameter means (for example “the price of the item, in dollars”, or “the number of students officially enrolled in the course”) as well as the meaning of the return value.
  5. Complete the stub function, and compile. Note that you still haven't yet written any code which computes a solution.
  6. Test cases: Write 2-3 test cases (or more, as appropriate).
  7. 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.

  8. Test — run the test cases you already wrote down from step 2. Does your program give the results you expect?
  9. Refactor — review and (as necessary) rewrite your code. Does it obey the The Laws of Programming?

Returning new objects

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 tomorrow's lab.

(optional) the conditional operator

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 : expressionfalse
For 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
This can be combined into other expressions, like the right-hand-side of an <= expression:
d.getDay() <= (d.isLeapYear() ? 29 : 28)
And of course, we already know that a return statement can return any expression:
  // ...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;
    }
  }
In this case, we are combining the conditional-operator into the expression portion of the return-statement (whose syntax is return expression).

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.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2008, Ian Barland, Radford University
Last modified 2008.Mar.03 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme