# OOP in Math

My coworker Lyubo came up with the following OOP problem while we were having lunch the other day. He says it is a well known problem that you can see on numerous places but it is new to me.

We know from mathematics that the square is a special case of rectangle. In OOP terms "special case" means inheritance. Obviously the mathematical square inherits from mathematical rectangle. Your task is to create OOP model for that relationship. Lets say that we want the rectangle to expose get property (getter for you Java readers) – the area of the rectangle. This is not as simple as it sounds. Here is one of the simplest (and very wrong) solutions:

class Rectangle
{
/// <summary>
/// A side of the rectangle
/// </summary>
public uint A { get; set; }

/// <summary>
/// B side of the rectangle
/// </summary>
public uint B { get; set; }

virtual public uint Area
{
get { return A * B; }
}
}

class Square : Rectangle
{
public override uint Area
{
get
{
return A * A;
}
}
}

However this means that the B property is not actually used. This can lead to unexpected results in polymorphic client code. We expect a rectangle to have a area of A*B and somehow it will not if it is an instance of Square. We can override the property setters, add some exceptions but what inheritance means is that the child class can do ANYTHING that the base class can. If you need to add special case behavior for any of the child classes then the inheritance hierarchy you have created is simply wrong. So go ahead make it right... (or view full text for explanation)
Tags:   english programming
Posted by:   Stilgar
00:20 25.10.2008

# Functional Sieve of Eratosthenes

I was thinking of how I can use functional constructs to implement Sieve of Eratosthenes and I bet that millions of readers are dying to learn what I came up with...
Tags:   english programming
Posted by:   Stilgar
01:07 13.10.2008

# Приложна математика

Мислите, че разбирате от математика? Мислите, че разбирате от програмиране? Следната математическа задача възникна от съвсем реална ситуация.

В пицария Годзила са увеличили диаметъра на пицата от 27 на 30cm.

a) с колко процента се увеличи площа на пицата?
b) Ако от старата (27cm) пица се отреже парче отговарящо на централен ъгъл X то в центъра на парчето ще има два пъти повече сирене отколкото на ръба му. Ако промяната в дисперсията на сиренето е постоянна, колко процента по-малко сирене има по ръба на новата пица отколкото в центъра и.
c) да се изчисли с колко процента се е увеличило сиренето в пицата при данните от подусловие b).
d) да се състави програма, която при зададени входни данни стар и нов диаметър на пицата да изчислява резултати за горните подусловия.
e) да се направи горната програма разширяема като се позволи написване на plugin съдържащ функция за изменение на дисперсията приемаща аргумент разстояние от центъра и връщаща резултат коефициент (сирене в центъра)/(сирене на съответния сантиметър).

Много обичам пиците от Годзила.
Tags:   bulgarian programming
Posted by:   Stilgar
17:16 18.09.2008

# Happy Programmer's Day

Happy programmer's day to everyone who is doing any coding. The day of the programmer is celebrated on the 256th day of the year (do I have to explain why?). Normally it falls on September 13th but on leap years like this year it falls on September 12th.

Happy coding and may the Source be with you.
Posted by:   Stilgar
13:18 12.09.2008

# .NET 3.5 SP1 and Visual Studio 2008 SP1 released

Service Pack 1 for .NET 3.5 and Service Pack 1 for Visual Studio 2008 have been released.

online installer
full package - 237MB includes several versions of the framework for several operatin systems and architectures

online installer
iso image - 851MB contains SP1 for all versions of VS

The Express Editions are not patched but installed over the old versions. Online and offline installers can be found here.

These releses add MANY features. Most of them are listed here. Enjoy!
Tags:   english programming
Posted by:   Stilgar
02:18 13.08.2008