Chapter 8 (Programming Projects)

249 downloads 73690 Views 73KB Size Report
... Preparer: Your Name. //. // Solution to Programming Project 8.2 ... HospitalEmployee.java Preparer: Your Name. //. // Solution to Programming Project 8.2.
Chapter 8 (Programming Projects)

CS 102- Inherritance Dr. H. Assadipour

PP 8.2 Driver program //******************************************************************** // Hospital.java Preparer: Your Name // // Solution to Programming Project 8.2 //******************************************************************** public class Hospital { //----------------------------------------------------------------// Creates several objects from classes derived from // HospitalEmployee. //----------------------------------------------------------------public static void main (String [] args) { HospitalEmployee vito = new HospitalEmployee ("Vito", 123); Doctor michael = new Doctor ("Michael", 234, "Heart"); Surgeon vincent = new Surgeon ("Vincent", 645, "Brain", true); Nurse sonny = new Nurse ("Sonny", 789, 6); Administrator luca = new Administrator ("Luca", 375, "Business"); Receptionist tom = new Receptionist ("Tom", 951, "Talking", true); Janitor anthony = new Janitor ("Anthony", 123, "Maintenence", false); // print the employees System.out.println (vito); System.out.println (michael); System.out.println (vincent); System.out.println (sonny); System.out.println (luca); System.out.println (tom); System.out.println (anthony); // invoke the specific methods of the objects vito.work(); michael.diagnose(); vincent.operate(); sonny.assist(); luca.administrate(); tom.answer(); anthony.sweep(); } }

1

HospitalEmployee: Parent (Base class) of the derived classes Doctor, Nurse, and Administrator //******************************************************************** // HospitalEmployee.java Preparer: Your Name // // Solution to Programming Project 8.2 //******************************************************************** public class HospitalEmployee { protected String name; protected int number; //----------------------------------------------------------------// Sets up this hospital employee with the specified information. //----------------------------------------------------------------public HospitalEmployee (String empName, int empNumber) { name = empName; number = empNumber; } //----------------------------------------------------------------// Sets the name for this employee. //----------------------------------------------------------------public void setName (String empName) { name = empName; } //----------------------------------------------------------------// Sets the employee number for this employee. //----------------------------------------------------------------public void setNumber (int empNumber) { number = empNumber; } //----------------------------------------------------------------// Returns this employee's name. //----------------------------------------------------------------public String getName() { return name; } //----------------------------------------------------------------// Returns this employee's number. //----------------------------------------------------------------public int getNumber(){ return number; }

2

//----------------------------------------------------------------// Returns a description of this employee as a string. //----------------------------------------------------------------public String toString() { return name + "\t" + number; } //----------------------------------------------------------------// Prints a message appropriate for this employee. //----------------------------------------------------------------public void work() { System.out.println (name + " works for the hospital."); } } Derived Class: Doctor (extension of the base class HospitalEmployee) //******************************************************************** // Doctor.java Preparer: Your Name // // Solution to Programming Project 8.2 //******************************************************************** public class Doctor extends HospitalEmployee { protected String specialty; //----------------------------------------------------------------// Sets up this doctor with the specified information. //----------------------------------------------------------------public Doctor (String empName, int empNumber, String special) { super (empName, empNumber); specialty = special; } //----------------------------------------------------------------// Sets this doctor's specialty. //----------------------------------------------------------------public void setSpecialty (String special) { specialty = special; } //-----------------------------------------------------------------

3

// Returns this doctor's specialty. //----------------------------------------------------------------public String getSpecialty() { return specialty; } //----------------------------------------------------------------// Returns a description of this doctor as a string. //----------------------------------------------------------------public String toString() { return super.toString() + "\t" + specialty; } //----------------------------------------------------------------// Prints a message appropriate for this doctor. //----------------------------------------------------------------public void diagnose() { System.out.println (name + " is a(n) " + specialty + " doctor."); } } Derived Class: Surgeon(extension of Doctor which is an extension of HospitalEmployee) //******************************************************************** // Surgeon.java Preparer: Your Name // // Solution to Programming Project 8.2 //******************************************************************** public class Surgeon extends Doctor { protected boolean operating; //----------------------------------------------------------------// Sets up this surgeon with the specified information. //----------------------------------------------------------------public Surgeon (String empName, int empNumber, String special, boolean isOper) { super (empName, empNumber, special); operating = isOper; } //-----------------------------------------------------------------

4

// Sets the operating status of this surgeon. //----------------------------------------------------------------public void setIsOperating (boolean isOper) { operating = isOper; } //----------------------------------------------------------------// Gets the current operating status of this surgeon. //----------------------------------------------------------------public boolean getIsOperating() { return operating; } //----------------------------------------------------------------// Returns a description of this surgeon as a string. //----------------------------------------------------------------public String toString() { return super.toString() + "\tOperating: " + operating; } //----------------------------------------------------------------// Prints a message appropriate for this surgeon. //----------------------------------------------------------------public void operate() { System.out.print (name + " is"); if (!operating) System.out.print (" not"); System.out.println (" operating now."); } }

Derived Class: Nurse (an extension of HospitalEmployee) //******************************************************************** // Nurse.java Preparer: Your Name // // Solution to Programming Project 8.2 //******************************************************************** public class Nurse extends HospitalEmployee { protected int numPatients;

