Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2007-07-27 08:39:21

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Java inheritance problem

here's a problem i've been trying to work out for a while. Everytime i think i have a solution, something else gets in the way.

I am working on a program to help me develop symple 2d games. I have created a base class 'GameObject' for all the objects in the game which contains general methods and fields that apply to every game object. (such as a sprite, image file, x, y coordinates, speed) this class I made abstract and left the implementation of the methods update() and render() to the subclass.

This all works fine except i also have programmed a room editor which allows me to select from the list of objects i have created (fully defined subclasses of the general game object class) drop them onto a playfield or 'room' and then save the set up which basically saves a collection of constructor statements into a file for me to paste into a particular rooms creation code. It also allows me to reopen a saved room for editing.

However, this editor, obviously needs to access all the implementations of the GameObject class i have defined. And thats causing trouble. The easiest way so far, is to explicitely state what objects you can drop in the room editor. However, constantly reediting the room editor is getting obnoxious and bug prone.

Another complication is I need each subclass to have a static array that holds all the instances of the class in existance. However, i cannot define that in the base class or it will apply to every GameObject in existance. If I define it in the subclass, I could A. forget to do it, and i'd like the superclass to ensure its definiton, and B i'd have to redefine all methods that deal with that static array in the subclass. sad

I thought i had a solution until i remembered java does not allow static abstract methods.

So, any suggestions?


A logarithm is just a misspelled algorithm.

Offline

#2 2007-07-27 10:34:48

Ricky
Moderator
Registered: 2005-12-04
Posts: 3,791

Re: Java inheritance problem

However, this editor, obviously needs to access all the implementations of the GameObject class i have defined. And thats causing trouble. The easiest way so far, is to explicitely state what objects you can drop in the room editor. However, constantly reediting the room editor is getting obnoxious and bug prone.

Create an environment class responsible for managing all the objects in the program.  Then, for each object, have a state "canGoInRoom" or something of that sort.  Have the constructor set the default for each subclass (note the flexibility this allows).

Another complication is I need each subclass to have a static array that holds all the instances of the class in existance.

Why do you need it?


"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

Offline

#3 2007-07-27 11:33:46

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: Java inheritance problem

Collision detection. So I can check for a collision with any and every instance of that class.

As for the environment class, do you mean explicitly stating all the objects i've defined there so other classes can access them through that class?

Because... thats what i'm already doing. I think i called the class 'CurrentGameObjects' or something like that. So thats not an ugly or unprofessional solution?


A logarithm is just a misspelled algorithm.

Offline

#4 2007-07-27 12:37:57

Ricky
Moderator
Registered: 2005-12-04
Posts: 3,791

Re: Java inheritance problem

The environment class is responsible for everything that happens in your program.  Your main() should only be used for setting up the data that will passed into your environment constructor, then call a function like environment.run().

Environment controls everything other than setting up the data.  But it does so from a high level.  So you might have a function:

Environment CollisionTest()

And this iterates through every object combination calling object1.collision(object2).  You basically want to do as little as possible in the environment class, while still doing every major action.

Because... thats what i'm already doing. I think i called the class 'CurrentGameObjects' or something like that. So thats not an ugly or unprofessional solution?

If the class's sole purpose is to store a collection of the game objects, then you don't need it.  Use a data structure to do this.  Of course, you may wish to implement your own data structure, but call it by its name like PRQuadTree or BufferPool.  And implement in such a way that it will work with whatever type of object you wish to store within it.


"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

Offline

#5 2007-07-27 13:34:33

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: Java inheritance problem

grr.. I've not taken a data structures course yet. in fact I'm taking it next semester.

Could you explain a little about how that works?


A logarithm is just a misspelled algorithm.

Offline

#6 2007-07-27 13:45:02

Ricky
Moderator
Registered: 2005-12-04
Posts: 3,791

Re: Java inheritance problem

Data structures implement several basic functions:

Insert(Data)
Remove(Data)
Find(Data)

It can have more, but it depends.  For example, you could also have Sort(), or InsertToFront().  Some data structures don't have a front, in which case it wouldn't make sense.  Some data types aren't sortable, so you have to be careful in what functions you have and what you plan on doing with them.  How data structures work varies greatly.  Here are some data structures you may wish to look up, if you want any details on a specific one, just ask.

Array
Stack
Queue (Pronounced "Q")
Deque (Pronounced "deck")
Drop Out Stack
Hash table
Linked List
Doubly Linked List
Circular Linked List
Binary Tree
N-ary Tree
AVL Tree
2-3 Tree
BTree
B+Tree
PRQuadTree
Buffer Pool
Heap

The important thing to recognize is that all of these can implement

Insert(Data)
Remove(Data)
Find(Data)

But some are better than others, but only in certain ways.  Also, there are many ways to implement the same structure.  For example, when an array needs to grow, the typical thing to do is to double its size.  This is because growing an array is very costly, and you want to do it as little as possible without using too much more memory than you need to.  But you can vary it.  Another example is a doubly linked list.  They can be implement with or without something called a head and tail, which are dummy nodes that get rid of special cases in the algorithm.

The important key here is sacrifice.  In a hash table, you use up a lot of memory, but inserts, removes, and finds are extremely fast.  In a link list, you can insert and remove fast, but you can't really sort or find all that great.  In a B+Tree, it's not as efficient as an AVLTree, but there is less data moving around.  This is great when your data is stored on a disk, as moving data takes a long time there.


"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

Offline

#7 2007-07-27 14:20:51

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: Java inheritance problem

i learned a lot about searching and sorting arrays, inserting with arrays and vectors last semester. And most of those terms i have been lightly introduced to though i've not seen how they work internally.

well the problem that i saw with adding created objects to a list is that, say when i first open the room editor, no objects of that class exist yet. They have to be selected from the menu and then dropped onto the screen, but the objects name on the menu and the sprite it uses is defined in the actual class itself. So when the editor populates that menu, I either have to directly tell it what classes to populate, or get an array from some other place that does the same thing. (which is what I'm doing now)

I'm sorry if this is a little unclear because it has many different aspects to it that are hard to keep track of. I often have a hard time grasping the situation.

I may need to start over as I didn't plan it well enough on the outset.


A logarithm is just a misspelled algorithm.

Offline

#8 2007-07-27 15:34:55

Ricky
Moderator
Registered: 2005-12-04
Posts: 3,791

Re: Java inheritance problem

It sounds like you shouldn't be using inheritance for the class objects itself, but rather more of a template class.  Have a class Object (or some name) with fields such as:

String type;
Sprite sprite;
int X, Y;
etc.

Then you have the type/sprite associated with the menu, and when you create an object you simply call new Object(type, sprite).  If you follow this method, it would probably also be a good idea to have a unique id (integer) for each object.


"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

Offline

#9 2007-07-27 15:53:23

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: Java inheritance problem

hmm.. doesn't that break the rule of always using abstract classes that you mentioned in luca's thread?


A logarithm is just a misspelled algorithm.

Offline

#10 2007-07-28 01:54:30

Ricky
Moderator
Registered: 2005-12-04
Posts: 3,791

Re: Java inheritance problem

Abstract classes?  You should use wrapper classes if that's what you mean.  But that is only for data structures.


"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

Offline

Board footer

Powered by FluxBB