Monday, January 17, 2005

Object BootCamp - day5

It was another day of lots of practical work and less of theory. We started the class with each one of us taking turns and giving a brief description of the various concepts that we had covered so far. We then continued with the Quantity/Unit exercise. We were asked to represent addition of two Quantities in terms of objects. For example, it should be possible to say 2 Quarts and 1 Quart equals 12 Cups.

So we started by writing the assert statement:
assertEquals(new Quantity(12, Unit.CUP), new Quantity(1, Unit.QUART).add(new Quantity(2, Unit.Quart));

We then easily implemented the add method in Quantity by putting the following code:

public Quantity add( Quantity other ) {
return new Quantity(this.value+ other.unit.convertTo(other.this)*other.value, this.unit);
}


Fred suggested to move the complex statement out there to a method in itself. So we ended up like this:
return new Quantity(this.value+convertedValue(other), this.unit);

When we did this, we found yet another usage of the above statement in equals method and changed that too. We found the code to be clean at the end of this. Fred noted that it is important to listen to code, as Kent calls it. The complex statement was yelling at us to refactor it. Had we listened to the code, we would have ended up with a neat code.

The class ended with an interesting problem waiting to be solved tomorrow. How do we represent Temperature conversions through the same sets of classes? How do we represent additions of two temperatures? 0 deg Celcius + 0 deg Celcius = 0 deg Celcius? Whereas, 32 deg F + 32 deg F = 64 deg F? We spent some time analyzing the problem, but could not come to a design. Try out for yourselves if you can come up with a design. Good Luck. Will probably provide solution in tomorrow's notes.

0 Comments:

Post a Comment

<< Home