I am having much difficulty getting this program to sort the DVD titles. Could someone please point me in the right direction or show me where or what I am doing wrong. The code will not compile due to errors related to the sorting code. I have read a bunch of information and am not sure where to go from here.
I see five errors that are simple syntax issues. You should follow what the compiler tells you the errors are.
As for the error on Collections.sort(List);, the API for Collections.sort(List<T>) explains what is required of the elements of the list in order for the list to be sorted via this method. DVD doesn't meet these requirements. [ May 09, 2008: Message edited by: Stevi Deter ]
There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
The compiler tells me that it cannot find the symbols for line 15 and 16 or class DVDProducts which is: String title1 = products[i].gettitle(); String title2 = products[j].gettitle();
I don't have a clue as to where else there should be a symbol in the code. I have checked this against a number of examples and I am baffled.
After you finish getting all the bugs out and compiling your program, here is some more advice:
I see you used encapsulization (making your class variables private and get and set methods public) but in the case of your program I think you are using encapsulizing several times where you do not need to. For example, you have this code:
public void setTitle(String title) { this.title = title; }// end setTitle public String getTitle() { return title; }//end getTitle
Ask yourself, what is the difference between these two statements? myProduct.title = "Killer Martians"; myProduct.setTitle("Killer Martians");
When I encapsulize, I do it in order to protect my class variables from being set to something I don't want them to be set to, for example:
class DividingTime{ private int divideByThisInt = 1;
public void setTheInt(int newInt){ if (newInt != 0){ divideByThisInt = newInt; } }
public int theAnswer(anInt){ return anInt / divideByThisInt; }
}
With this class, divideByThisInt should not be zero, because then theAnswer method would result in an error. So I make divideByThisInt private so that no one can make it zero. In your program, I see no reason to make title private, because anyone can change it to anything. If I were you, I would either make it public and get rid of the get and set methods (making your program easier to read), or I would think about what kind of values I would not want title to have. For example, I think you probably don't want to have a title that is null or a string with no characters. So you should do this:
public void setTitle(String title) { if (title != null){ if (!title.equals("")){ this.title = title; } } }// end setTitle
Kevin: Thank you for the advice and examples. I will redo based on your suggestion. One thing though. Because of these problems I have searched the internet for examples and such and found a program that mirrors my second code post. I corrected my code. Copied and ran their program which it did as one would expect. However, mine does not compile and run. I don't understand that. I have checked and double checked for errors. Case, etc. and I cannot find anything. Be that as it may, I will do what you are suggesting and see what happens. Thanks again all of you.