CS 2102 - Dterm 10
Homework 3 - Polymorphism: Designing methods for Complex Data
Due: Tuesday, March 30 at 5pm
Acknowledgement: This assignment is a modified version of an assignment
designed by Dr. Viera Proulx.
This assignment and all subsequent assignments are to be done with your
homework partner.
Assignment Goals
- To gain additional experience designing programs that take advantage of Java's dynamic method dispatch
Restrictions
For Homework 3, the only kinds of statements you may use in your method implementations are the if and return statements. In particular, you are not allowed to use any assignment statements (i.e. the "=" operator) except as they have been used in constructors. Also, you are not allowed to use any of Java's type-checking mechanisms
.
Preliminaries
For this assignment, you will design classes that represent the values
in the cells of a spreadsheet (such as might be created with a program
like Excel). For each cell, the information that is recorded is
the row and column number of the cell, and the data that is stored in
the cell. The data can be either a numerical (integer) value, or a
formula. A formula can be one of three possible functions: +
(computing the sum of two cells), mn (producing the minimum of two cells),
or * (computing the product of two cells).
Here is a class diagram:
+------------+
| Cell |<---------------------+-+
+------------+ | |
| int row | | |
| int col | | |
| IData data |-+ | |
+------------+ | | |
| | |
v | |
+-------+ | |
| IData | | |
+-------+ | |
/ \ | |
--- | |
| | |
----------------------------- | |
| | | |
+------------+ +----------+ | |
| Number | | Formula | | |
+------------+ +----------+ | |
| int number | | Cell op1 |---+ |
+------------+ | Cell op2 |-----+
+-| IFun fun |
| +----------+
v
+------+
| IFun |
+------+
/ \
---
|
------------------------
| | |
+------+ +---------+ +-------+
| Plus | | Minimum | | Times |
+------+ +---------+ +-------+
Here is an example of a spreadsheet that conforms to the above
definitions (for ease of reading, the columns are labelled
A, B, C... to represent columns 1, 2, 3...):
| A | B | C | D | E |
---+----------+----------+----------+----------+-----------+
1 | 8 | 3 | 4 | 6 | 2 |
---+----------+----------+----------+----------+-----------+
2 | mn A1 E1 | + B1 C1 | | | * B2 D1 |
---+----------+----------+----------+----------+-----------+
3 | * A1 A2 | + B2 B1 | | | mn A3 D1 |
---+----------+----------+----------+----------+-----------+
4 | | + B3 B2 | | | mn B4 D1 |
---+----------+----------+----------+----------+-----------+
5 | | + B4 B3 | | | * B5 E4 |
---+----------+----------+----------+----------+-----------+
Problems
- On a piece of paper, draw this spreadsheet, replacing each formula
with the value that would be stored in that cell. We won't be collecting
or grading your picture, but you should do this step, anyway (you'll be
very happy you did this step when you design test cases for your methods).
For the remaining problems, you should start with these
class and interface
definitions, adding content to them as necessary. (All the definitions are provided
in one
text file; you should create separate Eclipse .java files for each class and interface, as usual). You don't need to define any additional classes or interfaces (except an Examples class for testing). As you develop methods for a class, remember to add those
methods to the class's template, and to design tests for each method.
- In the
Examples
class, define cells representing each of the
occupied cells in the given picture.
- Develop the method
value
that computes the (integer) value of this cell.
- Develop the method
countFunc
that counts the number of
function applications needed to compute the value of this cell.
What to Turn In
Create an archive of your project, and name your .zip file using the
naming conventions for files.
Turn in your .zip file using
web-based turnin.