Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Method signatures question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method signatures question" Watch "Method signatures question" New topic
Author

Method signatures question

Daniel Lucas
Ranch Hand

Joined: Apr 24, 2006
Posts: 48
OK here's the question:

Consider the following class declaration.

public class SomeClass implements Comparable
{
// ... other methods not shown
}

Which of the following method signatures of compareTo will satisfy the Comparable interface requirement?

I. public int compareTo (Object other)
II. public int compareTo (SomeClass other)
III. public boolean compareTo (Object other)

(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III

My analysis:

OK so I'm given a class named SomeClass that implements Comparable (I've used implements ActionListener before, but never Comparable... I'll assume it compares two things)

So it needs a method signature that satisfies the Comparable interface requirement. First of all, I can't recall ever hearing about "method signatures" used as a term in my Java course. But I do remember seeing something that looks like methodName(Object other). From what I can see, the answer choices all involve two of three things: an Object, SomeClass (the class from above), and other. Answer choices I and II declare the method named compareTo an integer, while answer choice III declares the method compareTo as a boolean. I know boolean deals with true or false statements, and an integer is 0, 1, 2, 3, 4, etc. Now I need to satisfy the interface requirement, whatever the heck that is. So if I were doing this question on the AP Comp Sci A exam and being timed, I think at this point I would guess and put A, I only. Why? Because it looks right. I don't really have reasoning besides that. Am I right? *checks answer key* Yep! The answer is A. I guess this is just testing my knowledge of what a method signature looks like. Apparently I recalled correctly...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

I know boolean deals with true or false statements, and an integer is 0, 1, 2, 3, 4, etc. Now I need to satisfy the interface requirement, whatever the heck that is. So if I were doing this question on the AP Comp Sci A exam and being timed, I think at this point I would guess and put A, I only. Why? Because it looks right. I don't really have reasoning besides that. Am I right? *checks answer key* Yep! The answer is A. I guess this is just testing my knowledge of what a method signature looks like. Apparently I recalled correctly...


Unfortunately, you just happened to pick the right answer by pure luck. This is a test question on how well you know the Comparable interface -- not on whether you know what a method is...

To answer the question, take a look at the java.lang.Comparable interface -- either using the source code or the Java Doc.



Choice A is correct because it matches the interface definition. Not because it looks closest like a method signature.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Daniel Lucas
Ranch Hand

Joined: Apr 24, 2006
Posts: 48
Well, now I know about the Comparable interface...

How would I go about finding the answer using the source code or the Java Doc? What is the Java Doc? EDIT: nevermind, it's on the Sun website.
[ April 30, 2006: Message edited by: Daniel Lucas ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method signatures question
 
Similar Threads
Confused about use of compareTo()
CompareTo and Inheritance question
ID:8 SCJP Question of the day !!!
Design Review part III