5

//----------------------------------------------------------------// Sets up this nurse with the information specified. //----------------------------------------------------------------public Nurse (String empName, int empNumber, int numPat) { super (empName, empNumber); numPatients = numPat; } //----------------------------------------------------------------// Sets the number of patients for this nurse. //----------------------------------------------------------------public void setNumPatients (int pat) { numPatients = pat; } //----------------------------------------------------------------// Returns this nurse's current number of patients. //----------------------------------------------------------------public int getNumPatients() { return numPatients; } //----------------------------------------------------------------// Returns a description of this nurse as a string. //----------------------------------------------------------------public String toString() { return super.toString() + " has " + numPatients + " patients."; } //----------------------------------------------------------------// Prints a message appropriate for this nurse. //----------------------------------------------------------------public void assist() { System.out.println (name + " is a nurse with " + numPatients + " patients."); } }

6

Derived Class: Administrator (extending HospitalEmployee) //******************************************************************** // Administrator.java Preparer: Your Name // Solution to Programming Project 8.2 //******************************************************************** public class Administrator extends HospitalEmployee { protected String department; //----------------------------------------------------------------// Sets up this administrator with the specified information. //----------------------------------------------------------------public Administrator (String empName, int empNumber, String dept) { super (empName, empNumber); department = dept; } //----------------------------------------------------------------// Sets this administrator's department. //----------------------------------------------------------------public void setDepartment (String dept) { department = dept; } //----------------------------------------------------------------// Returns this administrator's department. //----------------------------------------------------------------public String getDepartment() { return department; } //----------------------------------------------------------------// Returns a description of this administrator as a string. //----------------------------------------------------------------public String toString() { return super.toString() + " works in " + department; } //----------------------------------------------------------------// Prints a message appropriate for this administrator. //----------------------------------------------------------------public void administrate() { System.out.println (name + " works in the " + department + " department."); } }

7

Derived Class: Receptionist (extending Administrator which extends HospitalEmployee) //******************************************************************** // Receptionist.java Preparer: Your Name // // Solution to Programming Project 8.2 //******************************************************************** public class Receptionist extends Administrator { protected boolean answering; //----------------------------------------------------------------// Sets up this receptionist with the specified information. //----------------------------------------------------------------public Receptionist (String empName, int empNumber, String dept, boolean ans) { super (empName, empNumber, dept); answering = ans; } //----------------------------------------------------------------// Sets the current answering status of this receptionist. //----------------------------------------------------------------public void setIsAnswering (boolean isA) { answering = isA; } //----------------------------------------------------------------// Returns the current answering status of this receptionist. //----------------------------------------------------------------public boolean getIsAnswering() { return answering; } //----------------------------------------------------------------// Returns a description of this receptionist as a string. //----------------------------------------------------------------public String toString() { return super.toString() + "\tAnswering: " + answering; } //-----------------------------------------------------------------

8

// Prints a message appropriate for this receptionist. //----------------------------------------------------------------public void answer() { System.out.print (name + " is"); if (!answering) System.out.print (" not"); System.out.println (" answering the phone."); } } Derived Class: Janitor (extending Administrator which extends HospitalEmployee) //******************************************************************** // Janitor.java Preparer: Your Name // // Solution to Programming Project 8.2 //******************************************************************** public class Janitor extends Administrator { protected boolean sweeping; //----------------------------------------------------------------// Sets up this janitor with the specified information. //----------------------------------------------------------------public Janitor (String empName, int empNumber, String dept, boolean sw) { super (empName, empNumber, dept); sweeping = sw; } //----------------------------------------------------------------// Sets the sweeping status of this janitor. //----------------------------------------------------------------public void setIsSweeping (boolean isS) { sweeping = isS; } //----------------------------------------------------------------// Returns the current sweeping status of this janitor. //----------------------------------------------------------------public boolean getIsSweeping () { return sweeping; }

9

//----------------------------------------------------------------// Returns a description of this janitor as a string. //----------------------------------------------------------------public String toString () { return super.toString() + "\tSweeping: " + sweeping; } //----------------------------------------------------------------// Prints a message appropriate for this janitor. //----------------------------------------------------------------public void sweep() { System.out.print (name + " is"); if (!sweeping) System.out.print (" not"); System.out.println (" sweeping the floor."); } } Sample Runs: ÏÏ«Ï ----jGRASP exec: java Hospital ÏÏ§Ï ÏϧÏVito_123 ÏϧÏMichael_234_Heart ÏϧÏVincent_645_Brain_Operating: true ÏϧÏSonny_789 has 6 patients. ÏϧÏLuca_375 works in Business ÏϧÏTom_951 works in Talking_Answering: true ÏϧÏAnthony_123 works in Maintenence_Sweeping: false ÏϧÏVito works for the hospital. ÏϧÏMichael is a(n) Heart doctor. ÏϧÏVincent is operating now. ÏϧÏSonny is a nurse with 6 patients. ÏϧÏLuca works in the Business department. ÏϧÏTom is answering the phone. ÏϧÏAnthony is not sweeping the floor. ÏÏ§Ï ÏÏ©Ï ----jGRASP: operation complete. ¼¼ÏÏ

10