CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2010
    Posts
    3

    Post make program if u can core java

    this is the core java program and its so easy what can i do for that............any one can help me to prepare for this program...



    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.

  2. #2
    Join Date
    Sep 2010
    Posts
    3

    Re: make program if u can core java

    //i have been made this one what changes i have to do for the following whole program to do


    abstract class Instrument
    {
    String InstrumentName="";
    abstract void play();

    }

    class Piano extends Instrument
    {

    void play()
    {
    System.out.println("Piano is playing tan tan tan tan:");
    }

    }

    class Flute extends Instrument
    {

    void play()
    {
    System.out.println("Flute is playing toot toot toot toot");
    }

    }

    class Guitar extends Instrument
    {

    void play()
    {
    System.out.println("Guitar is playing tin tin tin ");
    }
    public static void main(String ar[])
    {
    /*Instrument[] Instru=new Instrument[10];
    Instru[0].InstrumentName="Piano";
    Instru[1].InstrumentName="Flute";*/

    }

    }

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: make program if u can core java

    The commented out code in your main method correctly declares an array of 10 instruments but you are then trying to access Instrument objects in the array before you have added any objects. Creating the array doesn't fill the array with objects, you have to do that.

    What is the point of the InstrumentName field (BTW by convention all methods, fields, variables etc should start with a lower case letter, only class names should start with an upper case letter).
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured