File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Polymorphism 2!?!!??! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism 2!?!!??!" Watch "Polymorphism 2!?!!??!" New topic

Polymorphism 2!?!!??!

Rob Shan Lone

Joined: Jan 03, 2004
Posts: 8
Ok , I'm really struggling with this...
this is the Shape class inheritance hierarchy I have to stick too. Am I on the right tracks or not I do not know?

I was unsure how to include twodimensionshape for is there any point? The instructions here state I only need to concentrate on two dimensional shapes so I won't even need a Threedimensionalshape class?
a. Limit yourself to two dimensional shapes such as squares, rectangles, triangles and circles.
b. Interaction with the user must be demonstrated.
c. Let the user specify the position, size and shape to be used in drawing each shape.
d. The user can specify many items of the same shape.
e. As you create each shape, place a Shape reference to each new shape object into an array.
f. Each class has its own draw method. Write a polymorphic screen manager that walks through the array sending draw messages to each object in the array to form a screen image.
g. Redraw the screen image each time the user specifies an additional shape. Investigate the methods of class Graphics to help draw each shape.
Here is the java code I got for each java file.
Showshapes class - interaction with the user

Square class

Shape class

Poly class
[ edited to improve code layout, making it narrower -ds ]
[ January 05, 2004: Message edited by: Dirk Schreckmann ]
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

If you haven�t looked at the Campfire Story How my Dog learned Polymorphism, be sure to take a look at it. It�s a nice (and funny) introduction to Polymorphism. Also check out the book "Head First Java" (see this thread) � it is an excellent book for learning Java an OOP.
Looking briefly at your class hierarchy, I would say you are on the right track. Even if you only need to include 2D shapes, I would include the TwoDimensionalShape Class like you have. It will allow you to add 3D shapes in the future (and even 5D shapes once we get that pesky Space-Time continuum thing figured out). Others may have some suggestions for you...
I don�t do much with Swing or AWT, so I�ll leave the specifics of your code to others as well.
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
I just completed a similar class hierarchy. Is there any reason you chose an interface over just abstract classes? Was that part of your instruction or did you make that decision, and if so - why?

David Crossett
-nothing important to say, but learnin' plenty-
I agree. Here's the link:
subject: Polymorphism 2!?!!??!
It's not a secret anymore!