Site Map  Search

OOA Home OOD content UML Corrections book Code object Exercise Solutions oriented Resources tutorial Miscellany textbook

Code Samples

Chapter 9 - Design

Section 9.5.1

Java, C++, C#, Smalltalk

Java

final class Route {
  // ...
  Duration estimatedTimeToTransit(Transport transportUsed) {
    Duration durationEstimate = new Duration(0);
    // ...
    while ((leg = this.nextLeg()) != null) {
      durationEstimate.add(leg.estimatedTimeToTransit(transportUsed));
    }
    return durationEstimate;
  }
  // ...
}

C++

class Route {
  // ...
  public:
    Duration estimatedTimeToTransit(const Transport& transportUsed) {
      Duration durationEstimate;
      // ...
      while ( (pLeg = this->nextLeg()) != 0 ) {
        durationEstimate += pLeg->estimatedTimeToTransit(transportUsed);
      }
      return durationEstimate;
    }
    // ...
};

C#

internal class Route {
  // ...
  public Duration estimatedTimeToTransit(Transport transportUsed) {
    Duration durationEstimate = new Duration(0);
    // ...
    while ((leg = this.nextLeg()) != null) {
      durationEstimate = durationEstimate + leg.estimatedTimeToTransit(transportUsed);
        // questionable operator overloading as static operator overloading in C# is not
        // as useful as the non-static operator overloading in C++
    }
    return durationEstimate;
  }
  // ...
}

Smalltalk

Class: Route
  "..."
Route>>estimatedTransitTimeUsing: aTransport
  "Answer a Time object representing the receiver's estimate of the time the
   Transport argument object would take to transit the receiver."
  | durationEstimate |
  "..."
  [ (leg := self nextLeg) ~= nil ] whileTrue: [
    durationEstimate := durationEstimate addTime: (leg estimatedTransitTimeUsing: aTransport)
  ].
  ^durationEstimate