English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have a class, PhoneBook ,that instantiates an array of PhoneEntry objects. In phonebook, my teacher wants me to alphabetize the names as they come in. For example, you enter Bobby, it goes into book[0], next you enter Annie, after you enter Annie, i need it check to see if it less than the last entered(Bobby), which it is, so then the elements in the array need to move down, and Annie is inserted at book[0], and Bobby is now at book[1], i have a lessThan method in PhoneEntry, that checks to see if it less. So in the insert method of class Phonebook, i'll say if(book[current].lessThan) then perform the operation to insert it alphabetically. Does that make sense... i can post the code i have if neccessary. Any help is greatly appreciated

2007-02-19 10:33:14 · 4 answers · asked by Mike 1 in Computers & Internet Programming & Design

public void insert(String name)//inserts a new entry into the phonebook, let's user know
//if phonebook is full, and also organizes data when entered
{

if(isFull())
System.out.println("The Phonebook is full!");
else if(!isFull())
{

book[count] = new PhoneEntry(name);
count++;
}

2007-02-19 10:34:25 · update #1

I haven't entered anything yet to utilize the lessThan method yet.

The above is in Phone Book....the following is in PhoneEntry...

public boolean lessThan(PhoneEntry other)//determines if name is less than another name
{
if((this.getName()).compareTo(other.getName()) < 0)
return true;
return false;

2007-02-19 10:36:32 · update #2

4 answers

I hope you have well understood how your teacher wants you to keep the phone book sorted - ensuring the array is sorted even as the items are being entered - though doable is not the right way of doing it. If you want to insert the incoming items in their alphabetical place, then you should rather use a linked list instead of an array. It is much easier to insert an object anywhere in the list using a linked list as compared to doing the same in an array. Consider this example:

Your array has 30 items and the next item that comes in needs to be inserted at index=0, this would mean you will need to shift all the items to empty the first element.

Any way... if you still want to use array, you can choose to sort the array after inserting the item, by using Arrays.sort(book, String.CASE_INSENSITIVE_ORDER ) ;
where book is the array of entries.

If you still must go the tough route - then use this:
* Loop through the already added items in the book and compare the incoming item to each entry, until you find the appropriate place for the incoming item.
* Write another function e.g. insertItem(book, item, idx), this function will move items of book, from idx to last downwards and then assign book[idx]=item.

If u don't quite understand how to do this... email me your code at your_taurean@yahoo.com.

2007-02-19 11:17:03 · answer #1 · answered by SmartSpider 4 · 0 0

Instead of having a "lessThan()" method, make your PhoneEntry class extend "Comparable". Then you will need to add a function called compareTo(PhoneEntry other) which should return -1 if this is "less than" other, 0 if they are equal and 1 if this is "greater than" other. Here, you should choose to compare alphabetically and return the correct values.

Now, sorting is easy. Throw a bunch of your objects in an array (call it A), and now you just do Arrays.sort(A) and voila! It's an implementation of mergeSort build right into java.

2007-02-19 12:00:38 · answer #2 · answered by Zhuo Zi 3 · 0 0

Java has java.application.Scanner; that's one way. you progression for the time of the incoming documents finding for nextInt(), nextDouble(); the different ingredient you're able to do is study a line of text fabric at a time and then technique it. we are able to make a sized array to snatch the fields with String.chop up( "t" ); subsequently the tab character will chop up the line into array participants. if truth be told, it incredibly is what we did in the mainframe days with punchcards. Punchcards have been all text fabric. String s = ""; int count type = -a million; jointly as ( src.hasNext() ) { s = src.nextLine(); // you have a definition, type bill, already String[] flds = s.chop up("t" ); expenditures[ ++count type ] = new bill( flds ); } type bill ought to look as though this public type bill { inner maximum boolean undemanding; inner maximum double west; ... // the constructor public bill( String[] flds ) { undemanding = flds[0].equalsIgnoreCase("actual" ) ? actual : fake; west = Double.parseDouble( flds[ a million ] ) So, archiving products has many techniques. gaining wisdom of the text fabric report way is how a student can visualize the approach. a great number of circumstances use a text fabric report and usually they have the 1st line of the report with the kind of records.

2016-11-23 19:28:54 · answer #3 · answered by ? 4 · 0 0

As said, without knowing exactly what kind of coding is expected by the course I can say what you wrote so far is doable but no one who is doing it for real would do it the way you did so far; either a Linked List or some other utility classes within Java Collection would be used. String already implemented Comparable so you are not doing it 100% the way your professor wanted.

2007-02-19 11:53:36 · answer #4 · answered by Andy T 7 · 0 0

fedest.com, questions and answers