This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes SortNames Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "SortNames" Watch "SortNames" New topic
Author

SortNames

Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
With Marilyn on vacation, this seems to be a good time to work ahead. I have a working model of SortNames that sorts by first name. My problem is that I don't have a clue where to begin with sorting by last name. Does anybody have any good hints on where to begin?
Matthew Phillips

Matthew Phillips
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1747
    
    2
Note that I haven't been nitpicked for this assignment yet, so I'm just going by how I did things.
The assignment itself says "Use the Collections class for sorting." Check to see if the method you used for sorting can be used differently (for example, with a different set of parameters).
The "purpose" of the assignment also provides a clue.
Sorry to be so cryptic. I'm just trying not to give too much away.
[This message has been edited by Michael Matola (edited June 27, 2001).]
Amber Woods
Ranch Hand

Joined: Mar 28, 2001
Posts: 111
You will need to "implement an interface". It took me a while to figure out what that meant. I as well have not been nitpicked for this one yet, and I have three working versions but have not yet decided which is the best.
Good Luck!


"Happiness is a way to Travel, <b>Not</b> a Destination" -- Unknown
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1747
    
    2
I know what you mean about having several working versions of this assignment.
This one stumped me at first, until I made the discovery I think we're supposed to make, then it all fell together.
To me, this assignment, like NaturalLanguageMultiply ranks high on the way-cool scale. (As long as it turns out I did it them the right way!)
What I couldn't decide was which class should implement the interface. The solution I think I'll eventually submit has the interface implemented as an independent class. I have several versions going with inner classes (static, anonymous, etc.)
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
Actually looking at the source for the Collections class was of greate help to me. I followed the trail to several other classes which gave clues to what I needed to do for the assignment. If you don't have the source I highly recommend you get it.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Thanks for the hints. Once I found the right interface, I realized that I have been making this much harder than it really is.
Matthew Phillips
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
I have to confess I am totally stumped by this one. Don't even know how to begin thinking about planning a solution.......
Peter Gragert
Ranch Hand

Joined: Jan 16, 2001
Posts: 424
Sorting means you have to compare 'objects'.
So you could look at the API of Collections ?!
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1747
    
    2
Carol, I think the assignment itself provides a lot of clues on this one.
The designers of the Java collections classes built in the infrastructure to make this kind of thing easy for client programmers to implement. There's some detective work you'll need to do in the Java API (or the source code) to find the "hooks" that have been provided for you.
Then all you have to do is write the logic that determines what should come in front of what (or accept some default logic).
If you seem stuck with the assignment as a whole, maybe do it in chunks:
(1) Get the I/O stuff working, so you can read in the list and print it out (not worrying yet about the order).
(2) Get the list to print out according to the first sort rule.
(3) Get the list to print out according to the second sort rule.
Amber Woods
Ranch Hand

Joined: Mar 28, 2001
Posts: 111
Carol, another good thing to look into would be interfaces in general and how they work. I looked them up in sun's tutorial pages.
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Thanks everyone! I am struggling with the concept of reading in and writing .... I get very lost in language sometimes. Like reading in for example... Does this mean just reading the data in a file, or is it stored somewhere? Do you have to read it first and then write it??? Any way, I'll get the I/O thing working first, then worry about the sort. Oh- should the sort be in the original order of the list, or alphabetical, or ???
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1747
    
    2
The assignment says "Use com.javaranch.common.TextFileIn to read the names." and "Load an ArrayList with Strings from a text file of names."
Check either the JavaDoc or source code for TextFileIn
http://www.javaranch.com/common.jsp
for a great example of how to use that class.
Since ArrayList implements the List interface, you know that it will keep things in the order in which you put them (unless you make insertions, deletions, or sort it). So when you populate the ArrayList from the input file, the names in the ArrayList will be in the same order as in the input file.
As far as sorting is concerned, the assignment says "Show the names sorted in order of first name and then by last name without modifying the strings or the ArrayList."
[This message has been edited by Michael Matola (edited June 29, 2001).]
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
I have tried to write a simple program using ArrayList and I have hit a wall. Please see my post called "Sort this!!!"
How do you load elements into an ArrayList? I haven't run across any information on how to do that, so I decided to try and
create an ArrayList using the first several names on the list, and I couldn't get the add() method to accept the parameters it's supposed to accept. I'm missing something somewhere........
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Also- the word sort to me means to separate a pile of items into smaller, more organized piles of related items, so sorting a list would mean separating it into different, smaller lists. By sorting, do they mean dividing the list into a list of first names only and then a list of second names only keeping the order constant? I feel like I'm speaking a different language than the rest of you sometimes! I don't understand!!!
Please translate this assignment into simple English for me, so I know exactly what to do! ( I feel like an idiot right now....)
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Carol I also stuggled with this assignment.
I remember I found a good online tutorial on-line somewhere. I'll look around today of I can find the URL.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1747
    
    2
