- How about making the wheel customized for size?
- How about making different wheels have different sizes?
The size of the wheel does not change after creation – that's an assumption, but I don't know how else to approach it. I am thinking in terms of modeling the real world, here. So, I'll add a constructor to set a private field that will not be resettable.
private int size = 10;
(Starting with a value gives me a default. If I want to prohibit the use of that default, I can just make the no-argument constructor private.)
Here's the constructor:
public Wheel (int set_size)
{
size = set_size;
}
This will require a change in the Value property:
temp = (next.Value * size);
and in the click() method:
if (state [GREATER THAN SYMBOL] (size - 1))
Oh – that seems to be all. Let's see if it worked.
Nope. Very broken. Hold on.
Missed one:
next = new Wheel(size);
Let's try again.
I needed to change my testing code:
$x = new Wheel(12)
for ($i = 0; $i -lt 10000; $i++)
{
$x.addOne()
}
OK, I think it's working now. It returns the numbers from 1 to 10000. The only problem is that, since my Value property translates everything into decimal, it doesn't look any different from the decimal version. I better fix that.
public int Value
{
get
{
if (next != null)
{
Console.Write(next.Value + " ");
}
return state;
}
}
Here's the output:
. . .
5 9 4 9
5 9 4 10
5 9 4 11
5 9 5 0
5 9 5 1
5 9 5 2
5 9 5 3
5 9 5 4
(The last of these equals 10000 in base 12).
This doesn't work. Or it works only because PowerShell is low-maintenance. Each time through the loop, it goes downstream to the next biggest wheel until there are no larger wheels. Then it returns a value which gets printed.
So what prints the last value? It's the return value to the testing code, which PowerShell echos. That's why there's a hard return at the end of each series of wheels.
If we want to use this class outside the PowerShell development environment, the Value property needs to be changed.
So, what about the second design challenge – supporting different sized wheels?
That is, instead of an odometer, imagine a measuring wheel that measures in inches, feet, and yards.
This is challenging because each wheel will need to know something more about its place in the scheme of things. It needs to know that it is the foot-display wheel, that the wheel to the left of it is the yard display wheel (since the foot-display wheel has to create the yard-display wheel when it ticks up higher than 2.)
I fought with this one a couple of times, off-line. What I came up with is kind of hacky, but – in principle – it works OK.
First, I added an array to keep the list of values in the progression. That is, 12 for the first wheel (12 inches in a foot), three for the second (3 feet in a yard) and so forth:
private int[] progression = {12, 3, 1760, 10};
The last value recurses in this design – that is, every wheel after miles has a value of 10.
I wanted to stick to the most basic array control logic – skipping the built-in logic for C# Array or List class. This is what messed me up.
Without further ado:
// CONSTRUCTORS
public Wheel (int temp) {
size = progression[0];
int a = progression.GetLength(0);
a = a-2;
progression[0] = progression[1];
for (int i = 1; i [LESS THAN SYMBOL] a; i++) {
progression[i] = progression[i+1];
}
}
public Wheel (int[] set_size)
{
size = set_size[0];
int a = set_size.GetLength(0);
a = a-2;
progression[0] = set_size[1];
for (int i = 1; i [LESS THAN SYMBOL] a; i++) {
progression[i] = set_size[i+1];
}
(Yes, I am cringing from the hacky-ness of my code. It seems to work, though.)
The reason that there isn't a no-argument constructor for the default case is that PowerShell doesn't seem to like that – it wants to use a constructor with an argument. I just hacked it.
So, that's that.
No comments:
Post a Comment