Question

In: Computer Science

1- Write a class called MedicalStaff that is a Person (the class that you wrote in...

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 + "]";
}

}

Solutions

Expert Solution

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:

=================


Related Solutions

Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects and returns the standard deviation of all their ages (the population standard deviation that uses a denominator...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
We have created an ArrayList of Person class. write a method called push that pushes all...
We have created an ArrayList of Person class. write a method called push that pushes all the people with the even length last name to the end of the ArrayList Content of the ArrayList before push [alex Bus, Mary Phillips, Nik Lambard, Rose Rodd, Esa khan, Jose Martinex, Nik Patte] content of the ArrayList after the push method [alex Bus, Nik Lambard, Nik Patte, Mary Phillips, Rose Rodd, Esa khan, Jose Martinex] import java.util.*; class Person { private String name;...
Refactor the following classes so they are both derived from a base class called Person. Write...
Refactor the following classes so they are both derived from a base class called Person. Write the code for the new base class as well. Try to avoid repeating code as much as possible. Write the classes so that any common methods can be invoked through a pointer or reference to the base class. #include <string> #include <cmath> using namespace std; class Student { private: string name;    int age;    int studyYear; public:    Student(string,int,int); void study();    void...
Using Classes This problem relates to the pre-loaded class Person. Using the Person class, write a...
Using Classes This problem relates to the pre-loaded class Person. Using the Person class, write a function print_friend_info(person) which accepts a single argument, of type Person, and: prints out their name prints out their age if the person has any friends, prints 'Friends with {name}' Write a function create_fry() which returns a Person instance representing Fry. Fry is 25 and his full name is 'Philip J. Fry' Write a function make_friends(person_one, person_two) which sets each argument as the friend of...
Task You will write a class called Grid, and test it with a couple of programs....
Task You will write a class called Grid, and test it with a couple of programs. A Grid object will be made up of a grid of positions, numbered with rows and columns. Row and column numbering start at 0, at the top left corner of the grid. A grid object also has a "mover", which can move around to different locations on the grid. Obstacles (which block the mover) and other objects (that can be placed or picked up)...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
1. Write a public Java class called WriteFile which opens an empty file called letters.dat. This...
1. Write a public Java class called WriteFile which opens an empty file called letters.dat. This program will read a String array called letters and write each word onto a new line in the file. The method should include an appropriate throws clause and should be defined within a class called TFEditor. The string should contain the following words: {“how”, “now”, “brown”, “cow”}
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT