You are here:   ArielOrtiz.com > Software Design and Architecture > Lab 5: Template Method

Lab 5: Template Method

Objectives

During this lab session:

This activity helps the student develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.

Activity Description

This lab is based on chapter 8 of [FREEMAN] and chapter 6 of [ALBAHARI]. It can be developed individually or in pairs.

Make a new folder called chapter08. Place all the files you create in this folder.

  1. You are given the following class definition:

    namespace Headfirst.Templatemethod.Sort {
        
        using System;
        using System.Collections;
        
        public class Person {
            public string Name { get; private set; }
            public DateTime DateOfBirth { get; private set; }
            public Person(string name, DateTime dateOfBirth) {
                Name = name;
                DateOfBirth = dateOfBirth;
            }
            public override string ToString() {
                return string.Format("{0}, born {1:d}", Name, DateOfBirth);
            }        
        }    
    }

    You must modify this class so that its instances become comparable. When comparing two instance of the Person class, first you must compare the month of the DateOfBirth property. In case they happen to be the same, you must then do an ordinal comparison using the Name property, specifically using the last name of each person object. You may assume that all names are given with this format: "FirstName LastName".

  2. Make sure it behaves exactly as expected by the following unit tests. Do not modify this part of the code.

    namespace Headfirst.Templatemethod.Sort {
        
        using System;
        using System.Collections;
        using NUnit.Framework;
            
        [TestFixture]
        public class TestTemplateMethod {
            
            Person[] personArray;
            
            [SetUp]
            public void Init() {
                personArray = new Person[] {
                    new Person("Andreas Köpke", new DateTime(1962, 9, 12)),
                    new Person("Levi Strauss", new DateTime(1829, 2, 26)),
                    new Person("Hans Kollhoff", new DateTime(1946, 9, 18)),                                                
                    new Person("Josef Strauß", new DateTime(1915, 2, 6)),
                    new Person("Die Ärzte", new DateTime(1963, 2, 27)),
                    new Person("Horst Köhler", new DateTime(1943, 9, 22))
                };
            }
            
            [Test]
            public void TestPerson() {
                Assert.IsTrue(personArray[0].CompareTo(personArray[0]) == 0);
                Assert.IsTrue(personArray[0].CompareTo(personArray[1]) > 0);
                Assert.IsTrue(personArray[1].CompareTo(personArray[3]) < 0);
                Assert.IsTrue(personArray[0].CompareTo(personArray[5]) > 0);
            }
            
            [Test]
            public void TestArraySort() {
            
                Array.Sort(personArray);
                
                Assert.AreEqual("Levi Strauss, born 26/02/1829", 
                    personArray[0].ToString());            
                Assert.AreEqual("Josef Strauß, born 06/02/1915", 
                    personArray[1].ToString());
                Assert.AreEqual("Die Ärzte, born 27/02/1963", 
                    personArray[2].ToString());
                Assert.AreEqual("Hans Kollhoff, born 18/09/1946", 
                    personArray[3].ToString());
                Assert.AreEqual("Horst Köhler, born 22/09/1943", 
                    personArray[4].ToString());
                Assert.AreEqual("Andreas Köpke, born 12/09/1962", 
                    personArray[5].ToString());
            }
        }   
    }

Deliverables

To hand in your lab work, follow these instructions:

Evaluation

This activity will be evaluated using the following criteria:

100 The code works as requested.
60-90 The code works, but has some flaws.
20-50 The code doesn't work, but it seams that some amount of time was spent on it.
DA The program was plagiarized.
© 1996-2009 by Ariel Ortiz (ariel.ortiz@itesm.mx)
Made with Django | Licensed under Creative Commons | Valid XHTML | Valid CSS