Assignment 5
Goals: Practice working with abstract classes, constructors, exceptions.
Instructions
The names of the projects and some of the project files must be exactly the same as specified in the assignment. Failure to do so makes it impossible for the graders to run your submission and results in immediate loss of at least 50% of the homework credit.
Make sure you follow the style guidelines for code indentation.
You will submit this assignment by the deadline using the Web-CAT submission system. We will be practicing its use during the lab next week.
With each homework you will also submit your log file named pairxxx.txt where you replace xxx with your pair number.
On top of every file you submit you will have the names of both partners, and the pair number.
The .txt file will be the log of your work on this assignment. Each log entry will have data and time, who was present (one or both of the partners) and a short comment decribing what you were working on.
Submission instructions:
Submit the class diagram as a .txt or .pdf or .jpg file - or other common file type; nmae it ClassDiagram.xxx
You may divide your solution to Problem 1 into any number of .java files, but make sure that the name of every file matches a name of one of the classes or interfaces defined within that file.
You may use any names for your classes in Problem 1, but make sure you define an interface IData that represents the contents os a cell.
Name the Examples class for Problem 1 ExamplesExcelCells and define within an instance of the data for the cell E5 as
IData e5 = ...
Make sure the methods you define are named value and countArgs
For Problem 2, make sure the message the exception generates matches exactly the one shown in the assignment - as updated.
Submit one file HW5.zip
Due Date: Tuesday, February 12th, 11:59 pm.
Practice Problems
Work out these problems on your own. Save them in an electronic portfolio, so you can show them to your instructor, review them before the exam, use them as a reference when working on the homework assignments.
Work out the problems 21.3, 21.7, 21.8, 21.9
Problem 1: Complex Data
Define the file ExcelCells.java that will contain the entire solution to this problem.
For this problem we use classes that represent data in the cells of a spreadsheet. For each cell we record its row and column, where the cell is located, and the data (IData) stored. An IData item is either a number (int) or a Formula.
Each formula can be one of three possible functions: + (representing addition), max (producing the maximum of the two cells), or * (computing the product) and involves two other cells in the computation.
Design the classes (and interfaces) needed to represent the given information.
Draw the class diagram for the classes and interfaces you have designed.
Make an example of the following spreadsheet segment:
| A | B | C | D | E |
--+------------+----------+-------+-------+------------+
1 | 8 | 3 | 4 | 6 | 2 |
--+------------+----------+-------+-------+------------+
2 | max(B1,E1) | +(A3,C1) | | | *(B2,D1) |
--+------------+----------+-------+-------+------------+
3 | *(A1,A2) | +(B2,E1) | | | max(B3,D1) |
--+------------+----------+-------+-------+------------+
4 | | +(B3,B2) | | | max(B4,E3) |
--+------------+----------+-------+-------+------------+
5 | | +(B4,B3) | | | *(B5,E4) |
--+------------+----------+-------+-------+------------+
Draw this spreadsheet on paper and fill in the values that should show in each cell.
Do not hand this in. It should help you in working out the rest of this problem.
Design the method value that computes the value of this cell.
Hint: follow the recipe... examples really help.
Design the method countArgs that computes the number of cells that contain numbers (not formulas) involved in computing the value of this cell.
Note: Make sure you count every cell only once. So the value of the call B4 is computed by using the values in the cells A1, C1, B1, and E1, and so the method countArgs will return 4.
Problem 2: Constructors
Here is a class that represents a location within our drawing canvas:
class BoundedPt extends Posn{ |
int width = 600; // the width of the canvas |
int height = 400; // the height of the canvas |
|
BoundedPt(int x, int y){ |
super(x, y); |
} |
} |
We would like to make sure that the user cannot define a point outside of the width and height of the canvas.
Design a new constructor that throws an exception if the given coordinates are outside of the bounds, and lets the user know how far out of bounds the values were on each side. So the message should match exactly the following (replacing, of course the word right with the appropriate one from left, right, top, or bottom.
"The given x coordinate was 40 points beyond the right edge"
Note: The call to the super constructor must be the first action of the constructor for this class, even when the constructor throws an exception.