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

You are to create a class called Person. You should create the definition of the class...
You are to create a class called Person. You should create the definition of the class Person in a file person.h and the functions for Person in person.cpp. You will also create a main program to be housed in personmain.cpp. A Person will have attributes of Height (in inches) Weight (in pounds to 2 decimal places) Name (string) Gender (‘m’ or ‘f’) Ethnicity/Race (could be more than one word) Occupation (also more than a single word) A Person will have...
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 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 basic_stats that takes as a parameter a list of Person objects and returns a tuple containing the mean, median, and mode of all the ages. To do this,...
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...
Person class You will implement the Person Class in Visual Studio. A person object may be...
Person class You will implement the Person Class in Visual Studio. A person object may be associated with multiple accounts. A person initiates activities (deposits or withdrawal) against an account that is captured in a transaction object. A short description of each class member is given below: Person Class Fields -   password : string Properties +   «C# property, setter private» IsAuthenticated : bool +   «C# property, setter absent» SIN : string +   «C# property, setter absent» Name : string Methods...
Please solve in C++ only class is not templated You need to write a class called...
Please solve in C++ only class is not templated You need to write a class called LinkedList that implements the following List operations: public void add(int index, Object item); // adds an item to the list at the given index, that index may be at start, end or after or before the // specific element 2.public void remove(int index); // removes the item from the list that has the given index 3.public void remove(Object item); // finds the item from...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT