Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Shape Hierarchy

Ranch Hand
Posts: 209
As in my previous posts I have always asserted to the fact that I never post my homework. These questions I take from the net for practice. Reccently I read Polymorphism from "Thinking in Java" and I thought that now to try out my understanding I should do some work so I got this work. I somewhat finished this work but have doubts on Trapezium. Can it be regarded as a Quadrilateral? And pleasse if you see some error please inform me and give me a hint on how to resolve it.

This post may seem long but most of the codes are same.

I have not teacher available at the moment to guide me so I come here.

Write an inheritance hierarchy for classes Quadrilateral, Trapezium, Parallelogram, Rectangle and Square. Use Quadrilateral as the superclass of the hierarchy. Make the hierarchy as deep (i.e., as many levels) as possible. Specify the instance variables and methods for each class. The private instance variables of Quadrilateral should be the four end points of the Quadrilateral. Write a program that instantiates objects of your classes and outputs each object's area.

Here are my codes for different classes:

The Rectangle class

The Square class

The Paralleogram class

The ShapeGenerator class

Timmy Marks
Ranch Hand
Posts: 226
Make the hierarchy as deep (i.e., as many levels) as possible.

-> A Trapezium is a Quadrilateral with two sides parallel
-> A Parallelogram is a Trapezium with both pairs of sides parallel
-> A Rectangle is a Parallelogram with all right angles
-> A Square is a Rectangle with all side lengths equal

Ryan McGuire
Ranch Hand
Posts: 1086
4
Ahhh... so we meet again.

A few things:
1. What Timmy said.

2. You're kinda cheating with your Parellelogram class. The constructor doesn't really have enough arguments to define the shape. Once you have x, y, width, and height, you still need the angle between two sides. Granted, if the user of the class has to calculate the height perpendicular to one of the "width" sides, then you have all the information you need to calculate the area. But it would be more real-life if you allowed the user to define a parallelogram by two adjacent side lengths and the angle between them. If one point is at (x,y) and you assume one side is parallel to the x axis, you'll need a little trigonometry to find the third and fourth sides. Or maybe the user provides three points and the Parallelogram constructor calculates the fourth.

3. calculateArea() for Quadrilateral doesn't have to be abstract. Look here.

4. Since you can calculate the area, you might as well make the Quadrilateral class non-abstract with the appropriate constructor(s), right?

5. Once you start talking about non-right angles, which I think you should use for the Parallelogram, you should start using more floats and/or doubles instead of ints. (Look at java.awt.geom.Point2D.)

I hope this helps.

Ryan
[ May 09, 2005: Message edited by: Ryan McGuire ]

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
This exercise takes you to a fairly deep theoretical problem in OO. Is a square a rectangle? Say I write this test for rectangle:

The Liskov Substitution Principle tells me any test that works for rectangle should also work for any derived class. Will that test work for square? Or did square overload setHeight and setWidth to change both height and width since they are required to be equal?

In geometry a square is a rectangle. In software, maybe not.

There is no certifiably correct answer to this, so just take away a reminder to be very careful with your class heirarchy. Advice like "make it as deep as you can" is fun in lessons, but dangerous in real systems. A near opposite bit of advice is the safest way to satisfy Liskov: never extend a concrete class. Of course all generalizations are false, so don't tie yourself in knots on that one either. And remember software is not real life; your class hierarchy need not match any reality if it doesn't make sense for software.

Ryan McGuire
Ranch Hand
Posts: 1086
4
...and a couple trivial things.

1. What? No Rhombus?

2. Your use of "trapezium" separate from "quadrilateral" had me bewildered until I looked it up. Dictionary.com defines "trapezium" as...
• A quadrilateral having no parallel sides.
• Chiefly British. A trapezoid.
• A bone in the wrist at the base of the thumb.

• I had only ever heard of the first and third definitions, but I take it you're being "chiefly British."

Ryan

