CSE2305 - Object-Oriented Software
Engineering
Assessment
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:
Colour
object.Colour
object with three specified values for the colour's components.Colour::Set(int red, int green, int blue)
that allows you to set the three colour components after construction. Colour::Red(void), Colour::Green(void), Colour::Blue(void)
Colour::Output(ostream& s)
that prints the colour components to the stream in the following human-readable format: [ red, green, blue ].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:
Array
object.Array::Insert(const string& key,
const Colour& value)
,
that inserts the specified value into the array using index key.Array::Delete(string key)
,
that deletes the specified key and the value associated with it.Array::Exists(string key)
,
that searches for the specified key and returns true
if
the key was found and false
otherwise. Array::Find(string key)
,
that searches for the specified key and returns the data associated
with the key (if it exists), or a zero (0,0,0) colour otherwise.
(What are some problems with returning the colour black (0,0,0) in this instance? Can you think of a better solution? If so, implement it!) 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.
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:
Array
data
member¤¤
ColourLookUp::LookUp(string colourName)
,
that returns the Colour
for the colour name if it exists,
or black (0,0,0) otherwise.
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.
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:
|
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).
You should submit the following:
Makefile
in the root directory of your assignment code (not in a sub-directory) with 2 targets: the default target builds the
program(s), the clean target removes the executable and any intermediate
(e.g. .o files created during compilation).README
file briefly explaining what you have
done (including testing) and how to perform the tests on your submission.vi
).See the submit page for details on how to submit your assignment.
Last Modified: August 16, 2006