CSE2305 - Object-Oriented Software Engineering
Assessment

 

Assignment 2


This assignment is due before 11.59pm Friday, 1 September 2006 ( ).
Total marks: 50.
Contribution to final mark: 5%.

Synopsis


Question 1

Part A [5 marks]

In writing software for computer graphics, colours are usually specified by the intensity of their Red, Green and Blue components in a vector like this (red-intensity, green-intensity, blue-intensity). For instance, a colour of pure blue would be represented as the value (0, 0, 255), red as (255,0,0), black as (0,0,0) and mid-grey as (127,127,127). The values of colour components in this system are integers that range from 0 to 255 (inclusive).

Write a class called Colour that stores the red, green and blue components of a colour.

Your class will need to contain the following methods:

  1. A default constructor¤ that initialises the (zero) Colour object.
  2. A constructor that initialises the Colour object with three specified values for the colour's components.

  3. A destructor¤ (but only if one is required).

  4. A member function Colour::Set(int red, int  green, int  blue) that allows you to set the three colour components after construction.

  5. Member functions that return the values of the Red, Green and Blue components: Colour::Red(void), Colour::Green(void), Colour::Blue(void)

  6. A member function Colour::Output(ostream& s) that prints the colour components to the stream in the following human-readable format: [ red, green, blue ].

  7. Advanced: (Optional) Look ahead in the lecture notes and your text books to find out how to write an operator<< that will allow you to write out a Colour object.

 


Part B [25 marks]

An associative array is an array that stores one data type (the value) indexed by another (the key). This is a generalisation of the standard array in C which only allows indexing by a number. For example:

Colour deepPurple(127, 0, 127);

shades["deep-purple"] = deepPurple;
cout << "The components of " << colourName << " are " << shades[colourName] << endl;

Write a class¤ called Array that implements an associative array. (Make the Array class an array of Lists and implement a hashing function. See How To Proceed below for further information.  You will need to implement this software yourself. Use of the STL hashed associative containers will not receive any marks.) The key for the array will be a std::string.  Elements stored in the associative array will contain data, also of type Colour (as you just defined it above). Your Array class should be able to store any number of elements. The example above uses operator overloading to overload operator[], operator= and operator <<. For this assignment, there is no need to overload operators, so member functions will be used to get, set and print out the data.

Your Array class¤ should provide the following methods:

Classes¤ such as the List and Array classes¤ are called Containers¤.  Container Classes¤ are used to store any type of data.  Consider for the future how you could specify and store arbitrary data types for the key and value.


Question 2

[20 marks]

Using the Array class¤ you implemented in the previous question write a class¤ called ColourLookUp, that can load the list of colours stored in this file, and then be used to find the colour components for the supplied colour names. Make sure an error is reported if the file can't be opened successfully.

Your ColourLookUp class¤ should provide the following methods:

Use your ColourLookUp class¤ to write a simple colour lookup program. The program should load a list of colour names and colour components from the specified file into a ColourLookUp object, then prompt for a colour name, returning the corresponding colour components if found, or the string "No colour found for colourname" (where colourname is the name of the colour entered). This should repeat until the user terminates the cycle. The program then quits.


How to Proceed

Associative arrays are best implemented as a hash table. You will need to devise a suitable hash function to hash a key to the appropriate position in the array... or you can take this hint:

Use the following protected data members and function for hashing a key:

static const uint shift = 6;
static const uint mask = ~0U << ((sizeof(uint) * 8) - shift);
uint hash(const KeyType& key) const;
Use the following definition of the hash function that will take a key and hash it to an unsigned int:
uint Array::hash(const KeyType& key) const 
{
    uint result = 0;

    for (uint i = 0; i < key.size(); ++i)
    { result = (result & mask) ^ (result << shift) ^ key[i]; } 
    // cout << "hash(" << key << ") = " << result % myTableSize << endl; 

    return result % myTableSize;
 }

Hash tables need to handle collisions (when two different keys hash to the same location).  To handle collisions, make sure your Array class¤ stores a List of elements at each location in the array (See Assignment 1. If you prefer to, you may use the sample answer provided from Assignment 1 as the basis for your new code, rather than your own implementation).


What to submit for assessment

You should submit the following:

See the submit page for details on how to submit your assignment.


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

Last Modified: August 16, 2006