Results 1 to 18 of 18
4Likes Thread: URGENT toString method help
- 08-16-2011, 02:38 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
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
.gif)
.gif)
.gif)
public void testConstructor()
{
assertEquals("John Smith (id 5643) has an account balance of 5000.5",
theCustomer.toString());
}
- 08-16-2011, 03:25 PM #2
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!
-
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.
- 08-16-2011, 03:29 PM #4
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
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.
-
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.
- 08-16-2011, 03:42 PM #6
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
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.
-
- 08-16-2011, 04:05 PM #8
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
@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,
- 08-16-2011, 04:11 PM #9
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.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-16-2011, 04:13 PM #10
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
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
- 08-16-2011, 05:05 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
This won't even compile, and it should be pretty clear why.Java Code:public void setBalance(float aBalance){ balance=aBalance; return name + "(" + "id" + id + ")" + " has an account balance of" + balance; }
- 08-16-2011, 05:11 PM #12
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
@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
- 08-16-2011, 05:35 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
So, did you do what KevinWorkman suggested and print that out so you can see what it is actually returning from the toString() method?
- 08-16-2011, 06:47 PM #14
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
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:
This tells me that you have it almost have it correct. You are soooo close!!!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)
- 08-16-2011, 07:37 PM #15
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
@JeffGrigg I just cant see where im going wrong
@tolls do you mean like System.out.println will work?
- 08-16-2011, 07:45 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Yes.
For both Strings.
- 08-16-2011, 07:57 PM #17
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-16-2011, 08:09 PM #18
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
JUnit inserts square brackets ('[' and ']') in the string values to show where the differences are. Removing the square brackets, we get...
Those two String values are not exactly the same. I can see three places where they differ. Can you?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>
Similar Threads
-
Printing my toString method
By Epidilius in forum New To JavaReplies: 4Last Post: 06-18-2011, 03:32 AM -
toString() method error?
By blueduiker in forum New To JavaReplies: 6Last Post: 02-07-2010, 03:19 AM -
Trying to write toString method
By curious in forum New To JavaReplies: 3Last Post: 10-28-2009, 02:43 AM -
toString() method
By 01allenh in forum New To JavaReplies: 2Last Post: 03-25-2009, 11:43 PM -
toString method
By apfroggy0408 in forum New To JavaReplies: 6Last Post: 01-31-2008, 04:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks