• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I test an object boolean method.  RSS feed

 
Tate Hsumsor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been writing test scripts for my class but I'm stuck on this method. how do i test for an object?

 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tate Hsumsor wrote:I have been writing test scripts for my class but I'm stuck on this method. how do i test for an object?




First, you can use the instanceof operator to test the instance to a class type -- not need to use the Class classes.

Second, once you determined the type, you can cast the object -- and at which point, you should be able to access the instance variables (and accessor methods).

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Thanks Roger. Have a cow.


To the OP... The ranch actually allows cross-posting. We just ask that you be forthright about it.... http://www.coderanch.com/how-to/java/BeForthrightWhenCrossPostingToOtherSites

Henry
 
Tate Hsumsor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Hnery

Henry Wong wrote:
Tate Hsumsor wrote:I have been writing test scripts for my class but I'm stuck on this method. how do i test for an object?




First, you can use the instanceof operator to test the instance to a class type -- not need to use the Class classes.

Second, once you determined the type, you can cast the object -- and at which point, you should be able to access the instance variables (and accessor methods).

Henry
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:First, you can use the instanceof operator to test the instance to a class type -- not need to use the Class classes.

It depends on what you want to test.
Tate's code will test if obj is an instance of the Fraction class.
Using instanceof will test if obj is an instance of the Fraction class or a subclass of Fraction.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tate Hsumsor wrote:I have been writing test scripts for my class but I'm stuck on this method. how do i test for an object?

Sounds like you're learning about writing equals() methods; and there's a LOT to know. According to this article by Martin Odersky:

"...after studying a large body of Java code, the authors of a 2007 paper concluded that almost all implementations of equals() are faulty"

Personally, I'm with Henry: use instanceof - it's more flexible, and it also saves you having to check for null (which I notice you haven't done) - but do digest Stuart's post as well.

If you're really interested, you might want to have a look at the FirstClasses page, which goes into the process in some detail (and shows you an alternate way of doing it, based on the above article) - but I warn you: it's not short.

Winston
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problems occur with instanceof if you have a subclass with additional fields used in equals().
You can get superObject.equals(subObject) and subObject.equals(superObject) returning true and false respectively, so you have breached the general contract of equals.

I suspect a major reason for faulty code is that teachers don't all know about the general contract and teach people to overload equals rather than overriding it.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . have a look at the FirstClasses page, . . . Winston
The class described there is marked final, so the problem with instanceof I mentioned earlier cannot occur.

There are three good references about equals which I know of: they are listed in this post. Read all three; they don't all cover exactly the same material. I think Odersky Spoon and Venners has already been mentioned in this discussion.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The class described there is marked final, so the problem with instanceof I mentioned earlier cannot occur.

Actually, that's just to start with; it also goes into the whole business of subclassing, and what it means for equals().

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Problems occur with instanceof if you have a subclass with additional fields used in equals().

Actually, problems occur with equals(), period. Using getClass() is a draconian solution (a "non-solution", IMO) to the problem, and needs to be clearly documented; otherwise you can run into LSP issues when dealing with collections.

@Tate: And if all this discussion seems quite advanced, it is. I'm afraid that equals() in an "objective" language just ain't simple.

Winston
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree: the Bloch reference describes problems with getClass() and the LSP, with classes much simpler than Collections.
 
Tate Hsumsor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all so much for your contributions Winston you're right my head was spinning a bit after reading all the replies, but I am looking at the links added in the responses and I am learning something new, I didn't know there were so many ways to write an equals method. So It's all very helpful...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tate Hsumsor wrote:but I am looking at the links added in the responses and I am learning something new, I didn't know there were so many ways to write an equals method. So It's all very helpful...

Glad you're finding it useful. Even if you don't understand them all right now, it'll certainly put you ahead of the game (at least as far as equals() is concerned ).

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!