COMP 304B Object-Oriented Software Design -- Assignment 2

COMP 304B Object-Oriented Software Design - Assignment 2

 

Solution

[printable version of this document (pdf)]| [COMP 304B Home]


The requirements for this assignment are described on a here.

The design is given in the figure below.

design.png

The dia file is here. A larger image is here.

Class Diagram

Redesign first assignment

Classes in the Formula inheritance tree have only to be modified slightly: the __str__ method has been added to every class in the inheritance hierarchy.

More importantly, the CellRef class has been modified as follows:

We have three new classes: Spreadsheet, SpreadsheetData and SpreadsheetCell. The first one is self-explanatory.

SpreadsheetCell

The depends_on attribute is a list that contains the absolute coordinates (tuple containing two nonnegative integers) of the cells the object depends on.

We added the private attribute __this_spreadsheet to refer to the SpreadsheetData object the object belongs to. The methods are described below (the methods that were not specified in the requirements are in bold):

__init__
get
Return the contents of the __formula attribute
set
parse_formula
Parse the attribute __formula_string to build the attribute __formula. Return TRUE only if the parsing is successful.
get_depend
Do a DFS on __formula to update the depends_on attribute.
get_value
Return the content of the __value attribute.
get_coords
Return the tuple (__row, __column).
set_spreadsheet
get_spreadsheet
Return the __this_spreadsheet attribute.
evaluate
Update the value attribute by evaluating the __formula (recall: Formula.evaluate() returns a Number object).

SpreadsheetData

The dictionary of SpreadsheetCell references is the attribute __cell_data. As explained in the requirements, the entries are indexed by absolute coordinates, i.e., tuples containing two nonnegative integers.

The __check_dependencies method (see below) sorts the cells in an order appropriate for efficient calculation. Since a dictionary is not ordered, we need an ordered data structure to hold the sorted coordinates. This is the role of the __sorted_data attribute, which is a sorted list of absolute coordinates.

The methods are described below (no new methods were added):

__init__
The constructor does nothing special.
get_cell
get_cell(x, y) returns the SpreadsheetCell object __cell_data[(x, y)], or None.
set_cell
Basically, set_cell(cell) registers the SpreadsheetCell object cell in the current Spreadsheet. Since a SpreadsheetCell object knows its coordinates, there is no need to specify row and column in the method's parameters. If we register a cell in a position where a cell is already present, the latter is just overwritten (disappears thanks to garbage collection).

Pseudo code for set_cell(cell):

evaluate
Update the whole spreadsheet
__check_dependencies
This method does a topological sort of the cell dependencies (updates the __sorted_data attribute). For this it needs to look at the depends_on attribute of each SpreadsheetCell object in the dictionary __cell_data.

The method returns FALSE iff there is a cyclic dependency (including self-loops).

Other Considerations

Initially the SpreadsheetData object is empty. There are two ways to create a new SpreadsheetCell object:

(NOTE: a cell cannot be edited. It can only be overwritten). The method SpreadsheetData.evaluate is called every time a new SpreadsheetCell object is registered.

Some of the sources of errors are:

Below is the pseudo code for the methods CellRef.evaluate and CellRef.get_spreadsheet_coord: this should explain why a CellRef knows its SpreadsheetCell, and why a SpreadsheetCell knows its SpreadsheetData (Note: the method CellRef.__str__ will be implemented in a similar manner). The code also shows that in case a SpreadsheetCell object refers to an empty cell, the latter cell is interpreted as containing the value 0..

Pseudo code for CellRef.evaluate:

Sequence Diagram

In this use case, we assume we start with an empty SpreadsheetData object named s1.

SeqDia.png

The first part illustrates how a cell is added by explicitly typing a formula. Let the variable string holds the typed formula. We first instantiate a SpreadsheetCell object as
cell1 = SpreadsheetCell(1, 1, string)
In this case, the cell is to be added at coordinates (1, 1). The actual insertion is done by calling
s1.set_cell(cell1)

The second part illustrates how the cell just added can be copied into coordinates (1, 2). We first instantiate a new SpreadsheetCell object without providing a string, and get a reference to the SpreadsheetCell object at coordinates (1, 1):
cell2 = SpreadsheetCell(1, 2)
cell1 = s1.get_cell(1, 1)
To copy cell1.__formula into cell2.__formula, we use the get and set methods:
form1 = cell1.get()
cell2.set(form1)
Registering the new cell in the spreadsheet is done as before:
s1.set_cell(cell2)

Note how the whole spreadsheet is updated whenever a new cell is added.


Translated from TEX by TTH, version 3.02 (minor customizations by HV). On 1 Apr 2003, 20:54.