Question: Write Java code for a Baby class. A Baby has a name of type String and an age of type integer. Supply two constructors: one

Write Java code for a Baby class. A Baby has a name of type String and an age of type integer.

Supply two constructors: one will be the default constructor, which just sets default values for the name and age; the second constructor will take two parameters, a string to set the name and an integer to set the age. Also, supply methods for setting the name, setting the age, getting the name and getting the age.

The class should not contain I/O methods; use set method or constructor to set the value passed in as a parameter. The instance variables for output can be obtained using the get methods. The set method for the name instance variable should ensure that the value passed is not empty or contain whitespaces (otherwise set a default value). The set method for the age instance variable should validate the value passed to be between 1 and 4 inclusive (otherwise set a default value).

Give Java code for an equals method for the Baby class. Babies count as being the same (i.e. equal) if their names and their ages are exactly identical (names should not be case sensitive). The method will take a Baby type parameter and use the calling object (thus comparing these two objects via name and age); it should return Boolean - true or false as appropriate. Remember, if comparing Strings, you must use String comparison methods.

Test your Baby class by writing a client program which uses an array to store information about 4 babies. That is, each of the four elements of the array must store a Baby object.

If you have an array for baby names and another array for baby ages, then you have missed the point of the exercise and therefore not met the requirement of this exercise.

A Baby class object stores the required information about a Baby. So each Baby object will have its own relevant information, and thus each object must be stored in one element of the array.

The client program should: a. Enter details for each baby (name and age) and thus populate the

Baby array b. Output the details of each baby from the array (name and age) c. Calculate and display the average age of all babies in the array d. Determine whether any two babies in the array are the same

As the required information for these tasks is stored in the Baby array, you will need to use a loop to access each array element (and use the dot notation to access the appropriate set and get methods to assign/retrieve the information).

For part d above, a nested loop is required.

First add a new method (BabySound) in the class Baby in exercise 3 from Lab Practice 5, such that its purpose is to print the information Baby Sound is Neutral on the screen to show that Baby is normal.

Define a sub-class, Patient, which is used to store the record for a clinic. Derive Patient from the class Baby that you have created in this question, with its own BabySound method. A Patient record has the patients name and age (inherited from class Baby) and identification number (use the type Int). Provide necessary constructors and assessor methods, and an equal method for the Patient class. Patient is considered to be the same (i.e. equal) if their identification number is the same. In the Patient class, the method BabySound will print the information Baby Sound is Sick on the screen to show that Baby is sick.

Define another sub-class, Playgroup, which is used to store the record for a member of the playgroup. Derive Playgroup from the class Baby that you have created for this question. A Playgroup member record has the babys name and age (inherited from class Baby). Provide necessary constructors and assessor methods, and an equal method for the Playgroup class. Baby member is considered the same (i.e. equal) if their name and age are the same (Hint: Check if you already have the equal method in the parent class). In Playground class, create a method (BabySound), such that it will print the information Baby Sound is Happy on the screen to show that Baby is happy.

Write a client program to test the use of the classes. Create an array of Baby to store information of up to 4 babies.

The client program should:

1. 2. 3. 4. 5. 6. 7. 8.

Enter details for the patient (name, age and identification number) for the first baby array item this is a Patient object.

Enter details for the baby (name and age) for the second baby array item this is a Baby object.

Enter details for the baby (name and age) for the third baby array item this is a Playgroup object.

Enter details for the baby (name and age) for the fourth baby array item this is a Baby object. Output the details of the patient entered (name, age and identification number), and output the baby sound that show the baby is sick.

Output the details of the Playgroup member entered (name and age), and output the baby sound that show the baby is happy. Output all the information stored (including the baby sound) in the Baby array to a file (Baby.txt)

Implement the GenericQueue class from Topic 10. You can use ArrayList for this question. In the client class, use the GenericQueue class to create a queue to store Baby objects, Patient objects and Playgroup member objects. Modify your program for the exercise so that it repeatedly displays the following menu:

  1. Read a patient data: read the details of a patient from the keyboard and add the Patient object to the queue.
  2. Read a baby data: read the details of a baby from the keyboard and add the Baby object to the queue.
  3. Read a playgroup member's data: read the details of a playgroup member from the keyboard and add the Playgroup object to the queue.
  4. Serve a person in the queue: serve the first person in the queue and then remove the person from the queue. You can simulate "serve a person" by displaying someone is being served, such as "Jane is being served".
  5. Quit: display "Good-bye" and terminate the program.

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!