Originally posted by Carol Murphy:
I have tried to write a simple program using ArrayList and I have hit a wall. Please see my post called "Sort this!!!"
How do you load elements into an ArrayList?

The easiest way is to first create an ArrayList object, then call the add(Object o) multiple times for each element you want to add to the list.
add(Object o) puts each new element at the end of the list. add(int index, Object element) allows you to put things at specific locations.
I just used add(Object o) -- that's part of what I was talking about when I said to get the I/O stuff working first. First populate the ArrayList with the elements in the order of the output file, then worry about all the sorting business.
As far as calling add(Object o) multiple times -- take a look again at the sample code I mentioned for TextFileIn. This snippet of code first creates an input stream based on the specified file name. Then it reads through the stream and creates a String object for every line in the stream (file) and prints out that stream.
You can get this code to load Strings into an ArrayList by modifying just a single line. (Plus, change the filename.)
I haven't run across any information on how to do that, so I decided to try and create an ArrayList using the first several names on the list, and I couldn't get the add() method to accept the parameters it's supposed to accept. I'm missing something somewhere........

I saw in the other thread that you were creating Names objects. That seems like overkill. Why not just treat each line read from the input file as a String?

[This message has been edited by Michael Matola (edited July 01, 2001).]
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1747
    
    2
Originally posted by Carol Murphy:
Also- the word sort to me means to separate a pile of items into smaller, more organized piles of related items, so sorting a list would mean separating it into different, smaller lists.

What you describe I'd call "sorting into bins" or "sorting into buckets" or "grouping."

By sorting, do they mean dividing the list into a list of first names only and then a list of second names only keeping the order constant? I feel like I'm speaking a different language than the rest of you sometimes! I don't understand!!!
Please translate this assignment into simple English for me, so I know exactly what to do! ( I feel like an idiot right now....)

First show the list in alphabetical order by first name, then in alphbetical order by last name. Here's the output from when I run SortNames. Maybe somebody who's been nitpicked for this assignment can verify that the output is correct.
Abraham Lincoln
Adam Baum
Bill Joy
Connie Chung
James Gosling
Joe Montana
Justin Case
Walter Cronkite
Adam Baum
Justin Case
Connie Chung
Walter Cronkite
James Gosling
Bill Joy
Abraham Lincoln
Joe Montana
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1747
    
    2
Kinda reminds me of the mailboxes at my work. For about two years they were alphabetized by first name. It was confusing to find my stuff with all the other Michaels. (It was a fairly dynamic set of mailboxes because the project constantly had consultants rolling on and off.)
Then one day, just about the time I got used to the first name sort, they switched it all around to last names.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Carol Murphy:
the word sort to me means to separate a pile of items into smaller, more organized piles of related items, so sorting a list would mean separating it into different, smaller lists. By sorting, do they mean dividing the list into a list of first names only and then a list of second names only keeping the order constant?

Sort = alphabetize.
Alphabetize by first name and print the list
Alphabetize by last name and print the list again.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
THANK YOU!!!
I am still struggling with this, but I got an ArrayList to accept the names I added individually using add( Object o ) and then print them out using my ArrayList as the parameter. It printed out like this: [First Name, Second Name, Third Name]. I feel like I understand a little more about what I'm working with. Now I need to get something going where the specified list is read in from the original file. This is confusing to me as well, but I at least I understand what it is I have to do, even if I'm not sure how to do it yet! Thanks again for your help.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Michael Matola:
The assignment says "Use com.javaranch.common.TextFileIn to read the names." and "Load an ArrayList with Strings from a text file of names."

Check either the JavaDoc or source code for TextFileIn
http://www.javaranch.com/common.jsp

for a great example of how to use that class.

 
GeeCON Prague 2014
 
subject: SortNames