fred rosenberger
lowercase baba
Bartender
Posts: 12235
36
there are actually two differing definitions of trapezium, depending on where you live. Usually, in the U.S.A, a trapezium is a four sided object where no two sides are parallel.

However, the British definition is a four sided object where exactly two sides ARE parallel - what in the U.S.A we'd call a trapezoid.
[ May 09, 2005: Message edited by: fred rosenberger ]

Ranch Hand
Posts: 209
I was flabbergasted to see so much of information. Thank you very very much guys. I saved this page for future reference so that I can take in all the information at leisure.
And Ryan that link you gave me to "Polygon Area and Centroid" was too much too absorb. Anyways thanks for trying to help me out. Actually I am just 17 and I haven't learnt all that stuff in school till now.
As its late here, I will continue my program tomorrow. Thanks guys.

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Wow, at 17 I had almost mastered the four function calculator. Of course they were brand new and cost \$400. Keep coding, keep posting, keep it fun. Sorry if my post went off the complexity deep end!

Ranch Hand
Posts: 209
Thank you very much for all of your suggestions and now I will be posting my updated work according to the suggestions given by you guys, if you notice anything wrong please inform me.
And about the Trapezium I just couldn't understand how a Parallelogram could be a child class of Trapezium. I will be extending the Trapezium class directly from Quadrilateral(I haven't done this class yet).

The Parallelogram class

The MyRect class

The Square class

The ShapeGenerator class

Neeraj Dheer
Ranch Hand
Posts: 225

The Liskov Substitution Principle tells me any test that works for rectangle should also work for any derived class. Will that test work for square? Or did square overload setHeight and setWidth to change both height and width since they are required to be equal?

very, very interesting. and to think OO is supposed to follow real life. so..which book was it again?? Head First Design Principles??

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
It's often useful to make objects that model real things. In my work we have party (person), contract, communication (phone call or letter) and so on. But it's also harmful to model too literally. The objective is not to model the world, but to make software that meets your own criteria for quality. "Real life" modeling was oversold by some OO fans and still gets us in trouble now & then.

As for Liskov, just google for "Liskof substitution" and you'll find many references. It's a tough rule to follow all the time, but it's good to try.

Layne Lund
Ranch Hand
Posts: 3061
Originally posted by Stan James:
Wow, at 17 I had almost mastered the four function calculator. Of course they were brand new and cost \$400. Keep coding, keep posting, keep it fun. Sorry if my post went off the complexity deep end!

You actually HAD four function calculators back then?

I thought you were still carving everything in stone

Layne
[ May 10, 2005: Message edited by: Layne Lund ]

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
My senior year of HS my dad got an HP programmable calculator for his college teaching. About the size of a typewriter with a 4-register stack and a thermal writer like a bad cash register. I turned in physics homework with these smelly little listings stapled to it and got some interesting comments. The same functionality fit in a shirt pocket the next year, I think.

Oh, geez, I just realized what a bad analogy that was. You probably haven't ever seen a typewriter either!!
[ May 10, 2005: Message edited by: Stan James ]

Layne Lund
Ranch Hand
Posts: 3061
Originally posted by Stan James:
My senior year of HS my dad got an HP programmable calculator for his college teaching. About the size of a typewriter with a 4-register stack and a thermal writer like a bad cash register. I turned in physics homework with these smelly little listings stapled to it and got some interesting comments. The same functionality fit in a shirt pocket the next year, I think.

Oh, geez, I just realized what a bad analogy that was. You probably haven't ever seen a typewriter either!!

[ May 10, 2005: Message edited by: Stan James ]

I'm not quite THAT young. My parents used to have a manual (not electric) typewriter.

Layne

p.s. In case the Internet dropped any sarcasm packets, I was being facetious (sp?) in my previous post
[ May 10, 2005: Message edited by: Layne Lund ]

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Iz kool, man. I respect you from your posts even if you are so darned young.