Question: 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

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.

Refinements to the Person Class

You will use the Person class that was created in assignment #5 and add static methods and member variables as described below.

(Sorry for the caps its in the instructions lol)

NOTE: IN ORDER TO ENCOURAGE ENCAPSULATION AND INFORMATION HIDING, YOUR PROGRAM SHOULD NOT ALLOW ANY MEMBER VARIABLES TO BE DIRECTLY ACCESSED FROM OUTSIDE THE CLASSTHUS THESE MEMBER VARIABLES MUST BE PRIVATE. ANY ACCESS TO THE PRIVATE MEMBER VARIABLES FROM OUTSIDE THE CLASS WILL BE DONE THROUGH THE ACCESSOR METHODS, WHICH WILL BE PUBLIC. THIS INCLUDES BOTH INSTANCE AND STATIC MEMBER VARIABLES.

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. There will also be static methods for the following purpose:

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 #5.

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 #5, 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.

The Application Class

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.

SAMPLE RUN

The following outputs give an idea of the expected behavior of your application. 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:

Choices are:

(1) Add new person

(2) List People

(3) Display Information about a person

(4) Display average age of people

(5) Quit

What is your choice?

XXX

***Invalid Choice, please try again

Choices are:

(1) Add new person

(2) List People

(3) Display Information about a person

(4) Display average age of people

(5) Quit

What is your choice?

|

If the user chooses to add a new person, prompt for firstname, lastname, age, gender, and marital status.

What is your choice?

1

Enter te first and last am of the person

mary washington

Enter the person's age

35

Enter the persons marital status

married

Enter the person's gender

female

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: Person added OR Insufficient space to add person.

If the user chooses to list the people, your main method will call listPersons and display the resulting string:

What is your choice?

2

People List

Mary Washington

Ralph Boskins

Sandy Markus

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:

What is your choice?

3

Enter the first and last name of the person to search for

mary washington

Person Information

Name: Mary Washington

Age: 35

Gender: female

Marital Status: married

But if the person does not exist, and findPerson returns a -1, your main method should display an error message such as Person not found.

If the user chooses the average age, the main method should call averageAge and display a message that shows the resulting value:

What is your choice?

4

Average age is: 43.33333333336

If the user chooses to quit, say goodbye and terminate the application.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!