|
-
Apr 23rd, 2007, 09:55 PM
#1
Thread Starter
Admodistrator
Method available to all instances of class?
I remember reading about a type of method that all objects of that class can access, and it is the same for all of them. Maybe I read wrong. Or maybe this is more of a code setup question.
Anyways, we are writing a bookStore class, it lets you choose any book you like by author or title and you can sort all the books by title or author. My trouble begins when I try and sort by something other than Title, because I have that set in code. We are extending Comparable in the Book class, and using CompareTo to sort the books. It works perfect if I just want to sort by title, but what about author?
I know I could set a variable in each object of Book, but thats just wasted memory when I feel there is a better solution
I hope you can understand my question.
-
Apr 24th, 2007, 09:33 PM
#2
Lively Member
Re: Method available to all instances of class?
I remember reading about a type of method that all objects of that class can access, and it is the same for all of them. Maybe I read wrong. Or maybe this is more of a code setup question.
I think you are referring to a static method, thats the only thing that comes to my mind.
Anyways, we are writing a bookStore class, it lets you choose any book you like by author or title and you can sort all the books by title or author. My trouble begins when I try and sort by something other than Title, because I have that set in code. We are extending Comparable in the Book class, and using CompareTo to sort the books. It works perfect if I just want to sort by title, but what about author?
Yeah, I see that problem to. So you hard coded the compareTo method to compare by title right? The only direct way I see of doing this is by having a static int in the Book class that says what to sort by and in the compareTo method just figure out what the value is and sort by that. I really can't think of a good way (OO way) to do this since you have to follow the method prototype for compareTo and adding extra parameters would be a hassle.
-
Apr 25th, 2007, 12:41 AM
#3
Thread Starter
Admodistrator
Re: Method available to all instances of class?
 Originally Posted by fartman_900
I think you are referring to a static method, thats the only thing that comes to my mind.
Yeah, I see that problem to. So you hard coded the compareTo method to compare by title right? The only direct way I see of doing this is by having a static int in the Book class that says what to sort by and in the compareTo method just figure out what the value is and sort by that. I really can't think of a good way (OO way) to do this since you have to follow the method prototype for compareTo and adding extra parameters would be a hassle.
I ended up doing it a way I really didnt want to. Anyways heres how I did it:
I made a new class -- BookSort. In its constructor I added a parameter to sort by title,author,price. Then I added a sort method, that went through each book object and depending on the sort type, combined each wanted parameter into one string, eg: "J.K. Rowling Harry Potter 29.95". That would be for sorting by author, for sorting by title "Harry Potter J.K. Rowling 29.95". Then in the compareTo it would compare all the strings and now it works.
Really sloppy I guess, but I don't think there is much I can do about it.
-
Apr 27th, 2007, 08:48 AM
#4
Re: Method available to all instances of class?
The proper way to do this is to write a utility class that implements java.util.Comparator<Book> and compares according to the chosen attribute.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|