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
  


8. MusicStore: Open or Closed?: Help page 24 of 31




  • Task 1

    Add the instance variables openTime and closeTime to MusicStore. The data types should be int and the initial values should be reasonable times such as 9 and 21 (9 AM to 9 PM in 24-hour time), or something similar.


    For now, it's better to assume you're in an environment that operates on a 24-hour clock, which is exactly the case in many countries. Otherwise, you'll have to deal with complex comparisons in the logic sections that handle whether the store is open and closed.

  • Task 2

    Add four access methods for the integer-based open and closing time variables: setOpen(), getOpen(), setClose(), and getClose().


    The parameter for the set methods can have any variable name, for example, open and close. If, however, the parameter names are openTime and closeTime, respectively, that is, the same as the instance variable names, you must use the this reference to distinguish the instance variables from the method parameters:

    
      ...
      this.openTime = openTime;
      ...
    
  • Task 3

    Add a method that returns the appropriate boolean value for whether the store is currently open or closed: isOpen(). The method should compare the instance variables openTime and closeTime with the current system time. You will use the provided AltDate.getHourInt() method to obtain the current hour (0..24).


    Generally, Java's Date class (java.util.Date) is the starting point for determining date- and time-related values. Date is not trivial to use, however, because of the return types for its methods.

    To short-cut the time required, this exercise includes user-defined date-time functionality in the class AltDate. You simply have to create an instance and use its utility methods:

    
    AltDate d = new AltDate();
    

    In particular, you will need the getHourInt() method for comparing the current hour to the store's hours of operation. getHourInt() returns an int for the current hour based on a 24-hour clock. (Hint: getHourInt() is the only method you'll need.)

  • Task 4

    Add the convenience method getOpenClosedMessage(). It should return a String value stating whether the store is currently open or closed, based on the current system time and the openTime and closeTime instance variables.


    Because the more fundamental method isOpen() already exists, adding getOpenClosedMessage() is quite trivial. Simply use isOpen() with an if-then-else statement to test the current status and return the appropriate string, for example, "We're Open!" or "We're Closed!".

  • Task 5

    For now, modify the method displayHoursOfOperation() so that it reports the arbitrary (hard-coded) opening and closing times you specified when initializing the openTime and closeTime instance variables.


    No help data is necessary here.

  • Task 6

    Add functionality to TestMusicStore to display a message such as "We're Open!" or "We're Closed!".


    This functionality is available in MusicStore, so simply use the String value returned from MusicStore's getOpenClosedMessage().

  • Task 7

    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 8

    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