Solution Review: Task I to Task V
Have a look at the solution to the previously assigned challenges: Task I to Task V.
Task I: Design the basic structure
The task was to create the classes and add the instance variables. Look at the code below.
import java.util.ArrayList;public class Hospital{/* Instance variables */private String name;private ArrayList<Cardiologist> cardiologist;private ArrayList<Neurosurgeon> neurosurgeon;private ArrayList<Dietitian> dietitian;}
Look at the Patient.java file. We add a String
variable for name and two int
type variables for age and code.
Look at the Doctor.java file. We add a public
interface: Doctor
. An interface has no instance variable, so we leave it blank. All the doctor’s implementation is to be done in the Cardiologist
, Neurosurgeon
, and Dietitian
classes.
Look at the Cardiologist.java file. Cardiologist
class implements the Doctor
interface. We add a String
variable for name and an int
variable for age. We also have to store the availability status. It’s a two way decision: a doctor is either available or unavailable. So, we add a boolean
variable: availability
.
Look at the Neurosurgeon.java file. The Neurosurgeon
class implements the Doctor
interface. We add a String
variable for name and an int
variable for age. We also have to store the availability status, so we add a boolean
variable: availability
.
Look at the Dietitian.java file. The Dietitian
class implements the Doctor
interface. We add a String
variable for name and an int
variable for age. We also have to store the availability status, so we add a boolean
variable: availability
.
Look at the Hospital.java file. A hospital has a name. So, we declare a String
variable: name
. Look at line 8. To store information about cardiologists, we make an ArrayList
of Cardiologist
type. We choose an ArrayList
type because the size can be incremented or decremented. Similarly, we make an ArrayList
of both Neurosurgeon
and Dietitian
type.
Task II: Design the constructors
The task was to design the constructor methods of the classes. Look at the code below.
import java.util.ArrayList;public class Hospital{/* Instance variables */private String name;private ArrayList<Cardiologist> cardiologists;private ArrayList<Neurosurgeon> neurosurgeons;private ArrayList<Dietitian> dietitians;/*Constructor Methods*/public Hospital(String name){this.name = name;cardiologists = new ArrayList<Cardiologist>();neurosurgeons = new ArrayList<Neurosurgeon>();dietitians = new ArrayList<Dietitian>();}}
Look at the Patient.java file. Look at line 9. According to the requirement, the constructor takes name,
age
, and code
as parameters. Inside the constructor, instance variables are assigned the values that are sent via the arguments.
Look at the Doctor.java file. We can’t create an object of the interface. Thus, ...