Wednesday, May 4, 2011

Designing the Hand Class, Part 3

OK, the day hand was not a problem. It took me a little time to work through the cases, but it was fine:

testDayHand 1 2

testDayHand 0 2

testDayHand 43199 2

testDayHand 43201 1

testDayHand 43200 1

testDayHand 86399 1

testDayHand 86400 2

testDayHand 86401 2

This testing, like the other hands, checks at 0 and 1 tick and then again as the number of ticks is sufficient to change the object's display. For Day, that means every 12 hours or 43200 ticks.

Oh, 2 means “a.m.” Well, I guess. 1 will mean “p.m.”

I thought about writing more general tests – a function that would work for all the different hands or for other imaginable hands. I decided, though, that that was beyond my design.

Next, I don't see how to test display any better than what I've done so far. For now, I'm going to put a note in the display test file and call the Hand class done.

Except for documentation, which I will write now.

Here is the final version of the class:


Add-Type @'

#define DEBUG


/*

This class models a hand on a wristwatch. The same class will work for

second hands, minute hands, hour hands, and a.m./p.m. hands.


As an assumption, 2 means a.m., while 1 means p.m.


This class might work for other items (like weeks or equal-length months), but hasn't been designed

or tested for those uses.

*/

public class Hand

{

// FIELDS

// The number of ticks necessary to return to the same state.

int maxState;

// Number of ticks necessary to change the object's displayed state. 60 for a minute, for instance.

int increment;

// Current state of the object. Values 1 to maxState, inclusive.

// Note: not 0-based.

int state;

// CONSTRUCTORS

/*

Specified the type of hand, using the size of the hand (in seconds) and the nubmer of seconds needed to

increment the hand.

xState: Number of ticks necessary to make the hand have the same value.

xIncrement: Number of ticks necessary to cause a change in the hand's displayed value.

*/

public Hand (int xState, int xIncrement)

{

if (xIncrement < 1) { xIncrement = 1;} // validation to avoid negative numbers and divide by 0 errors.

if (xState < xIncrement) { xState = xIncrement;} // validation to avoid cases where increment is larger than the possible size of the hand.

maxState = xState; // Set maxState

increment = xIncrement; // Set increment

state = xState; // Set current state to same as maxState (that is, to midnight.)

}

/*

Displays the value of the hand.

Value is determined by the state of the hand to the resolution specified by increment.

*/

public int Display

{

get

{

if ((state / increment) == 0) return (maxState / increment); // States of less than 1 increment are returned as the max value. That is, 12 oclock and one second is 12 oclock.

return state / increment; // Returns current value. Note the implicit truncation achieved by using only ints.

}

}

// PROPERTIES

// METHODS

/*

Change the state of the hand by one tick, implicitly a second.

Returns: True if the tick can be accomplished. This version does not have a way to return false.

*/

public bool tick()

{

state++; // Increase state by one.

if (state > maxState) // If larger than the size of the hand:

{

state = 1; // Reset to 1

} // There is no 0 value.

return true; // Always return True.

}

#if DEBUG

// TESTING CODE

#endif

// OVERRIDES

}

'@

This is a little short for today (I've been at this about 40 minutes. For this week, I'm trying to do about one hour at a time.)

No comments:

Post a Comment