The Artima Developer Community
Sponsored Link

Java Answers Forum
Arrays

1 reply on 1 page. Most recent reply: Mar 2, 2002 8:00 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Arrays Posted: Feb 25, 2002 9:53 AM
Reply to this message Reply
Advertisement
the task of this assignment is to make a database so you can add to the database a faculty, staff, and student and list them. I am supposed to add methods to sort the database by year of birth, a method to remove a person from the databse by searching the name and then removing it.
now i did most of this and i am able to add person list all but i cant get the sort year method to work and when i used the remove person code i had it doesnt remove the person but it adds that person and says null. I am kinda lost now and i know its pathetic but if anyone could help I would be grateful and if now then its alright. heres the code i got so far....


public class Database
{

private Person[] personArray;
private int personCount = 0;



/**
* Create a new, empty person database.
*/
public Database()
{
personArray = new Person[20];
}

/**
* Add a person to the database.
* @param aPerson the person to add
*/
public void addPerson(Person aPerson)
{
personArray[personCount] = aPerson;
personCount++;
}





/**
* Sort the array by the year of birth.
*/
public void sortYear()
{
int i= 0;
int position = -1;
boolean found = false;

while (i < personCount && !found)
{
if (personArray.getYearOfBirth() > 1800)
{
position = i;
found = true;

}
i++;

}

}
/**
* Remove a person from the database.
* @param rPerson the person to remove
*/
public void removePerson(Person rPerson)
{
Person[] tempArray = new Person[personCount];
int nonMatch = 0;
//This will remove the matching item

for (int count = 0; count < personCount; count++)
{
if ((personArray[count] == rPerson))
{
tempArray[nonMatch] = personArray[count];
nonMatch++;
}
personCount--;
}


//This will place the good items back in the personArray
for (int good = 0; good < personCount; good++)
{
personArray[good] = tempArray[good];
}

}




/**
* List all the persons currently in the database on standard out.
*/
public void listAll ()
{
for (int i = 0; i < personCount; i++)
{
Person aPerson = (Person)personArray;
System.out.println(personArray);
}
}

}


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Arrays Posted: Mar 2, 2002 8:00 PM
Reply to this message Reply
You need to create a class called Person with any necessary data fields to hold the data on a given person, such as name, position, etc.
then in you Database program create and add Person objects to a Vector object Persons.

Vectors are easier to add elements to, delete elements from, etc. because they grow with data size.
class Person{
      String name = "";
      String position = "";
 
      public Person(String name, String position){
            this.name = name;
            this.position = position;
      }
}

Flat View: This topic has 1 reply on 1 page
Topic: VRML and JAVA Previous Topic   Next Topic Topic: Java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use