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 Quadrilateral class
The Rectangle class
The Square class
The Paralleogram class
The ShapeGenerator class
With Best Regards,
Shyam Prasad Murarka
Make the hierarchy as deep (i.e., as many levels) as possible.
This means that you start with your superclass (Quadrilateral) and generalize one step at a time:
> 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
So, instead of everything extending Quadrilateral, Trapezium should extend Quadrilateral, Parallelogram should extend Trapezium, etc.
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 reallife 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 nonabstract with the appropriate constructor(s), right?
5. Once you start talking about nonright 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 ]
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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
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...
I had only ever heard of the first and third definitions, but I take it you're being "chiefly British."
Ryan
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 ]
There are only two hard things in computer science: cache invalidation, naming things, and offbyone errors
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.
With Best Regards,
Shyam Prasad Murarka
Ranch Hand
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
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 Quadrilateral class
The Parallelogram class
The MyRect class
The Square class
The ShapeGenerator class
Please guys if you notice anything wrong please inform me.
With Best Regards,
Shyam Prasad Murarka
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??
Ranch Hand
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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
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 ]
Ranch Hand
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 ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
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 4register 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 ]
Ranch Hand
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
