|
-
Sep 20th, 2004, 12:54 PM
#1
Thread Starter
Hyperactive Member
Problem designing simple class
Hello everyone, i'm having troubles deciding how I should design my class heirarchy. Im' supose to do the following:
Implement a superclass Vehicle and subclasses Car and truck. A vehicle has a position on the screen, utilizing the class java.awt.Point. Implement a method draw inthe class RandomDrawing. This draw method can take in different types of vehicles and paint car and truck. In RandomDrawing class, generate four trucks and sick cars with randomly generated postions for drawing. You must atke advantage of inheritance, polymorphism and genericity.
Well I got all the code working and the function draw implemented. This is my class hierarchy.
Code:
(RandomDrawing) <---superclass
^
|
|
(Vehicle)
^
|
|
..................
| |
(Car) (Truck)
RandomDrawing extends JFrame, and this is where all the code is implemented and the objects are painted in this class by using the draw method inside the paint method, such as....
Code:
public void paint(Graphics g, int x, int y)
{
}
public void draw(Vehicle v, int x, int y)
{
v.paint(jPanel1.getGraphics(), r.nextInt(sP.x),r.nextInt(sP.y));
}
//draw vechicles
public void paint(Graphics g)
{
Car c[] = new Car[6];
Truck t[] = new Truck[4];
for(int i = 0; i < 6; i++)
{
c[i] = new Car();
draw(c[i],r.nextInt(sP.x),r.nextInt(sP.y));
}
But, now that I think about it, Why is RandomDrawing the Superclass of Vehcile, a Vehicle isn't a RandomDrawing. So I tried to make RandomDrawing its own seperate class but I was confused on how this should work. If RandomDrawing is its own seperate class, should it still be where all the work is done? Should I still make RandomDrawing extend JFrame? Or should I have a seperate .java file called Main or somthing and let it do all the creation of windows and such...
Thanks for listening
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Sep 20th, 2004, 04:57 PM
#2
Hmm....
I think logically it would make most sense to have it this way:
RandomDrawing extends JPanel
RandomDrawing has member JFrame, which it uses as parent
RandomDrawing has member ArrayList, which stores Vehicle and subclasses
RandomDrawing has method draw that goes through the ArrayList, calling paint(Graphics g) on each Vehicle in it (must cast to Vehicle, of course)
Vehicle's subclasses implement paint(Graphics g) and use it to paint themselves.
So Vehicle would NOT derive from anything. It should be marked abstract, and the method paint should be marked abstract, too.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 21st, 2004, 09:21 AM
#3
Thread Starter
Hyperactive Member
We havn't learned about abstract yet, but that design does make alot more sense, i'll try that out and see how it works!
Thanks.
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Sep 21st, 2004, 10:52 AM
#4
abstract class
simply says that the class may not be directly instantiated. Only derived classes can.
abstract method
means that you do not intend to provide an implementation for a method in this class, so that subclasses have to override it.
All methods in interfaces are abstract by default.
A class that has one or more abstract methods (declared in itself or inherited and not overridden) has to be abstract too.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|