1. An abstract Instrument class is created with an abstract play method. Piano, Flute, and Guitar subclasses override the play method to output instrument-specific strings. An Instrument array holds different instrument objects.
2. A Compartment abstract class with an abstract notice method is created. FirstClass, Ladies, General, Luggage subclasses override notice. A TestCompartment class creates a Compartment array of random subclasses.
3. An abstract Shape class with position and move methods is created. Line, Circle, Rectangle subclasses are derived along with a PolyLine class using Shape as a base. The show method is implemented for each to output shapes. Random Shape objects are selected from derived classes.
Download as DOC, PDF, TXT or read online on Scribd
100%(1)100% found this document useful (1 vote)
506 views
Assignments 4
1. An abstract Instrument class is created with an abstract play method. Piano, Flute, and Guitar subclasses override the play method to output instrument-specific strings. An Instrument array holds different instrument objects.
2. A Compartment abstract class with an abstract notice method is created. FirstClass, Ladies, General, Luggage subclasses override notice. A TestCompartment class creates a Compartment array of random subclasses.
3. An abstract Shape class with position and move methods is created. Line, Circle, Rectangle subclasses are derived along with a PolyLine class using Shape as a base. The show method is implemented for each to output shapes. Random Shape objects are selected from derived classes.
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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?