programming projects - cornell programming project application // // authors: lewis and loftus...

Download Programming Projects - Cornell     Programming Project Application // // Authors: Lewis and Loftus // // Classes ... private int prog_hour, prog_min; protected int channel, volume;

Post on 06-Mar-2018

217 views

Category:

Documents

4 download

Embed Size (px)

TRANSCRIPT

  • 100 Chapter 8 Inheritance

    Programming Projects

    8-1

    3 Experiment with a simple derivation. Write a constructor for a child class that does not explicitly refer to the co nstructor of the parent. What happened? Why?

    Tw

    o different results can occur. The first case occurs when the parent class contains an explicit declaration of a p arameterless constructor (the default constructor). In this case, when the object of the child class is instan-tia ted using a constructor that does not explicitly refer to the parents constructor, the default constructor of th e parent is called implicitly before executing the childs constructor. The second case occurs when a con-s tructor for the parent class exists, but no default constructor is declared. This configuration will cause a co mpile time error when compiling the child class.

    8

    -14 Design and implement a set of classes that define the employees of a hospital: doctor, nurse, administrator, sur geon, receptionist, janitor, etc. Include methods in each class that are named according to the services pro-v ided by that person and that print an appropriate message. Create a main method to instantiate and exercise sever al of the classes.

    //*******************************************************************

    //

    // Hospital.java Programming Project Application

    //

    // Authors: Lewis and Loftus

    //

    // Classes: Hospital

    // Doctor

    // Nurse

    // Administrator

    // Surgeon

    // Receptionist

    // Janitor

    //

    //*******************************************************************

    //-------------------------------------------------------------------

    //

    // Class Hospital

    //

    // Methods:

    //

    // public static void main (String[] args)

    //

    //-------------------------------------------------------------------

    class Hospital {

    //============================================================

    // Program to demonstrate using various classes related to

    // a set of hospital employees.

  • 101

    //============================================================

    public static void main (String[] args) {

    Doctor doctor = new Doctor();

    Nurse nurse = new Nurse();

    Administrator administrator = new Administrator();

    Surgeon surgeon = new Surgeon();

    Receptionist receptionist = new Receptionist();

    Janitor janitor = new Janitor();

    doctor.diagnose();

    nurse.care();

    administrator.budget();

    surgeon.diagnose();

    surgeon.operate();

    receptionist.type();

    receptionist.call();

    janitor.clean();

    } // method main

    } // class Hospital

    //-------------------------------------------------------------------

    //

    // Class Employee

    //

    // Methods:

    //

    // public void set_salary(int salary)

    //

    //-------------------------------------------------------------------

    class Employee {

    int salary;

    //============================================================

    // Sets the salary for each employee.

    //============================================================

    public void set_salary (int salary) {

    this.salary = salary;

    } // method set_salary

  • 102 Chapter 8 Inheritance

    } // class Employee

    //-------------------------------------------------------------------

    //

    // Class Doctor

    //

    // Methods:

    //

    // public void diagnose()

    //

    //-------------------------------------------------------------------

    class Doctor extends Employee {

    //============================================================

    // Doctors provide a diagnoses.

    //============================================================

    public void diagnose() {

    System.out.println ("Doctor is diagnosing.");

    } // method diagnose

    } // class Doctor

    //-------------------------------------------------------------------

    //

    // Class Nurse

    //

    // Methods:

    //

    // public void care()

    //

    //-------------------------------------------------------------------

    class Nurse extends Employee {

    //============================================================

    // Nurses care for their patients.

    //============================================================

    public void care() {

    System.out.println ("Nurse is caring.");

    } // method care

    } // class Nurse

    //-------------------------------------------------------------------

  • 103

    //

    // Class Administrator

    //

    // Methods:

    //

    // public void budget()

    //

    //-------------------------------------------------------------------

    class Administrator extends Employee {

    //============================================================

    // Administorators prepare a budget

    //============================================================

    public void budget() {

    System.out.println ("Administrator is budgeting.");

    } // method budget

    } // class Administrator

    //-------------------------------------------------------------------

    //

    // Class Surgeon

    //

    // Methods:

    //

    // public void operate()

    //

    //-------------------------------------------------------------------

    class Surgeon extends Doctor {

    //============================================================

    // Surgeons are doctors who operate.

    //============================================================

    public void operate() {

    System.out.println ("Surgeon is operating.");

    } // method operate

    } // class Surgeon

    //-------------------------------------------------------------------

    //

    // Class Receptionist

  • 104 Chapter 8 Inheritance

    //

    // Methods:

    //

    // public void type()

    // public void call()

    //

    //-------------------------------------------------------------------

    class Receptionist extends Employee {

    //============================================================

    // Receptionists type reports and forms.

    //============================================================

    public void type() {

    System.out.println ("Receptionist is typing.");

    } // method type

    // ============================================================

    // Receptionists make phone calls

    // ============================================================

    public void call() {

    System.out.println ("Receptionist is calling.");

    } // method call

    } // class Receptionist

    //-------------------------------------------------------------------

    //

    // Class Janitor

    //

    // Methods:

    //

    // public void clean()

    //

    //-------------------------------------------------------------------

    class Janitor extends Employee {

    //============================================================

    // Janitors clean the hospital.

    //============================================================

    public void clean() {

    System.out.println ("Janitor is cleaning.");

    } // method clean

    } // class Janitor

  • 105

    8

    -15 Design and implement a set of classes that define various types of reading material: books, novels, magazines, t

    extbooks, etc. Include data values that describe various attributes of the material: number of pages, favorite

    ch aracter, etc. Include methods that are named appropriately for each class that print a message. Create a main method to instantiate and exercise several of the classes.

    N

    ot Provided

    8

    -16 Design and implement a class that simulates a VCR remote control. The class should have various methods f

    or interacting with the TV as well as recording and playing tapes. Extend the VCR remote control class to

    cr eate a different type of remote control. Add a method to the subclass that provides additional or less func-tio

    nality for the remote control. Create a main method which instantiates several different remote controls and

    ex ercises the various class methods.

    //*******************************************************************

    //

    // Remote.java Programming Project Application

    //

    // Authors: Lewis and Loftus

    //

    // Classes: Remote

    // VCR

    //

    //*******************************************************************

    //-------------------------------------------------------------------

    //

    // Class Remote

    //

    // Methods:

    //

    // public static void main(String[] args)

    //

    //-------------------------------------------------------------------

    class Remote {

    //============================================================

    // Simulates the operations of a VCR remote control.

    //============================================================

    public static void main(String[] args) {

    VCR video = new VCR();

    video.set_channel(6);

  • 106 Chapter 8 Inheritance

    video.s

Recommended

View more >