CIS 331 Programming Assignment #7 Refining The Person Class and Using It in An Application
CIS 331 Programming Assignment #7 Refining The Person Class and Using It in An Application
In this assignment, you will add static (class-wide) methods and data items to your Person class,
and you will create a separate application class that makes use of the Person classs features.
You will use the Person class that was created in assignment #6 and add static methods and
member variables as described below.
In addition to the instance variables you created in the previous assignment, you will add the
following class (static) member variables. First, you will have a static member variable called people,
which will be an array of references to Person objects. You will also have a static member variable
(initialized to a value of zero) called totPeople, which you will use to keep track of how many people you
have. Also, it would be useful to have a static member constant (MAXPEOPLE) as the upper bound for
how many people to allow in the arraythis can be used to set the size of the array.
1. listing the names of people in the array. This method can be called listPersons, and should loop
through the array listing the names of the people. It should return a String that contains the list of
all the person names.
2. finding a person in the array. This method can be called findPerson. It should take as a parameter
a single string containing the first name followed by a space followed by the last name of the
desired person. Then it should search the array, looking for a match. If it finds a match, the method
should return the index value for the array element that contains the matching person. If it cannot
find a match, the method returns -1 as a value. This is a typical linear search method, but note that
it should not matter if the case of the input name matches the case of the persons name. Thus, it
would be useful for this method to make use of your existing equals method that was created in
assignment #6.
3. a method creating a Person object and adding it to the array of people. This method can be called
addPerson. It will take as parameters the first name, last name, age, gender, and marital status.
This method will return a Boolean value, which will indicate whether the attempt to add a new
person was successful or not. The method should first check to see if the array of people is full. If
it is, the method should return false. Otherwise the method should create an instance of the Person
class using the overloaded constructor, assign the reference to the next available spot in the people
array, and increment the totPeople count, then return a true value indicating a successful add of a
person.
NOTE: in order to be flexible in the size of the array (and therefore in indicating the maximum
number of people to allow) make use of the constant member MAXPEOPLE for defining the
maximum number of people instead of hard-coding a number value throughout your code.
4. calculating the average age of all people in the array. You can call this method averageAge, and it
should return a double value.
5. a get- accessor method for obtaining a particular person from the array of people (you can call it
getPerson). The return type of this method should be Person (that is, it will return a reference to a
person object). It should take as a parameter an integer value. This value will serve as an index.
Your method should return the person in the array element of the people array that is indexed by
the parameter value. The reason you need a getPerson method is because that array, like all other
member variables, is private. getPerson will then be useable from outside the Person class to
retrieve one of the people from the array.
As with assignment #6, there should be NO user interaction in your Person class, even in the static
methods. All communication will take place between the method and the calling statements using
parameters and return values.
You will also create (in a separate .java source code file) an application class (you can call it
PeopleApplication) that makes use of your Person class. As an application, this class will of course have a
main method. You will present a menu of choices to the user (actually this can be done in a separate
menuChoice method). The menu of choices should include: (1) adding a new person, (2) displaying a list of
all people (3) displaying information about a particular person, (4) display the average age of the people in
the array, and (5) quitting the application. Make sure to validate that the user is giving a correct choice in
response to the menu, and handle any exceptions that may be generated because of erroneous inputs.
If the user selects option (1), your application program should prompt the user in order to obtain the name,
age, gender and marital status of the person, then call the addPerson method of the person class passing
these as parameters. If addPerson returns a false value (indicating that the array was full), display an error
message to the user, otherwise display a message indicating a successful add.
If the user selects (2), your application program should call the listPersons method, and display the string
value that was returned (this return value will be the list of peoples names.
If the user selects (3), you should ask the user to enter the full name of the person to find, then call the
findPerson method passing this value as a parameter. If the findPerson method returns a value of -1
(indicating that the desired person was not found), you should display an error message that indicates that
the desired person was not found. If it returns a positive value, you should use this value to obtain the
person at that index of the array (call the getPerson method to do this), and then call the personInfo
method (or whatever you called it in your Person class) passing it a true value in order to obtain the string
containing all information about that person. The string that returns from the personInfo method should
then be displayed to the user.
If the user selects (4), you should call the averageAge method and display its result.
You can choose whether you want this to use JOptionPane for input/output or standard input/output using
Scanner and Ssytem.out.print/println.
Deliverables:
You will send to Blackboard's digital dropbox a zip file with the package folder containing your source
code files (.java files) for both your Person and your PeopleApplication classes. In addition, please
modify the Person class UML diagram to indicate the static members, and include this in your zip file.
(NOTE: static members are indicated in UML by underlining the member name; this can be done by setting
the scope of the member to Classifier instead of Instance).
SAMPLE RUNTHROUGH
The following screens give an idea of the expected behavior of your application. Note that although I am
doing this using JOptionPane, you could choose to use standard I/O if you want. The actual displays may
not look exactly like this, but you should use this as a guideline.
Present a menu to the userensure that valid data is entered. Handle any exceptions or invalid choices with
an error message and allow the user to re-enter the menu choice:
If the user chooses to add a new person, prompt for firstname, lastname, age, gender, and marital status.
Then call the addPerson method, passing these values as parameters. addPerson will decide if there is
enough space, and if so instantiate a new person and add it to the array, then return a true valueif not it
will return a false value.
So, your main method should either display a confirmation or an error message:
OR
If the user chooses to list the people, your main method will call listPersons and display the resulting string:
If the user chooses to find a specific person, your main method will prompt for the full name (first and last),
and then call findPerson. If found,, the return value from findPerson is the index in the people array, so you
will call getPerson passing that value and for the resulting Person object you will call personInfo and
display the string that returns:
But if the person does not exist, and findPerson returns a -1, your main method should display an error
message:
If the user chooses the average age, the main method should call averageAge and display a message that
shows the resulting value:
If the user chooses to quit, say goodbye and terminate the application: