Results 1 to 12 of 12
Thread: toString
- 01-24-2012, 10:23 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
toString
Hi i must write (school) a toString method which prints a formula
The code in the public static void main:
The string output should be (in this case) something like:Java Code:Formula formula = new Exof (new False(), new Not(new And(new Or(new True(), new Atom(1)), new Ifthen(new Atom(2), new Ifonlythen(new Atom(2), new True()))))); System.out.println(formula);
0^¬((1∨A)∧(B→(B↔1)))
I think i need to make the toString method recursive, but don't know how to do this. Can someone please help me?
- 01-24-2012, 10:38 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: toString
Since it's formula that you are writing the toString() for, it might be an idea to show us what that looks like as the structure of that class will pretty much determine how you need to write this.
- 01-24-2012, 10:40 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
Re: toString
For example the Exof class at the moment:
Java Code:class Exof extends Code{ Formule veld1; Formule veld2; public Exof(Formule exof1, Formule exof2) { veld1 = exof1; veld2 = exof2; } }
- 01-24-2012, 11:09 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: toString
You know better than us how these things fit together.
What's a Formule?
Does that have it's own toString?
Can you use that toString()?
If not, then how does the above map to the code sample you gave above?
- 01-24-2012, 11:16 AM #5
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
Re: toString
Got it working with:
gives output:Java Code:public String toString() { StringBuilder result = new StringBuilder(); String NEW_LINE = System.getProperty("line.separator"); // formule weergeven // 1 Waar if (this instanceof Waar) { result.append("T"); } // 2 Onwaar else if (this instanceof Onwaar) { result.append("F"); } // 3 Exof else if (this instanceof Exof) { // exof formule afdrukken ^ Formule veld1 = Exof.veld1; Formule veld2 = Exof.veld2; result.append("(" + veld1 + " ^ " + veld2 + ")"); } // 4 Of else if (this instanceof Of) { // of formule afdrukken | Formule veld1 = Of.veld1; Formule veld2 = Of.veld2; result.append("(" + veld1 + " | " + veld2 + ")"); } // 5 En else if (this instanceof En) { // en formule afdrukken & Formule veld1 = En.veld1; Formule veld2 = En.veld2; result.append("(" + veld1 + " & " + veld2 + ")"); } // 6 Alsdan else if (this instanceof Alsdan) { // alsdan formule afdrukken -> Formule veld1 = Alsdan.veld1; Formule veld2 = Alsdan.veld2; result.append("(" + veld1 + " -> " + veld2 + ")"); } // 7 Desda else if (this instanceof Desda) { // desda formule afdrukken <-> Formule veld1 = Desda.veld1; Formule veld2 = Desda.veld2; result.append("(" + veld1 + " <-> " + veld2 + ")"); } // 8 Atoom else if (this instanceof Atoom) { // atoom afdrukken int atoom = Atoom.atoom; result.append("p" + atoom); } // 9 Niet else if (this instanceof Niet) { // negatie afdrukken ~ Formule veld1 = Niet.veld1; result.append("~(" + veld1 + ")"); } return result.toString(); }
(F ^ ~(((T | p2) & (p2 -> (p2 <-> T)))))
only problem is with Atoom: the first p2 should be p1 (because it's new Atoom(1)), which piece of code should i change to get the first p2 to be p2?
Atoom.java:
Java Code:class Atoom extends Code{ static int atoom; public Atoom(int x) { atoom = x; } }
- 01-24-2012, 12:01 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: toString
Um, that's a lot of "instanceof" all called on "this".
Surely it would be preferable to supply a toString for each of those different subclasses?
That would remove all of that stuff and stick the toString logic where it belongs.
- 01-24-2012, 12:05 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
- 01-24-2012, 12:19 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: toString
You have a parent class:
This has children that each define their own toString:Java Code:public class Parent { }
So when you call the toString() for that subclass it will use the correct toString():Java Code:public class Child1 { public String toString() { return "This is a Child1"; } } ... public class Child2 { public String toString() { return "This is a Child2"; } }
No "instanceof" involved.Java Code:void someMethod() { Parent p = new Child1(); System.out.println(p.toString()); // This will print "This is a Child1" Parent p2 = new Child2(); System.out.println(p2.toString()); // This will print "This is a Child2" }
- 01-25-2012, 01:04 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
Re: toString
thank you.
got 1 more question:
i got a class Exof:
and a class Niet:Java Code:class Exof extends Code{ Formule veld1; Formule veld2; public Exof(Formule exof1, Formule exof2) { veld1 = exof1; veld2 = exof2; } }
class Niet extends Code
If i want to use the exof1 and exof2 from Exof in a new method in Niet, when veld1.getClass() == Exof.class, how do i do this?Java Code:{ Formule veld1; public Niet(Formule niet) { veld1=niet; } }
- 01-25-2012, 02:03 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: toString
Pass the Exof object into the Niet method that needs it?
It's a pretty vague question.
- 01-25-2012, 02:31 PM #11
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
Re: toString
if i have this method in Niet:
this wont work because exof1 and exof2 are not given in Niet. So how can I use the exof1 and exof2 from a Exof object in the method above?Java Code:public Formule herbrandNormaalvorm() { Formule formule; if (veld1.getClass() == Exof.class) { veld1 = exof1; Formule veld2 = exof2; formule = new En(veld1,veld2); } }
Hope this is less vague.
- 01-25-2012, 03:02 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
toString
By juanvalentinequintana in forum New To JavaReplies: 2Last Post: 05-13-2011, 04:30 PM -
help with toString
By teardrop3903 in forum New To JavaReplies: 5Last Post: 04-28-2011, 02:39 AM -
toString
By justin23 in forum New To JavaReplies: 13Last Post: 05-02-2010, 02:44 PM -
toString
By luckyleaf95 in forum New To JavaReplies: 9Last Post: 02-11-2010, 08:52 AM -
Can i just use toString?
By cachi in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks