Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

Assignments

----------------1

Create an abstract class Instrument which is having the


abstract function play.
Create three more sub classes from Instrument which
is Piano, Flute, Guitar.
Override the play method inside all three classes
printing a message
Piano is playing tan tan tan tan for Piano class
Flute is playing toot toot toot toot for Flute class
Guitar is playing tin tin tin for Guitar class
You must not allow the user to declare an object of
Instrument class.
Create an array of 10 Instruments.
Assign different type of instrument to Instrument
reference.
Check for the polymorphic behavior of play method.

Use the instanceof operator to print that which object


stored at which index of instrument array.
Create an abstract class Compartment to represent a rail
coach. Provide an abstract function notice in this class.
Derive FirstClass, Ladies, General, Luggage classes
from the compartment class. Override the notice
function in each of them to print notice suitable to the
type of the compartment.
Create a class TestCompartment . Write main function
to do the following:
Declare an array of Compartment of size 10.
Create a compartment of a type as decided by a
randomly generated integer in the range 1 to 4.
Check the polymorphic behavior of the notice method.
Define an abstract base class Shape that includes

protected data members for the (x, y) position of a shape,


a public method to move a shape, and a public abstract
method show() to output a shape. Derive subclasses for
lines, circles, and rectangles. Also, define the class
PolyLine that you saw in this chapter with Shape as its
base class. You can represent a line as two points, a
circle as a center and a radius, and a rectangle as two
points on diagonally opposite corners. Implement the
toString() method for each class. Test the classes by
selecting ten random objects of the derived classes, and
then invoking the show() method for each. Use the
toString() methods in the derived classes.
Develop a java class that has finalize method which
displays Finalize method called. Create another class
which creates objects of the previous class and it uses
the same object reference for creating these objects. For
example, if A1 is the class name, then the objects are
created as below :
A1 a = new A1();
a = new A1();
a = new A1();
When the statement Runtime.getRuntime().gc() is
invoked, how many times the finalize method is called?

You might also like