I got one that’s kind of the opposite of ADD, but no less stupid for it.
I’m taking a class on object-oriented programming. An assignment I had last week was to write a program that would take numbers given to it by the user, and display them as lines and rectangles on a grid.
First part of the assignment, the user enters in two sets of coordinates, and the program creates a line between them, and returns the length of that line. Not too hard - the formula for calculating the length of a line is a bit complicated, but nothing terribly difficult. The tricky part is that we’re supposed to be doing this using classes: that’s the point of the exercise, learning how to use classes.
Next, the user enters in a starting coordinate, a height, and a width: I have to write a class that will create a rectangle of those dimensions. Okay, easy enough. The starting coordinates are the lower left corner, you can use the height and length to determine the other four corners, and wallah!* A rectangle!
Part three, the program is supposed to return the area of the rectangle. Well, this requires a bit of cleverness! I’ve got a line class that will determine the length of a line. I need my rectangle class to use the line class to get the length of each side, which I can multiply together to get the area. We hadn’t actually covered how to reference classes within other classes, but it seems like it should work. After about twenty minutes of tinkering, I’ve got an elegant bit of code that takes information from one class, and uses it in a different class to calculate the area of a rectangle.
Which is when I realize that the area of a rectangle is it’s height, times its width. Which is directly supplied by the user when he creates the rectangle. I had maybe a dozen lines of code, when all I needed was “area = x * y.”
:smack:
I ended up changing it, so as not to look like a total dumbass when I turned it in. But, goddammit, my way was more fun!
[sub]*Yes, I know.[/sub]