home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / RB3641.ZIP / EX2 / TODOITEM.ST < prev    next >
Text File  |  1991-12-18  |  6KB  |  158 lines

  1.        Exercise 2 - solutions
  2.  
  3.         ToDoItem ( * This is the step by step instruction file ** )
  4.  
  5.        "In Class Hierarchy Browser select the class 'Object' 
  6.        and select 'Add Subclass' in  'Classes' menu.
  7.        Write the name of the class (ToDoItem) 
  8.        in the entry field. In the input are, 
  9.        insert four  instance variable names to this new class.
  10.        The variable names are: type, description, 
  11.        deadline ,priority and completed."
  12.  
  13.        " You should add two methods for every instance variable 
  14.         in ToDoItem: one for setting the value and one for 
  15.         returning the value to the caller. If value is returned, 
  16.         method looks very simple:"
  17.  
  18.        " To install the following method, evaluate with Do It:"
  19.  
  20.           (File pathName: 'ex2\typeset.mth') fileIn; close.
  21.  
  22.  
  23.           type
  24.                "this method returns the value of 'type'
  25.                 instance variable"
  26.                ^type
  27.  
  28.         "In this case, the method name is same as variable name
  29.         ( it can be something else as well ). The method to set 
  30.         the value of 'type' variable looks like this:"
  31.  
  32.           type: aType
  33.                "this method sets the value of 'type'
  34.                 instance variable to a value passed
  35.                 as a parameter ( aType)"
  36.                type := aType.
  37.  
  38.         "To set values you have to create an instance of the 
  39.         class and assign a variable to it. By using this 
  40.         variable and methods available in ToDoItem  you can 
  41.         modify the instance variables. To set values :"
  42.  
  43.           |item| 
  44.           item := ToDoItem new.  
  45.           item type: 'Meeting'.  
  46.           item description: 'Output'.  
  47.           item deadline: '91.12.20'.  
  48.           item priority: 'High'.
  49.           item completed: 'false'.
  50.           item type.                                             
  51.  
  52.         "If you evaluate the previous expressions with show it, 
  53.         you can also see that the last line of code return the 
  54.         value of 'type' variable."
  55.  
  56.        "The following example creates an ordered collection and 
  57.         two instances of ToDoItem. After setting type-variables 
  58.         these instances are added in collection:"
  59.  
  60.           |myOC item1 item2| 
  61.           item1 := ToDoItem new.  
  62.           item2 := ToDoItem new.  
  63.           item1 type: 'type1'.  
  64.           item2 type: 'type2'.  
  65.           myOC := OrderedCollection new.  
  66.           myOC add: item1;
  67.                   add: item2. 
  68.           myOC  inspect.                                     
  69.  
  70.           "In this example values of instance variables are 
  71.           changed after adding ToDoItem instances in collection. 
  72.           The values contained in collection are also changed:"
  73.  
  74.             |myOC item1 item2|
  75.             item1 := ToDoItem new.
  76.             item2 := ToDoItem new.
  77.             item1 type: 'type1'.
  78.             item2 type: 'type2'.
  79.             myOC := OrderedCollection new.
  80.             myOC add: item1;
  81.                     add: item2.
  82.             item1 type: 'Newtype'.
  83.             myOC  inspect.                                                                   
  84.  
  85.           "The simplest way of creating a string ( without any 
  86.           formatting ) is to create a method, that creates an 
  87.           empty string and adds all the instance variables to 
  88.           that string. The method can be implemented as follows:"
  89.  
  90.               
  91.             getItemText
  92.                      | answer |
  93.             
  94.                      answer := WriteStream on: (String new: 120).                          
  95.                      answer  nextPutAll: (type);
  96.                         nextPutAll: '  ';
  97.                         nextPutAll: (description);
  98.                         nextPutAll: '  ';
  99.                         nextPutAll: (deadline);
  100.                         nextPutAll: '  ';
  101.                         nextPutAll: (priority).
  102.                      ^answer contents.                 
  103.  
  104.         "A method is required to compare two items using a key 
  105.         and return a Boolean result. 
  106.         This method is used ,for example,
  107.          in the following expressions: Evaluate with ShowIt:"
  108.  
  109.  
  110.           | tItem1 tItem2 |
  111.           tItem1 := ToDoItem new.
  112.           tItem2 := ToDoItem new.
  113.           tItem1 type:'f'.
  114.           tItem2 type:'z'.
  115.           tItem1 compareTo: tItem2 using:'type'.
  116.  
  117.        "To install this method,
  118.         you can evaluate the following fiexpression:"
  119.           
  120.          (File pathName: 'ex2\compTo.mth') fileIn; close.
  121.  
  122.  
  123.         "To file in all the required methods evaluate with Do It:"
  124.  
  125.           (File pathName: 'ex2\todoitem.cls') fileIn; close.
  126.  
  127.         "*** IMPORTANT NOTE ***
  128.          A method in the ToDoItem accesses to 
  129.          the ToDoPriorityList class. 
  130.          If the ToDoPriorityList class dose NOT exist
  131.          in your system, an error occurs at the installation
  132.          process. A dialogbox tells you like this:
  133.                  'Define ToDoPriorityList as Global ?'
  134.          Click 'No' .
  135.          In the next exercises, the ToDoPriorityList class
  136.          will be installed in your system. After that,
  137.          try to install the ToDoItem class.
  138.  
  139.          When you click 'Yes', the ToDoPriorityList will be
  140.          defined as Global variable. When you install the
  141.          the ToDoPriorityList class, you must remove that
  142.          Global variable from the Small Dictionary.
  143.          To do that, enter the following expression in the
  144.           TranscriptWindow or the WorkspaceWindow.
  145.                'Smalltalk inspect'
  146.           Remove the ToDoPriorityList variable from it.
  147.  
  148.            ***** END OF NOTE ************" 
  149.  
  150.           ****** END OF EXERCISE ****** 
  151.  
  152. 
  153.  
  154.  
  155.  
  156.  
  157.  
  158.