[an error occurred while processing this directive] File: index.shtml Creator: George Ferguson Created: Tue Nov 6 10:02:09 2012 Time-stamp: [an error occurred while processing this directive] File: templates/doc-start.shtml Creator: George Ferguson Created: Tue Dec 6 12:31:29 2011 Time-stamp: [an error occurred while processing this directive] File: site-settings.shtml Creator: George Ferguson Created: Tue Dec 6 13:49:49 2011 Time-stamp: Site (or subsite)-wide settings. [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] File: head.shtml Creator: George Ferguson Created: Tue Dec 6 12:34:15 2011 Time-stamp: SSI variables for this template: head_title if given, else ``sitename | title'' [an error occurred while processing this directive] File: head-title.shtml Creator: George Ferguson Created: Tue Dec 6 14:29:52 2011 Time-stamp: SSI variables for this template: head_title: complete content of title element if given site_title trailing part of title (if given) section_title middle part of title (if given) page_title leading part of title (if given) title title shown on page, also used as leading part of title (if given) [an error occurred while processing this directive] (none) [an error occurred while processing this directive] File: head-meta.shtml Creator: George Ferguson Created: Tue Dec 6 14:29:18 2011 Time-stamp: SSI variables for this template: meta_description meta_keywords meta_generator [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] File: head-stylesheets.shtml Creator: George Ferguson Created: Tue Dec 6 14:22:58 2011 Time-stamp: SSI variables for this template: site_stylesheet, page_stylesheet stylesheet0, stylesheet1, ...: URL for stylesheets [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] File: head-scripts.shtml Creator: George Ferguson Created: Tue Dec 6 14:23:19 2011 Time-stamp: SSI variables for this template: site_script, page_script script0, script1, ...: URLs of javascript scripts [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] File: body-start.shtml Creator: George Ferguson Created: Tue Dec 6 13:20:46 2011 Time-stamp: SSI variables for this template: body_class page_class page_id [an error occurred while processing this directive] [an error occurred while processing this directive] File: body-header.shtml Creator: George Ferguson Created: Tue Dec 6 13:46:12 2011 Time-stamp: Content above banner, if any. [an error occurred while processing this directive] File: body-banner.shtml Creator: George Ferguson Created: Tue Dec 6 13:27:12 2011 Time-stamp: Duplicate URCS Drupal banner. hajim urcs
Computer Science @ Rochester
Computer Science Department : Hajim School of Engineering & Applied Sciences Skip to main content [an error occurred while processing this directive] File: body-title.shtml Creator: George Ferguson Created: Tue Dec 6 14:34:49 2011 Time-stamp: SSI variables for this template: section_name First line of title on page (if any) title Title shown on page

[an error occurred while processing this directive] CSC170: Introduction to Programming and the Web
Fall 2012
[an error occurred while processing this directive]
Assignment 8: Objects: Part 1

  1. [5 pts] Javascript has a built-in Date object, but it really doesn't have many useful methods. Let's build our own.
    1. Create a constructor for a kind of object named CalendarDate. It should take three parameters: the day, month, and year of the date it represents and store them in the constructed object instance.
    2. Add a method isChristmas() that tests whether a CalendarDate is Christmas Day (December 25). A test method like this always returns a boolean value (true or false).
    3. Add a method next() that increments a CalendarDate instance to the next day. Note that it is not enough to simply increment the day field. Watch for special cases.
    4. Add a method previous() that decrements a CalendarDate instance.
    5. Add a method increment(n) that increments a CalendarDate by the given number of days. Note that n can be positive, negative (in which case it means decrement), or even zero.
    Demonstrate each of the aspects of your CalendarDate object with reasonable output to the page's document.
  2. [5 pts] We often need to represent people and the relationships between them in our programs. So let's do that.
    1. Create a constructor for a Person object. It should take two parameters: the person's first and last names and store them in the constructed object instance. It should also setup additional properties named mother, father, siblings, and children. The first two will hold references to other Person objects representing the person's mother and father respectively. The latter two will be arrays that contain references to other Person objects that are in the corresponding relationship. In your constructor, initialize these to an empty array.
    2. Add a method setParents(mother, father) that takes as arguments two Person objects and makes them the parents of the Person object on which the method is invoked.
    3. Add a method addSibling(sibling) that adds its argument (another Person object) to the array of siblings of the Person object on which the method is invoked.
    4. Add a method addChild(child) that works similarly but adds to the array of children of the Person object on which the method is invoked.
    5. Observe that when you make one Person another Person's parent, you are also implicitly adding to the first Person's (the parent's) children. Similarly for addSibling and addChild. Adjust your methods to maintain these so-called reciprocal relationships.

      Edit 12 Nov 2012: A student has pointed out the following to me:

      Since parentof(child) is not a one-to-one function (children have two parents), how are we to maintain a reciprocal relationship when we add a child to person? Without knowing whether that person is supposed to be the mother or father (person doesn't have a "gender" or similar in its constructor), we can't pick which of the two to assign person to for the child we are adding to person...

      In a normal tree data structure, this sort of thing doesn't matter, but for this particular example, it isn't clear how we're supposed to implement this from the prompt, so I was wondering how we were expected to handle this.

      They are quite right. Therefore you should only worry about maintaining the reciprocal relationship for the siblings property. That's enough to make the point that this part of the question was making anyway.

    Demonstrate each of the aspects of your Person object with reasonable output to the page's document.

    Note: You may need some methods for printing the contents of the arrays, if the defaults don't look good. Don't forget Array.join in this context.

Things to think about

  • Objects are simply collections of property-value pairs, That is, they are a collection of properties and the corresponding values of those properties for the object instance.
  • Object properties can have values that are simple (string, number, boolean), or complex (array, object). You can have arrays of objects. Objects are just another type of value.
  • Methods define actions that can be performed with or on an object instance. Methods can return any kind of value (including array or object references), or may return nothing (technically, these return undefined).
  • Constructors define the kinds of objects you can create and use. You generally initialize objects in the constructor. Constructors may or may not take parameters, There are ways of handling optional parameters, which will have value undefined if they weren't given in the invocation of the constructor, but Javascript is not as flexible about this as some other programming languages.

On to Part 2 when it is available...

[an error occurred while processing this directive] File: doc-finish.shtml Creator: George Ferguson Created: Tue Dec 6 13:46:48 2011 Time-stamp: [an error occurred while processing this directive] File: body-footer.shtml Creator: George Ferguson Created: Tue Dec 6 14:43:56 2011 Time-stamp: Content at bottom of page, if any.

Last update: Monday, 12-Nov-2012 09:22:40 EST

[an error occurred while processing this directive] File: body-finish.shtml Creator: George Ferguson Created: Tue Dec 6 13:47:36 2011 Time-stamp: [an error occurred while processing this directive]