Results 1 to 12 of 12

Thread: toString

  1. #1
    Waarten is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Question toString

    Hi i must write (school) a toString method which prints a formula

    The code in the public static void main:
    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);
    The string output should be (in this case) something like:
    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?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default 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.

  3. #3
    Waarten is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default 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;
    	}
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default 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?

  5. #5
    Waarten is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: toString

    Got it working with:

    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();
      }
    gives output:
    (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;
    	}
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default 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.

  7. #7
    Waarten is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: toString

    Quote Originally Posted by Tolls View Post
    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.
    What do you mean? Can you give me an example?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: toString

    You have a parent class:
    Java Code:
    public class Parent {
    }
    This has children that each define their own 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";
        }
    }
    So when you call the toString() for that subclass it will use the correct toString():
    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"
    }
    No "instanceof" involved.

  9. #9
    Waarten is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: toString

    thank you.

    got 1 more question:

    i got a class Exof:
    Java Code:
    class Exof extends Code{
        Formule veld1;
        Formule veld2;
    	
        public Exof(Formule exof1, Formule exof2) {
            veld1 = exof1;
            veld2 = exof2;
        }
    }
    and a class Niet:
    class Niet extends Code
    Java Code:
    {
        Formule veld1;
    	
        public Niet(Formule niet) {
            veld1=niet;
        }
    }
    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?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: toString

    Pass the Exof object into the Niet method that needs it?
    It's a pretty vague question.

  11. #11
    Waarten is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: toString

    Quote Originally Posted by Tolls View Post
    Pass the Exof object into the Niet method that needs it?
    It's a pretty vague question.
    if i have this method in Niet:
    Java Code:
    public Formule herbrandNormaalvorm() {
    		Formule formule;
    		if (veld1.getClass() == Exof.class) {
    			veld1 = exof1;
    			Formule veld2 = exof2;
                            formule = new En(veld1,veld2);
                   }
    }
    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?

    Hope this is less vague.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: toString

    WHere is the Exof object that you want to get this data from?

Similar Threads

  1. toString
    By juanvalentinequintana in forum New To Java
    Replies: 2
    Last Post: 05-13-2011, 04:30 PM
  2. help with toString
    By teardrop3903 in forum New To Java
    Replies: 5
    Last Post: 04-28-2011, 02:39 AM
  3. toString
    By justin23 in forum New To Java
    Replies: 13
    Last Post: 05-02-2010, 02:44 PM
  4. toString
    By luckyleaf95 in forum New To Java
    Replies: 9
    Last Post: 02-11-2010, 08:52 AM
  5. Can i just use toString?
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •