• Post Reply Bookmark Topic Watch Topic
  • New Topic

phonebook and array? help?  RSS feed

 
devin panchal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i need some help creating a phonebook (array).
ok, heres the code wat i got so far;
Phonebook class
public class PhoneBook
{
private int arraySize;
private Contact[] array;
public PhoneBook(int maxArraySize)
{
array = new Contact[maxArraySize];
arraySize =0;
}
public void addAContact(String name, int number)
{
Contact theContact = new Contact (name, number);
array[arraySize] = theContact;
arraySize++;
}
private int findContact(int arrayNumber)
{
int location = -1;
for(int i=0; i<arraySize; i++)
{
if(array.getArrayNumber() == arrayNumber)
{
location = i ;
}
}
return location;
}
//public void incAppearance(int arrayNumber)
//{
// array[findContact(arrayNumber)].addToAppearances();
//}
public void incGoals(int goals, int arrayNumber)
{
array[findContact(arrayNumber)].addToGoals(goals);
}
public void displayArray()
{
System.out.println("Player Played Scored\n");
for(int i=0; i<arraySize; i++)
{
System.out.println(array.getName()+"\t "+array.getAppearances()
+"\t\t"+array.getGoalsScored());
}
}

}
ignore the end bit, its has been added from my other group member. i edited this one, and got the above to compile.
Contact class
public class Contact
{
private String name;
private int arrayNumber;
private int goalsScored;
private int appearances;
public Contact(String aName, int aNumber)
{
name = aName;
arrayNumber = aNumber;
}
public String getName()
{
return(name);
}
public int getArrayNumber()
{
return(arrayNumber);
}
public int getAppearances()
{
return(appearances);
}
public int getGoalsScored()
{
return(goalsScored);
}
public void addToGoals( int score)
{
goalsScored += score;
}
}
ok. thats wat i got so far. it holds names and numbers, but i want to store them in an array. my other group memebers r wokring on other bits. i'm try to create an array, so;
-a new entry can be added to the phonebook,
- entry can be deleted, modifyed, etc.
- a search function. (which is the main bit)(using name and/or number)
please void the appearances, goal scores, as i have not implemented that code. i have not had time to edit that bit.
wat else can i do to make the code better? more effecient? make it a proper array?
thanx
dev
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you are asking. As far as I can tell, you have all the correct syntax to declare an array and initialize it:

You also seem to have the code to correctly add information to the array:

So what exactly are you having problems with here? I don't see anything obviously incorrect. It looks like you understand how to use arrays.
If I'm missing something here, please clarify your question. Include compiler errors, if any, as well as the output you get and an explanation of how it differs from what you expect.
Keep coding!
Layne
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Realize, when using an array (indicated with []), you will need to define the size of the array at compile time. So in essence, you will need to determine the maximum size (as you did with the parameter maxArraySize). This is not always the best solution because:
.
  • make the size too large, and you'll be wasting space
  • make it too small, and your users may get pissed when they run out of room in their phonebook
  • you'll need to "null" out empty indexes so you know which elements are empty
  • eventually your array will resemble a piece of Swiss cheese (certain elements will be empty, others full)
  • because of the "Swiss cheese" effect, you'll have to do a sequential search for an empty element every time you want to add a new object (you won't be able to simple add to the "end" anymore because the "end" may be occupied)

  • A better implementation would be to use a Hashmap (see the Java Collections API). Whereas the traditional array[] requires a sequential search (starting at 0 and going up to size - 1), the Hashmap allows you to perform a search in constant time. Also, adding and removing elements are simple operations. It would be helpful if you understand the basics of hashing, but it is not entirely essential.
    [ April 19, 2004: Message edited by: Jeffrey Hunter ]
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • you'll need to "null" out empty indexes so you know which elements are empty

  • This is one of several possible solutions. The code above opts to use the variable arraySize variable instead to keep track of which elements are used and the position to insert the next element.
  • eventually your array will resemble a piece of Swiss cheese (certain elements will be empty, others full)
  • because of the "Swiss cheese" effect, you'll have to do a sequential search for an empty element every time you want to add a new object (you won't be able to simple add to the "end" anymore because the "end" may be occupied)

  • This situation, also called fragmentation, is avoided in the above code since there is no "remove" operation in the above code.
     
    Jeffrey Hunter
    Ranch Hand
    Posts: 305
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    a new entry can be added to the phonebook,
    - entry can be deleted, modifyed, etc.
    - a search function. (which is the main bit)(using name and/or number)

    A indexing variable alone is insufficient to add an element to an array suffering from the "Swiss cheese" effect. Eventually the indexing variable will reach the end of the array, and you'll need some mechanism to identify where the next free slot is.
     
    Joel McNary
    Bartender
    Posts: 1840
    Eclipse IDE Java Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Jeffrey Hunter:
    Realize, when using an array (indicated with []), you will need to define the size of the array at compile time.

    While this is true in C, Java does not have this restriction. You are perfectly capable of creating arrays of dynamic size -- which is what is done above. The size of the array is passed in as a variable.
    I do agree with your analysis that a Map would be the better implementation, however -- unless, of course, if this is a school project and the instructor is expecting an array....
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Jeffrey Hunter:

    A indexing variable alone is insufficient to add an element to an array suffering from the "Swiss cheese" effect. Eventually the indexing variable will reach the end of the array, and you'll need some mechanism to identify where the next free slot is.

    I guess the original poster hasn't implemented the deletion/removal operation from the requirements. My comments were referring to the code. You are correct that such issues need to be considered in order to implement this operation.
     
    devin panchal
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi,
    thanx for all the replies. i appreciate the help.
    thanx
    devin
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!