Results 1 to 18 of 18
Like Tree4Likes
  • 1 Post By johnnyjava
  • 1 Post By KevinWorkman
  • 1 Post By johnnyjava
  • 1 Post By Tolls

Thread: URGENT toString method help

  1. #1
    johnnyjava is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default URGENT toString method help

    Hey guys im new to java and am having problems getting a Junit test class to pass as my tostring method wont work for me.Can anybody help me write the code to make this pass, this assignment is due tomorrow and i really need this to pass


    public void testConstructor()
    {
    assertEquals("John Smith (id 5643) has an account balance of 5000.5",
    theCustomer.toString());
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    That's not how this works. Please see the link in my signature on asking questions the smart way before you post again.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello and welcome to the java-forums. Please note that your urgency and grade are not our problem. Also we are more than happy to answer any specific questions you may have but are not here to write your code.

  4. #4
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    I would guess from looking at this that your instructor wants you to create a class, possibly called something like Customer, and that this class should include at least three fields. You would probably want to pass the values in as constructor parameters. And then assign field values in the body of the constructor method. Then implementing (overriding) the 'toString()' method should be easy.

    I do hope that some instruction and/or reading assignment happened before giving you this homework.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure what the original poster is asking here myself: "having problems getting a Junit test class to pass as my tostring method". At least to me his problem is very unclear.

  6. #6
    johnnyjava is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    Sorry im new to the forum,so I am not used to how things go here,@ Fubarable im just unsure how to format tostring properly,i have been writing code to make a test pass.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by johnnyjava View Post
    Sorry im new to the forum,so I am not used to how things go here,@ Fubarable im just unsure how to format tostring properly,i have been writing code to make a test pass.
    I'm not sure how you need to format it yet either. Perhaps if you post your exact instructions we can clarify this for you.

  8. #8
    johnnyjava is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    @Fubarable.Basically my task was to write code to make a junit test pass.Ive have gotten everything to pass apart from Testing the constructor of the Customer class using the overridden toString() method.
    This is what is in the test class
    public void testConstructor()
    {
    assertEquals("John Smith (id 5643) has an account balance of 5000.5",
    theCustomer.toString());
    }

    to make it pass i wrote this to make it pass
    return name + "(" + "id" + id + ")"
    + " has an account balance of" + balance;


    but it still didnt work im just wondering where I have go wrong here,
    JeffGrigg likes this.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    Print out what you're returning from toString(). Compare that to the String in your assert. Check each character carefully. Line them up side by side or on top of one another if you have to.
    JeffGrigg likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    johnnyjava is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    this is the code i have written to make the test pass

    Java Code:
    public class Customer {
    private int id;
    private String name;
    private float  balance;
    
    public Customer(int anId, String aName, float aBalance        ){
    	this.id=anId;
    	this.name= aName;
    	this.balance=aBalance;
    }
    
    public void setId(int anId       ){
    	id=anId;
    	
    }
     public int getId(    ){
    	 return id;
    	 
     }
    public void setName(String aName){
    	name=aName;
    	
    }
    
    public String getName(   ){
    	return name;
    	
    }
    
    public void setBalance(float aBalance){
    	balance=aBalance; 
    	  return name + "(" + "id" + id + ")"
    	  + " has an account balance of" + balance;
    	  
    	
    }
    
    public float getBalance(){
    	return balance;
    	
    }
      public String toString(){
    	 
      }
    
    }


    this is the test




    Java Code:
    i m p o r t junit.framework.TestCase;
    /**
     * @ a u t h o r Agile Systems Ltd.
     *
     */
    p u b l i c c l a s s CustomerTest e x t e n d s TestCase 
    {
    /**
     * Customer reference variable storing reference to
     * Customer object. Used in all tests
     */
    Customer theCustomer = n e w Customer(5643, "John Smith", 5000.5f);
    /**
     * Testing the constructor of the Customer class
     * using the overridden toString() method
     */
    p u b l i c v o i d testConstructor()
    {
    assertEquals("John Smith (id 5643) has an account balance of 5000.5", 
    theCustomer.toString());
    }
    /**
     * Testing the setId() method. In the process
     * this tests the getId() method as well
     */
    p u b l i c v o i d testSetId()
    {
    theCustomer.setId(6000);
    assertEquals(6000, theCustomer.getId());
    } 
    /**
     * Testing the setName() method. In the process
     * this tests the getName() method as well
     */
    p u b l i c v o i d testSetName()
    {
    theCustomer.setName("Sean Smith");
    assertEquals("Sean Smith", theCustomer.getName());
    }
    /**
     * Testing the setBalance() method. In the process
     * this tests the getBalance() method as well
     */
    p u b l i c v o i d testSetBalance()
    {
    theCustomer.setBalance(200f);
    assertEquals(200f, theCustomer.getBalance());
    Last edited by Fubarable; 08-16-2011 at 04:17 PM. Reason: code tags added
    JeffGrigg likes this.

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

    Default

    Java Code:
    public void setBalance(float aBalance){
    	balance=aBalance; 
    	  return name + "(" + "id" + id + ")"
    	  + " has an account balance of" + balance;
    	  
    	
    }
    This won't even compile, and it should be pretty clear why.
    JeffGrigg likes this.

  12. #12
    johnnyjava is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    @tolls
    public class Customer {
    private int id;
    private String name;
    private float balance;





    public Customer(int anId, String aName, float aBalance ){
    this.id=anId;
    this.name= aName;
    this.balance=aBalance;
    }

    public void setId(int anId ){
    id=anId;

    }
    public int getId( ){
    return id;

    }
    public void setName(String aName){
    name=aName;

    }

    public String getName( ){
    return name;

    }

    public void setBalance(float aBalance){
    balance=aBalance;


    }

    public float getBalance(){
    return balance;

    }
    public String toString(){

    return name + "(" + "id" + id + ")"
    + " has an account balance of" + balance;








    }

    }



    i copied and pasted it wrong this is what i wrote but it still wont work

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

    Default

    So, did you do what KevinWorkman suggested and print that out so you can see what it is actually returning from the toString() method?

  14. #14
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    When a JUnit test fails, the message in the exception typically gives you important information about where the test failed, and why. So including the stack trace of the JUnit failure in a posting to this group would be very helpful.

    This is what I'm getting:
    Java Code:
    junit.framework.ComparisonFailure: expected:<John Smith[ (id 5643) has an account balance of ]5000.5> but was:<John Smith[(id5643) has an account balance of]5000.5>
    	at junit.framework.Assert.assertEquals(Assert.java:81)
    	at junit.framework.Assert.assertEquals(Assert.java:87)
    	at CustomerTest.testConstructor(CustomerTest.java:19)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at ... [blah, blah, blah] ...
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    This tells me that you have it almost have it correct. You are soooo close!!!

  15. #15
    johnnyjava is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    @JeffGrigg I just cant see where im going wrong
    @tolls do you mean like System.out.println will work?

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

    Default

    Yes.
    For both Strings.

  17. #17
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    Quote Originally Posted by johnnyjava View Post
    do you mean like System.out.println will work?
    What happened when you tried it?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  18. #18
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    JUnit inserts square brackets ('[' and ']') in the string values to show where the differences are. Removing the square brackets, we get...
    Java Code:
    junit.framework.ComparisonFailure: expected:<John Smith (id 5643) has an account balance of 5000.5> but was:<John Smith(id5643) has an account balance of5000.5>
    Those two String values are not exactly the same. I can see three places where they differ. Can you?

Similar Threads

  1. Printing my toString method
    By Epidilius in forum New To Java
    Replies: 4
    Last Post: 06-18-2011, 03:32 AM
  2. toString() method error?
    By blueduiker in forum New To Java
    Replies: 6
    Last Post: 02-07-2010, 03:19 AM
  3. Trying to write toString method
    By curious in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 02:43 AM
  4. toString() method
    By 01allenh in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 11:43 PM
  5. toString method
    By apfroggy0408 in forum New To Java
    Replies: 6
    Last Post: 01-31-2008, 04:08 AM

Posting Permissions

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