homework2_coen120

2
SANTA CLARA UNIVERSITY Computer Engineering Department COEN 120 Real Time Systems Homework 2 (Inheritance, Abstract Class, Interface) (15 points) For each of the following programs, compile and execute the program and submit the code as well as the output. Programs must contain comments and should use an appropriate indentation style. 1. What is the difference between an abstract class and an interface? 2. Create three classes Mouse, Gerbil and Hamster that have a common superclass called Rodent. In the base class Rodent, provide methods that are common to all rodents, and override these in the derived classes to perform different types of behaviors depending on the specific type of Rodent. Create an array of Rodent, fill it with different specific types of Rodents, and call your base class methods to see what happens. 3. Create a class as abstract without including any abstract methods, and verify that you cannot create an instance of that class. 4. Modify the Rodent class in Example 1 so that it is an abstract class. Make the methods of Rodent abstract whenever possible. 5. (a) Write an interface calls Instrument with two methods: void play(String notes); void adjust(); (b) Write two classes called Wind and Percussion that implement this interface. The method body contains a print statement to display a message containing the name of the class and the method name on the screen. (c) Write a class called Brass that extends Wind and does not define any methods. (d) Write a class called Woodwind that extends Wind and does not define any methods.

Upload: n

Post on 01-Oct-2015

213 views

Category:

Documents


0 download

DESCRIPTION

COEN120 Homework Assignment 2

TRANSCRIPT

  • SANTA CLARA UNIVERSITY

    Computer Engineering Department COEN 120 Real Time Systems

    Homework 2 (Inheritance, Abstract Class, Interface)

    (15 points)

    For each of the following programs, compile and execute the program and submit the code as

    well as the output. Programs must contain comments and should use an appropriate indentation

    style.

    1. What is the difference between an abstract class and an interface?

    2. Create three classes Mouse, Gerbil and Hamster that have a common superclass called

    Rodent. In the base class Rodent, provide methods that are common to all rodents, and

    override these in the derived classes to perform different types of behaviors depending on

    the specific type of Rodent. Create an array of Rodent, fill it with different specific types

    of Rodents, and call your base class methods to see what happens.

    3. Create a class as abstract without including any abstract methods, and verify that you

    cannot create an instance of that class.

    4. Modify the Rodent class in Example 1 so that it is an abstract class. Make the methods of

    Rodent abstract whenever possible.

    5. (a) Write an interface calls Instrument with two methods:

    void play(String notes);

    void adjust();

    (b) Write two classes called Wind and Percussion that implement this interface. The

    method body contains a print statement to display a message containing the name of the

    class and the method name on the screen.

    (c) Write a class called Brass that extends Wind and does not define any methods.

    (d) Write a class called Woodwind that extends Wind and does not define any methods.

  • (e) Write a new class called Music that contains a main method. In main, create an

    instance of the Brass, Woodwind and Percussion classes. Each instance calls its play

    method.