Gil Shoam

Greenhorn
+ Follow
since Dec 21, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Gil Shoam

Thanks for your answer but it didn't really answer my question.
With try/catch I define what happens in case of an exception. (for example i can print "an exception occured"). In this case (main throws) what actually happens? what does the program do?
13 years ago


in the above code, where is the Exception actually handled? It just keeps getting thrown...
isn't there supposed to be a try/catch somewhere to define what happens if the Exception is thrown?
I know that if I would delete the "throws" from the main, I would be forced to try/catch, but if main throws where is it caught?
Thanks for your time
13 years ago

Jeff Verdegan wrote:Put the code in a method.



isn't there a "redo case(a)"
or something similar?
13 years ago
Hi,

I'm trying to do an inner loop using the switch option.
I want to do something like this:

I know I can just copy paste the code.. but is there a more elegant way to do it?

Thanks

13 years ago

Stephan van Hulst wrote:

Why are you using such a method anyway? Why not provide a constructor that takes an array?



You are right.. that is what I meant to do.....

(it's 2:30 a.m here... )

but anyway can you elaborate on this:

Stephan van Hulst wrote:Because you are redeclaring the type parameter E in your method.

13 years ago
Hi,

I'm trying to implement a generic class used to define a set of different types. I'm trying to add a constructor that stores an array into the set. for some reason the add method does not work. (The compiler says: "Add (E) in arrayList can not be applied to (E)"

Here is my code:

Any idea why It won't add the elements?

Thanks
13 years ago
I'll clarify: (the Object identifies and translates 'A' into new Character('A'), so that is not the problem)

this is the Node class:


it prints :
A
A
true

meaning it compares two different nodes and 'equals' checks if they have the same name.

this is the Edge Class:

prints
(C,D)
(C,D)
true

so here the equals also works.

in the graph class i did this:


it prints:
Nodes: [A,D,B,A,C]
Edges: [(A,B),(B,C)(A,C)]
nodes1: [A,C,B,D]
Edges 1:[(A,B),(B,C)(A,C)]
false
true

so there are two problems here.
1) How can I add another 'A' to Nodes? a hashset isn't supposed to allow it. i.e it has two similar elements. I know it sees them as different because it is looking at the 'Node' wrapping.
2) If I erase the extra 'A' from Nodes, and to the equals method again (which currently only checks the _nodes)
it still returns false even though they have the same elements in the set.
the last line gives 'true' because the set has wrapped the Characters with the nodes, so I guess the equals method is comparing that. (So these 2 problems are actually the same I think..?)
That is where I am stuck..
Hope this helps...
13 years ago
Hi,
I'm writing a program that represents a graph.
I have 3 classes.
1. a Node class (which takes an object)
2. a Edge class (which build an edge from a source Node to destination Node)
3. A graph class. Here it is:


I'm trying to override the equals and hashcode methods to check if two graphs are similar.
meantime I wrote it just to check the nodes but with no luck:

when I tried

I got:
false
true

in other words I wasn't able to implement the method for the actual Node which contains a char, but only for it's wrapping. for the same reason I am able to add two similar char to the _nodes hashset (which should in theory not be possible). I did so and the did toString and got
Nodes: [A,B,D,A,C]
So it sees is as two different object although I want it to see the two A's as the same (and then it won't add another A right?)
I understand what the problem is, I just don't know how to solve it.

BTW, I wrote equal methods in the Node and Edge classes, both work fine. (in both cases i based the methods on the toString result)
Here:

prints:
A
A
true

and in Edge:

returns:
(C,D)
(C,D)
true


Please Help, and sorry for the long post

Thanks

Gil
13 years ago
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
13 years ago

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
13 years ago

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?
13 years ago
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)
13 years ago
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
13 years ago