Forums Register Login

print a HashSet of objects

+Pie Number of slices to send: Send
Hi,
sorry if this should be in Java for beginners. I wasn't sure..

Anyway, I'm writing a program that deals with graphs of any primitive types.
I have 3 classes.
One represents the Nodes:

Another the Edges


And finally the Graph:


Just to test it I ran this Main:



Problem is as you can see, it prints the object address.
I actually need to write a String representation of the graph inc the nodes and edges.
i.e the output should be:
Nodes: {A,,B,C,D}
Edges: {(A,B),(A,C),(B,C)


(here I tried printing only the nodes to see if it works, but no luck)
Since the actual node is an object how can I downcast it to the type I initialized it with?
Or in general- How do I override object toString() method for the graph class?

Thanks in advance

Gil
+Pie Number of slices to send: Send
Welcome to JavaRanch Gil

So do you want to see Node's instance variable _object ? then override Object's toString method in Node class
+Pie Number of slices to send: Send
Fast reply

Not exactly, that was just a test to see if I can access the char from graph._nodes.

What I want is to write a String representation of the graph inc the nodes and edges.
i.e the output for the main (above) should be:
Nodes: {A,,B,C,D}
Edges: {(A,B),(A,C),(B,C)
+Pie Number of slices to send: Send
 

Gil Shoam wrote:Fast reply

Not exactly, that was just a test to see if I can access the char from graph._nodes.

What I want is to write a String representation of the graph inc the nodes and edges.



Then your Graph class needs to override toString() to pring out the nodes and edges how you want. And if you have classes for Node and/or Edge, they need to override toString to display themselves appropriately.
+Pie Number of slices to send: Send
 

Jeff Verdegan wrote:

Gil Shoam wrote:Fast reply

Not exactly, that was just a test to see if I can access the char from graph._nodes.

What I want is to write a String representation of the graph inc the nodes and edges.



Then your Graph class needs to override toString() to pring out the nodes and edges how you want. And if you have classes for Node and/or Edge, they need to override toString to display themselves appropriately.



That I know..

How do I do it? Since I don't know In advance what types the graph has.
i tried overriding the toString() in the Node Class using the StringBuffer but it does not make sense...
if I don't yet know what type it is how can I write a string method for it?
+Pie Number of slices to send: Send
 

Gil Shoam wrote:

Jeff Verdegan wrote:

Gil Shoam wrote:Fast reply

Not exactly, that was just a test to see if I can access the char from graph._nodes.

What I want is to write a String representation of the graph inc the nodes and edges.



Then your Graph class needs to override toString() to pring out the nodes and edges how you want. And if you have classes for Node and/or Edge, they need to override toString to display themselves appropriately.



That I know..

How do I do it?



That's a really broad question. You should be able to find tons of examples of overriding toString() with a simple web search. What did you try and where exactly did you get stuck?

Since I don't know In advance what types the graph has.



You don't need to. Graph's toString() just says something like:

except, of course, you'd do that in a loop that walks over the nodes.

The individual nodes are then responsible for their own toString(). If a node is just any old class, then it's up to the author of that class to provide a reasonable toString(). If, on the other hand, you have a Node class that contains data and pointers to other Nodes, then you could have its toString() simply return the data's toString().

So you could have Graph's toString() which calls Node's toString() which calls it's data's toString(), with each layer adding whatever structural stuff it knows about/is responsible for (commas, brackets, etc.).

i tried overriding the toString() in the Node Class using the StringBuffer but it does not make sense...



Not sure why you'd use SB. And without seeing what you tried and what problems it caused, I can't really tell you where you went wrong.

if I don't yet know what type it is how can I write a string method for it?



You don't. And you don't need to. It provides its own.
+Pie Number of slices to send: Send
 

So you could have Graph's toString() which calls Node's toString() which calls it's data's toString(), with each layer adding whatever structural stuff it knows about/is responsible for (commas, brackets, etc.).



I tired this in the node class and the output I got is the object address not the actual char:


I also tried wrapping it with the Character class but still the same problem...

Any suggestions?
Thanks
+Pie Number of slices to send: Send
 

Gil Shoam wrote:I tired this in the node class and the output I got is the object address not the actual char:



Because you're not storing the Character you pass in.



This says, "Totally ignore the obj argument that was passed to me, and instead create a new Object, and store that."

What you're getting it Object's hashCode, because you have stored an Object that you just created, not a Character that you passed in.
+Pie Number of slices to send: Send
I got it!

In the constructor I didn't use the obj I got. This solved it



Thanks for your help.

If I have more Questions related to this project should I keep posting on this thread or open a new one?

Thanks again,

Gil
+Pie Number of slices to send: Send


The first line is totally pointless. It creates a new object, and stores a reference to that object in the _object variable. Then, on the next line, you copy the reference value from the obj parameter into the _object variable, and that new Object that you created and never used becomes eligible for GC.

Just get rid of the first line.
+Pie Number of slices to send: Send
 

Gil Shoam wrote:ore Questions related to this project should I keep posting on this thread or open a new one?



New one, unless it's further clarification on a point that's already come up in this discussion.
Why is the word "abbreviation" so long? And this ad is so short?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 4468 times.
Similar Threads
Discussion on recursion, stack overflows, & compiler optimization for tail recurssion
Undirected graph help - using ArrayLists and Maps.
HashMap, Iteraring problem
abstract class constructors
How to check whether two trees have identical structure ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 03:02:57.