Solved

How do I create an object to hold 10 people in the main method using arrays?

Posted on 2008-10-21
11
191 Views
Last Modified: 2013-11-23
Hi all,
   Okay I have been working on this program for hours and I'm stuck right at the end. I am writing a program that will create an address book using two classes. I have both classes fine, but in the second class, the one that deals with arrays, I am finding some trouble. The requirement for this specific part I am having trouble with says: create an object book, to hold 10 people. I tried to declare the size of my array to 10 in main but its just not working. I know you're supposed to declare the arrays size when you declare the array, but why then would I need to create an object called book to hold 10 people?

I will be adding a scanner at the end, that's why the array has nothing in it right now.  

But basically I know that the "people[] = new book[10];" is wrong.. I just don't know what to do. Do you think I am misunderstanding the question, or am I just missing on little thing?

Thanks in advance for all your help!!! I appreciate it!!!
/***Here is the first class***/

public class Person

{

    private String firstName;

    private int yearBorn;

    private String phone;

    

    public Person()

    {

        firstName = "";

        yearBorn = 0;

        phone = "";

    }
 

    public Person(String firstNameX, int yearBornX, String phoneX)

    {

        firstName = firstNameX;

        yearBorn = yearBornX;

        phone = phoneX;

    }

    

    public void setFirstName(String firstNameX)

    {

        firstName = firstNameX;

    }

   

    public String getFirstName()

    {

        return (firstName);

    }
 

    public void setYearBorn(int yearBornX)

    {

        yearBorn = yearBornX;

    }
 

    public int getYearBorn()

    {

        return (yearBorn);

    }
 

    public void setPhone(String phoneX)

    {

        phone = phoneX;

    }
 

    public String getPhone()

    {

        return (phone);

    }
 

    public void print()

    {

       System.out.println("name=" + firstName + "\t" + "yearBorn=" + yearBorn + "\t" + "phone=" + phone);

    }

}
 
 

/***Here is the second class***/
 

public class AddressBook

{

    Person[] people;
 
 
 

    public void addPerson(Person newPerson)

    {

        for(int indexPeople = 0; indexPeople<people.length; indexPeople++)

        {

            if(people[indexPeople] == null)

            {

                people[indexPeople] = newPerson;

            }

        }

    }
 

    public void removePerson(String firstName)

    {

        for(int indexPeople = 0; indexPeople<people.length; indexPeople++)

        {

            String[] newPeople = new String[people.length];

            if(newPeople[indexPeople] == firstName)

            {

                people[indexPeople] = null;

            }

        }

    }
 

    public void print()

    {

        for(int indexPeople = 0; indexPeople<people.length; indexPeople++)

        {

            System.out.println(people[indexPeople]);

        }

    }
 

    public static void main(String[] args)

    {

        AddressBook book = new AddressBook();

        people[] = new book[10];
 

    }

}

Open in new window

0
Comment
Question by:ccsk89
  • 6
  • 5
11 Comments
 
LVL 31

Expert Comment

by:Frosty555
ID: 22773428
You're almost there, you have the basic idea:

Person class - should be responsible for identifying a single person. Provides storage for the person's first name, year of birth, phone number etc.

AddressBook class - responsible for holding a collection of 10 people using arrays. It should include methods for adding and deleting people.

But your main() method isn't part of the addressbook class, necause your program USES an addressbook. Your program should be it's own class. E.g.

class MyAssignment
{

    public static void main(...etcetc...)
    {
            // create an address book object

            // create a person object
            // fill persson object with data provided by user
            // add the person object to the address book
            //    .... rinse and repeat
    }

}
0
 

Author Comment

by:ccsk89
ID: 22773466
Forgive me, but I'm totally confused. Isn't my main method located in the AddressBook class?  Are you saying that I need to add another class just for the main method?

Thanks again for your help!
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 22773514
It looks to me like your main() method should not be located in the AddressBook class.

Your "AddressBook" class and your "Person" class are both helper classes which your program is using. So your program should be external to both the addressbook, and the person class.

If your main method was inside the AddressBook class, and you made a new instance of the AddressBook class inside your main method.... you can see how that is circular in nature. There are some rare instances where that is appropriate, but this is not one of them.
0
 

Author Comment

by:ccsk89
ID: 22773558
Okay I get what your saying now and it makes sense. The actual requirement states, "Create AddressBook object book, to hold 10 people" so I know it has to be in that class. would this rare instance be okay in this matter if it has to be in this class?
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 22773608
I really don't think it should be the same class. Essentially you would just be creating a public array to hold the people, and then a few public to add and remove from that array. Since your main() method is static, your arrays and functions would also have to be static. Suddenly you don't have any objects anymore except for the few instances of the Person object.

Your assignment asked you to create and implement the AddressBook class. But once you've created said class, something has to USE it. And that is what your program is. That's why it is a separate class. You are satisfying the requirements of your assignment, but in addition you're creating a small program that tests that your AddressBook class actually works.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:ccsk89
ID: 22773622
Right.... ok so how would I go about doing that then? this object "book" will be a new array and I set it equal to the "people" array?
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 22773650
No, leave the people array inside of the AddressBook class. It is hte ADdressBook class's responsibility to manipulate the array, add people, delete people etc.

Then your main program makes a single instance of the AddressBook class, and uses it. This is what the "book" object is. It isn't an array, it's a single instance of the addressbook class.

Then, your main program will create various instances of the Person class, fill them with data, and then make multiple calls to book.addPerson or book.removePerson in order to add or remove people. You'll probably want to do that in a loop somewhere.

e.g.

publi static void main(...)
{

     AddressBook book = new AddressBook();
     
     Person tom = new Person("Tom", 1986, "123-123-1234")
     Person mary = new Person("Mary", 1942, "123-123-1234")
     Person henry = new Person("Henry", 1999, "123-123-1234")

     book.addPerson(tom);
     book.addPerson(mary);
     book.addPerson(henry);
}
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 22773652
I do see some bugs in your addPerson() and removePerson() functions that you'll need to kink out. Remember, those functions are only responsible for adding or removing a SINGLE person from the array inside your AddressBook class.
0
 

Author Comment

by:ccsk89
ID: 22773683
Oh okay... so this "....to hold 10 people" has nothing to do with this object I am creating. I had the object created all along right? But if it says "..to hold 10 people" should I declare the size of the person array or just leave it without a size declared?

Thank you again for all your help!
0
 
LVL 31

Accepted Solution

by:
Frosty555 earned 500 total points
ID: 22777710
Right. The AddressBook class is what is responsible for the "holding 10 people" bit.  I suppose the easiest way to do this is to declare the array as 10 elements long always, and then keep track of what the "last used index" was.

E.g.

      people[10]     -    array of Person objects
      nextIndex = 0   -    last index, starts at zero

      PERSON GETS ADDED:
             people[lastindex] = [the new person object]
             lastIndex++;

      PERSON GETS REMOVED:
             See if you book has an example of how they want you to implement removing
             items from an array. If it does, use that system. But one way you can do this is:

             1) Find the index into the array where the person is. I'll call that "x"
             2) Shuffle everyone down from that index onward. E.g. if index "6" was to be removed, then index 7 overwrites index 6, index 8 overwrites 7, and 9 overwrites 8. Then 9 gets set to null.
              3) Finally, the lastIndex variable gets decremented.
0
 

Author Comment

by:ccsk89
ID: 22780966
Alright.. I finally understand it! Thank you so much, you've been a big help! I'll work on it and see how it goes. If I have another question I'll post a new question.

THANK YOU THANK YOU THANK YOU!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now