I have spent hours looking at forums and everyone mentions adding to a vector and they mostly use just 1-5 ect. I cannot find anything to help my particular problem. I am new to computer programming and am slipping behind. I will type the assignment I was given and attach what I've got so far. I'm done everything except for the sort method. Can someone please help me understand how to write this code. All I know is I should be using Collections and Comparators but don't know how to implement them. Please advise. ***Sorry it's so long, can't upload ZIP or Rar files ***
THIS JAVA PROGRAMMING ASSIGNMENT SHOULD HELP YOU PRACTICE AND
UNDERSTAND THE FOLLOWING:
- MASS DATA PROCESSING USING SIMPLE DATA STRUCTURES LEARNED
IN COMPUTER SCIENCE I, SUCH AS ARRAYS AND VECTORS
- CONTROL OF PERMANENT DATA STORAGE AND I/O USING FILES
- USE AND CONTROL OF GUI AND ITS LINKS WITH DATA PROCESSING
YOU ARE TO DESIGN A JAVA GUI APPLICATION TO HELP MANAGE CLIENTS'
DATA FOR A COMPANY.
FOR SIMPLICITY, EACH CLIENT'S DATA INCLUDE ONLY A NAME, AN ID AND
A PHONE NUMBER. DATA ARE STORED IN FILES AND CAN BE OPENED, READ,
DISPLAYED, EDITED AND SAVED FOR FURTHER PROCESSING IN THE FUTURE.
ARRAYS OR VECTORS ARE USED TO ORGANIZE AND MANAGE THESE DATA AFTER
THEY ARE INPUT FROM FILES.
A MENU IS REQUIRED TO HANDLE THE FOLLOWING FILE AND DATA OPERATIONS:
- EDIT (NAME/ID/PHONE)
- SORT (BY NAME/ID/PHONE)
- SEARCH FOR CLIENTS (BY NAME/ID/PHONE)
That's too much code to go through and try to figure out what it does and does not do, and why. I'm guessing the problem is with sorting the "clientData" Vector? If so, then your Client class needs to implement the Comparator interface. The important method is "compare", which compares two object of class Client and returns -1, 0 or 1, depending on which one is "larger" than the other one. (The "equals" method is not important, you can have it return false in all cases, and be done with it.)
Does this help?
(As an aside, if you're writing new code, don't use the obsolete Vector class - use ArrayList instead. But it'll work with Vector, too.)
Well, I don't want to read through all your code, but I don't want to leave you out in the cold either.
The basic idea is that any list of objects can be sorted, so long as you explain how to sort them. In other words, you define the natural order. Numerically by ID? Alphabetically by last name? Campaign contributions in descending dollar amounts? It's up to you. You put this logic into a class that implements the Comparator interface, in the method compare(Object o1, Object o2). Generally speaking, you only compare two objects of the same class, but that's not mandated by the interface. This method returns -1 if o1 should come first, 1 if o2 should come first, and 0 if it doesn't matter. For arcane (but important) reasons, you should also implement an equals() method that returns true if and only if compare() returns 0.
That's it. You put your objects in a List, call Collections.sort(List, Comparator) (giving it an instance of the comparator class you wrote) and bingo! Your list is sorted for you.
ETA: I think Ulf may have mixed up the roles of Comparable and Comparator here. You can either have Client implement Comparable and write a compareTo() method, or you can make a separate class that implement Comparator and write a compare() method to compare two Client objects. You're free to use whichever way seems easiest to you. I agree that you should use ArrayList instead of Vector unless you're being coerced!
Agree with Ulf and Greg. But it's not quite compare(Object o1, Object o2) in the Comparator interface. That is now described as Comparator<T> so you can instruct the compiler what T means and use the compare(T t1, T t2) method.
More details in the generics section in the Java Tutorials. Fortunately you can probably get away with simple generics in this instance without needing bounds and super or extends. That means you only need to read the generics bits you actually understand
Joined: Feb 11, 2009
Thanks for the feed back. Sorry for all the coding. Like I said, I'm new to it and didn't know what was relavent to show. I wanted to just attach the .java files in a zip but it wouldnt let me. And as for using Vectors, I know, it's annoying, but it's what my comp sci II teacher wants.
Joined: Oct 13, 2005
Welcome to JavaRanch and you're welcome
The java.util.Vector class is often regarded as a legacy class, but it's not actually deprecated. Probably best only to use methods which you can also find in the java.util.List interface; if you look in the link quoted, it says "specified by xxx in interface List". Don't use the constructor which takes capacityIncrement.