Results 1 to 5 of 5
- 02-16-2015, 12:06 AM #1
Member
- Join Date
- Jan 2015
- Posts
- 48
- Rep Power
- 0
Given a @Test case, why is toString giving error?
hi! Given a @Test case(JUnit), why is toString giving error? The Test case is
Java Code:@Test public void test_equals1(){assertTrue(new Exam("Richard","Carver",1,"final",100).equals(new Exam("Richard","Carver",1,"final",100)));}
Java Code:class Exam { private String firstName; private String lastName; private int ID; private String examType; private int score; public Exam(String firstName, String lastName, int ID, String examType, int score) { this.firstName = firstName; this.lastName = lastName; this.ID = ID; this.examType = examType; this.score = score; } public String getFirstName() { return this.firstName; } public String getLastName() { return this.lastName; } public int getID() { return this.ID; } public char getExamType() { char examTypeCasted = 0; examTypeCasted = examType.charAt(0); return Character.toUpperCase(examTypeCasted); } public int getScore() { return this.score; } public String toString() { return this.firstName + " " + this.lastName + " " + this.ID + " " + this.examType + " " + this.score; } public boolean equals(Exam e) { if(this.equals(e)) return true; else return false; } }
- 02-16-2015, 12:23 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Given a @Test case, why is toString giving error?
You have not indicated that your other issues (in other threads) have been resolved. Others may get tired of not getting feedback on their recommendations or help.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 02-16-2015, 01:00 AM #3
Member
- Join Date
- Jan 2015
- Posts
- 48
- Rep Power
- 0
Re: Given a @Test case, why is toString giving error?
Sorry, jim829, previous threads were very helpful. Thank you, folks! I'll change that in seconds.
- 02-16-2015, 02:52 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Given a @Test case, why is toString giving error?
Ok, Great! Now, your equals method looks like it recursively calls itself. This is not appropriate. A proper equals method uses Object as its type. Second, you need to compare specific state information of the class using == or equals as appropriate. Here is an example:
Java Code:class Foobar { int val; String str =""; // default value public boolean equals(Object o) { if (this == o) { return true; } if (o instanceof Foobar) { Foobar f = (Foobar) o; if (val == f.val && str.equals(f.str)) { return true; } } } return false; }
Regards,
JimLast edited by jim829; 02-16-2015 at 03:24 AM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 02-16-2015, 05:32 AM #5
Member
- Join Date
- Jan 2015
- Posts
- 48
- Rep Power
- 0
Similar Threads
-
toString() giving errors
By alexandra12 in forum New To JavaReplies: 2Last Post: 06-12-2012, 04:41 AM -
junit test case error
By acmohan in forum New To JavaReplies: 7Last Post: 07-12-2011, 01:13 PM -
JavaCompiler giving an error WITHOUT ide
By divs1210 in forum Advanced JavaReplies: 9Last Post: 04-09-2011, 01:04 AM
Bookmarks