CSE2305 - Object-Oriented Software Engineering
assessment

 

Assignment 1: solutions


This assignment was due Friday, August 11, 2006.
Total marks: 50.
Contribution to final mark: 5%.

Synopsis


Question 1

[10 marks]

The List::Node class is used to store individual elements in the list, linked by pointers. It is private to the List class since it is part of the internal working of the List class (encapsulation).


Question 2

[10 marks]

List allocates memory each time List::Insert is called. When an object is destroyed it should free any memory allocated by the object using the new operator.

List.h
class List {
 public:
    List();  // constructor
    ~List(); // destructor
    .
    .
    .
};
List.C
// ~List()
// List destructor
//
List::~List()
{
    myCurrent = myHead; // Start at the beginning of the list

while (myCurrent != 0)
{
myCurrent = myHead->GetNext();
delete myHead;
myHead = myCurrent;
} }

 


Question 3

[10 marks]

List.h

class List {
 public:
    .
    .
    .
    bool Search(const DataType& data);
    .
    .
    .
};
List.C

// Search
// search for the data in the list and return true if it exists
// otherwise return false
//
bool List::Search(const DataType& data)
{
 string word;

if (First())
{
do
{
if ( GetCurrent(word) && (word == data) )
{ return true; }
}
while (Next());
}

return false;
}
main.C
cout << "Now, please enter a search term: " << endl;
cin >> word; 
cout << "Search term '" << word << "' was ";
if (!myList.Search(word)) cout << "not ";
cout << "found in the lists." << endl;


Question 4

[10 marks]

Sorting should be done in lexicographic order. In the example below, this is implemented using the standard library lexicographical_compare function. However, you could easily do your own version that compares two strings in a case insensitive manner, one char at a time. Notice the use of the static function nocaseCompare() which uses the function toupper, also included as part of the C++ standard library. (You were given a hint about this in the assignment specification.)
List.h

class List {
 public:
    .
    .
    .
    bool InsertSorted(const DataType& data);

 private:
    .
    static bool nocaseCompare (char c1, char c2);
    .
    .
};
List.C
#include <string>
#include <algorithm>

// InsertSorted
// Insert a new element into a list in the correct (lexicographic) location.
// Assmes an already sorted list.
// Assumes the < operator is defined for the data types
//
bool List::InsertSorted(const DataType& data)
{ 
   string word;

if (First())
{ while ( GetCurrent(word)
&& lexicographical_compare(word.begin(), word.end(), data.begin(), data.end(), nocaseCompare) )
{ Next(); }
}

Insert(data);
return true; } bool List::nocaseCompare (char c1, char c2)
{
return toupper(c1) < toupper(c2);
}


Question 5

[10 marks]

UML notation for the List class. Note the compositional relationship between List and Node. Depending on which flavour of UML you use (how old your text book and the specification), these depictions of Node as an inner class of List may (or may not) be correct. You will not lose marks for using a minor variation of what apppears below as long as the relationships are shown clearly.

(a)

(b)

(c)


This material is part of the CSE2305 - Object-Oriented Software Engineering course.
Copyright © Alan Dorin 2006, Jon McCormack, 2005. All rights reserved.

Last Modified: August 18, 2006