In: Computer Science
1- Write a class called MedicalStaff that is a Person (the class that you wrote in last lab). A MedicalStaff has specialty (i.e. Orthopedic, cardiology, etc.).
2- Then write two classes:
Doctor class has office visit fee.
Nurse class has title (i.e. RN, NP, etc.)
Both classes inherit from MedicalStaff.
Be sure these are all complete classes, including toString method.
3- Write a tester to test these classes and
their methods, by creating an array or ArrayList of
Person and adding different kinds of objects
(MedicalStaff, Doctor, Nurse) to it.
Now that you know polymorphism, you don't need to create specific
references for each object.
4- Print the list of Person items by calling toString method in a for-each loop.
The Person class was,
public class Person { // Decalring super class//
private String name; // attributes
private int birthYear;
public Person(String n, int y) {
this.name = n;
this.birthYear = y;
}
public Person() {
name = "";
birthYear = 0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getbirthYear() {
return birthYear;
}
public void setbirthYear(int birthYear) {
this.birthYear = birthYear;
}
public String toString() {
return "Person [name=" + name + ", birthYear=" + birthYear +
"]";
}
}
Code is Given Below:
=====================
MedicalStaff.java
====================
//creating MedicalStaff class
public class MedicalStaff extends Person {
//declaring variable
private String specialty;
//declaring parameter constructor
public MedicalStaff(String n, int y, String specialty)
{
super(n, y);//calling super
constructor
this.specialty = specialty;
}
public MedicalStaff() {
super();
this.specialty="";
}
//getter and setter method
public String getSpecialty() {
return specialty;
}
public void setSpecialty(String specialty) {
this.specialty = specialty;
}
//to string method
@Override
public String toString() {
return "MedicalStaff [specialty=" +
specialty + ", Name=" + getName() + ",birthYear="
+ getbirthYear() + "]";
}
}
Doctor.java
================
//creating Doctor class
public class Doctor extends MedicalStaff {
//declaring variable
private int visitFee;
//declaring parameter constructor
public Doctor(String n, int y, String specialty, int
visitFee) {
super(n, y, specialty);
this.visitFee = visitFee;
}
public Doctor() {
super();
this.visitFee=0;
}
//getter and setter method
public int getVisitFee() {
return visitFee;
}
public void setVisitFee(int visitFee) {
this.visitFee = visitFee;
}
//to string method
@Override
public String toString() {
return "Doctor [visitFee=" +
visitFee + ", Speci,Name=" +
getName()
+ ", birthYear=" + getbirthYear() + "]";
}
}
Nurse.java
================
//creating Nurse class
public class Nurse extends MedicalStaff {
//declaring variable
private String title;
//declaring parameter constructor
public Nurse(String n, int y, String specialty, String
title) {
super(n, y, specialty);
this.title = title;
}
public Nurse() {
super();
this.title="";
}
//getter and setter method
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
//to string method
@Override
public String toString() {
return "Nurse [title=" + title + ",
Specialty=" + getSpecialty() + ", Name=" + getName() + ",
birthYear="+getbirthYear()+"]";
}
}
Tester.java
=============
import java.util.ArrayList;
public class Tester {
public static void main(String[] args) {
//creating arraylist objects to
hold Persons
ArrayList<Person> persons=new
ArrayList<Person>();
//creating persons objects and
adding it to ArrayList
persons.add(new
Doctor("John",1998,"Orthopedic",5000));
persons.add(new
MedicalStaff("Mike",1972,"cardiology"));
persons.add(new
Nurse("Julie",1991,"Orthopedic","RN"));
//loop to print all persons
info
for(Person p:persons) {
System.out.println(p.toString());
}
}
}
Output:
=============
Code Snapshot:
=================