Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Each class should have a constructor, and the fields and methods specified below. For each of the classes, all the fields should be declared as

Each class should have a constructor, and the fields and methods specified below. For each of the classes, all the fields should be declared as private.

Also, if a method has a precondition, specify the precondition in a @precond comment, and throw a runtime exception if it is not satisfied. Be sure to include a meaningful error message in the String parameter for the exception constructor. Note that these additional comments and precondition checks have already been added to the Ward class, and should be added as well to the new classes. When appropriate, exceptions should be caught and handled. In particular, if a task of the system fails and as a result throws an exception, it is reasonable to print the message of the exception, and then try the next task. The system should not crash when a user enters invalid data. To achieve that, a try-catch will be needed for the invocation of any method to handle a task that might throw an exception. These try-catches will probably be in the system class, as it is the one that has the invocations of the methods in the entity classes. Note that you might be able to handle all situations with only a couple try blocks.

image text in transcribed

/**

* The model of a person who has a name and a health number

* that cannot be changed.

*/

public class Person

{

/**

* The name of the person.

*/

private String name;

/**

* The health number of the person.

*/

private int healthNum;

/**

* Initialize an instance with the given name and health number.

*

* @param pName the person's name

* @param pNumber the person's health number

*/

public Person(String pName, int pNumber)

{

name = pName;

healthNum = pNumber;

}

/**

* Return the name of the person.

*

* @return the name of the person

*/

public String getName()

{

return name;

}

/**

* Return the health number of the person.

*

* @return the health number of the person

*/

public int getHealthNumber()

{

return healthNum;

}

/**

* Change the name of the person.

*

* @param newName the name of the person

*/

public void setName(String newName)

{

name = newName;

}

/**

* Return a string representation of the person.

*

* @return a string representation of the person

*/

public String toString()

{

return " Name: " + name + " Health number: " + healthNum + " ";

}

/**

* A method to test the Person class.

*/

public static void main(String[] args)

{

int numErrors = 0;

Person p = new Person("Pete", 123456);

System.out.println("The person called Pete with number 123456 is " + p + " ");

if (! p.getName().equals("Pete"))

{

System.out.println("The constructor or getName failed");

numErrors++;

}

if (p.getHealthNumber() != 123456)

{

System.out.println("The constructor or getHealthNumber failed");

numErrors++;

}

p.setName("Jim");

if (! p.getName().equals("Jim"))

{

System.out.println("setName failed");

numErrors++;

}

p = new Person("Mary", 987654);

System.out.println("The person called Mary with number 987654 is " + p + " ");

if (! p.getName().equals("Mary"))

{

System.out.println("The constructor or getName failed");

numErrors++;

}

if (p.getHealthNumber() != 987654)

{

System.out.println("The constructor or getHealthNumber failed");

numErrors++;

}

p.setName("Sue");

if (! p.getName().equals("Sue"))

{

System.out.println("setName failed");

numErrors++;

}

System.out.println("The number of errors found is " + numErrors);

}

}

Modifications to Person class: Change this class to an abstract class. Even though Person exists, we have no persons in the hospital. Thus we want to disallow an instantiation of a Person object. Patients, doctors and surgeons are all persons.. Patient class: The Patient class extends the Person class. The Patient class should have a Bed reference. Also, the patient should have a linked list to store all the doctors of the Patient. When a patient is created, she/he is not in a bed, and has no doctors (Bed variable has NULL as reference). This class will need methods to handle the two fields and the toString0 method should be overridden to output the bed label (if assigned to a bed) and the name of each doctor of the Patient (there could be many so consider a linked list). Note that in the toString0 method, if you include all the information for a doctor of a patient, you might end up with an infinite loop

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DNA Databases

Authors: Stefan Kiesbye

1st Edition

0737758910, 978-0737758917

More Books

Students also viewed these Databases questions

Question

What are the categories protected under copyright laws?

Answered: 1 week ago