Suggested Solution

Task 1

Task 1 Part A

A CareWorker works three days a week and has two properties:

  1. A 3-element String array storing the days to be worked eg ["Monday", "Tuesday", "Wednesday"]
  2. an ElderlyPerson object

An ElderlyPerson has 3 properties:

  1. A name, eg "Hilda McFarlane"
  2. an age, eg 78
  3. an illness eg "Dementia"

In a main program, when a CareWorker object is constructed, a reference to an ElderlyPerson object and a reference to a 3-element String array are passed as parameters eg

String[] days = {"Monday", "Thursday", "Saturday"};

ElderlyPerson e = new ElderlyPerson("Jane Briggs", 78, "Dementia");

CareWorker s = new CareWorker(e, days);

Create a new project called CareWorkerProject. Create the following 3 class files:

Using the information above, define private properties and design constructor methods for both the CareWorker and ElderlyPerson classes.

Task 1 Part B

In the CareWorker class, create accessor methods for the 2 CareWorker properties.

In the ElderlyPerson class, create accessor methods for the 3 ElderlyPerson properties.

Task 1 Part C

In the ElderlyPerson class, create a method called toString.

This method returns a String which contains all of an ElderlyPerson objects property data in 1 sentence eg:

Name: Martha Munroe; Age: 78; Illness: Dementia

Task 1 Part D

In the ElderlyPerson class, create a method called birthday.

This method simply adds 1 to the current age of the ElderlyPerson.

Task 1 Part E

In the CareWorker class, create a method called displayDays.

This method simply displays the three days that the CareWorker works.

Task 1 Part F

In the CareWorker class, create a mutator method, setNewWorkDay(String dayToBeReplaced, String newDay)

This method searches the String array property for dayToBeReplaced and, if found, replaces it with newDay

Task 1 Part G

In the CareWorker class, create a mutator method, setElderlyPerson(ElderlyPerson newPerson) which will replace the current elderly person with newPerson, as long as they don't have the same illness!

Task 1 Part H

Now turn to the Main class. A program has been started:

public static void main(String[] args){

public ststic void main(String[] args){

String[] days = {"Friday", "Saturday", "Sunday"};

ElderlyPerson ep = new ElderlyPerson("Janny Jones", 104, "Deafness");

CareWorker c= new CareWorker(ep, days);

System.out.println(c.getElderlyPerson().toString());

c.getElderlyPerson().birthday();

//display all of the data associated with the ElderlyPerson object inside the CareWorker object!

System.out.println(c.getElderlyPerson().toString());

ElderlyPerson ethel = new ElderlyPerson("Ethel Xu", 95, "Deafness");

//attempt to change the ElderlyPerson inside the CareWorker object - this shouldn't work!

c.setElderlyPerson(ethel);

//hopefully no change!

System.out.println(c.getElderlyPerson().toString());

System.out.println(c.displayDays());

c.setNewWorkDay("Saturday", "Wednesday");

System.out.println(c.displayDays());

Code this Main program and see if it works as expected.