Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Java...I'm stuck...

I'm working on a java assignment, but I'm having a hell of time getting it to work. Basically, we are supposed to implement a Generic Tree class, where you can insert nodes, remove nodes, etc. So I made a GTREE (generic tree) class, and a node class...but my problem is that I want the node class to be a "generic" type, so the header is something like:

public class node<type>

That way, I can have a node that will work for any type. But no matter what version of this I try, I always run into problems. I need to be able to have a compareTo() method for this node class, so that for example I can compare Integers, strings, etc. This is necessary for the GTREE class to make comparisons. But how can I ensure that the parameterized type for the node class has a compareTo method? It won't compile unless that is the case. Is there some way to say: if(objectX has method desiredMethod) ?

Sorry if that sounds confusing. I'm just having difficulty coming up with an appropriate design for this.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top