Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
Java Language EssentialsDownload tutorial zip fileEmail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
Exercises
  


7. MusicStore with an Owner: Help page 21 of 31




  • Task 1

    Add the instance variable owner to MusicStore. The data type should be String and the initial value should be "No owner.", or something similar.


    The convention, or at least the practice for most programmers, is to place instance variables at the beginning of the class definition for readability. The class layout would be similar to the following:

    
    public class MusicStore {
      // instance variables ...
      // instance methods ...
    }
    
  • Task 2

    Add the method setOwner() to MusicStore. This method should set the instance variable owner to whatever String value is passed as an argument.


    The method parameter can have any variable name, for example, newOwner. If, however, the parameter name is owner, that is, the same as the instance variable name, you must use the this reference to distinguish the instance variable from the method parameter:

    
      ...
      this.owner = owner;
      ...
    
  • Task 3

    Add functionality to TestMusicStore to set the store owner's name.


    The method invocation would be:

    
      ...
      <instance-name>.setOwner("Ringo Starr");
      ...
    
  • Task 4

    Compile the source code for MusicStore and TestMusicStore.


    Some Java environments require a CLASSPATH setting. You should set the CLASSPATH environment variable to include the current directory, but only if your environment requires CLASSPATH.

  • Task 5

    Execute TestMusicStore.


    With the JDK, for example, simply invoke the Java interpreter with TestMusicStore as an argument:

    
    D:\>java TestMusicStore
    


copyright 1996-2000 Magelang Institute dba jGuru


Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
PrivacyLegalContact