Assignment 2: Designing methods for complex data
Goals: Learn to design methods for complex class hierarchies. Practice designing the representation of complex data.
Please note changes in the second problem: modified method countArgs and a new method countFuns. The original problem was too complex.
The change has been posted on Friday, 17 January at 5:37 pm
1 Instructions
The names of the classes must be exactly the same as specified. The same is the case for the names and types of the fields within the class, as well as the order in which they are defined and listed as the constructor arguments. This allows us to design our own Examples class that tests your program.
Make sure you follow the style guidelines that WebCAT enforces. For now the most important ones are: using spaces instead of tabs, indenting by 4 characters, following the naming conventions (data type names start with a capital letter, names of fields and methods start with a lower case letter), and having spaces before curly braces.
You will submit this assignment by the deadline using the Web-CAT submission system. You may submit as many times as you wish. Be aware of the fact that close to the deadline the WebCAT system may slow down to handle many submissions - so try to finish early.
With each homework you will also submit your log file named pair-user1-user2.txt where you replace user1 and user2 with the usernames of the two partners.
On top of both files you will have five lines of comments as follows:
// assignment 2 |
// partner1-last-name partner1-first-name |
// partner1-username |
// partner2-last-name partner2-first-name |
// partner2-username |
(In the text file you do not need the two slashes)
There will be a separate submission for each problem - it makes it easier to grade each problem, and to provide you with the feedback for each problem wou work on.
The two submissions will be organized as follows:
Submission HW2P1: The log file and the WP.java file in one .zip file
Submission HW2P2: The ExcelCells.java file
Due Date: Tuesday, January 21st, 10: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.
Problem 10.6 on page 102
Problem 11.2 on page 113
Problem 12.1 on page 125
Problem 14.7 on page 140
Problem 15.2 on page 149
Problem 15.3 on page 149
Problem 15.8 on page 171
Problem 1
The following DrRacket data definition describes the contents of a web site:
;;A Web Page (WP) is (make-wp String String [Listof Item (ILoItem)]) |
(define-struct wp (url title items)) |
|
;; An Item is one of |
;; -- Text |
;; -- Image |
;; -- Link |
|
;; A Text is (make-text String) |
(define-struct text (contents)) |
|
;; An Image is (make-image String int String) |
(define-struct image (file-name size file-type)) |
|
;; A Link is (make-link String WP) |
(define-struct link (name page)) |
We are giving you the names of the classes or interfaces you will probably need
—
For lists of data, the names of the interface should be always starting with ILo, the names two classes should be always starting with MtLo for the empty lists and ConsLo for the nonempty lists, each of these followed by the name of the datatype of the elements of the list. So we would have ILoString, MtLoString, ConsLoString to represent lists of Strings, ILoBook, MtLoBook, ConsLoBook to represent lists of Books, etc.
Describe in English, on paper a web page that links to another one, which again contains a link to the next level and this collection of web pages contains at least two of each of other types of items (text, images).
Draw a class diagram for the classes that represent this data definition.
Define Java classes that represent web pages as defined above.
Design the data representation of the example you have defined earlier.
Name your Examples class ExamplesWP
In the ExamplesWP class design the example of the following web pages:
WP: with title "Our team" at url "panthers.org"
that contains the following items:
-- text "Team picture"
-- image of the team in the file "team" of size 340
and file type "jpeg"
-- text "Our coach"
-- image of the team coach in the file "coach" of size 300
and file type "png"
-- link with the label "Our fans" to the
web page named "Panther's fans"
WP: with the title "Panther's Fans" at url "panther-fans.org"
that contains the following items:
-- text "Jesse"
-- image of Jesse in the file "jesse" of size 300
and file type "jpeg"
Name this example myWP. Our test program will check that the instance myWP in the class ExamplesWP represents this information.
Design the method totalImageSize that computes the total size of all images in this web page and all web pages that are linked to it.
Design the method textLength that computer the number of letters in all text that appears on the web site starting at this web page. This includes the contents of the text, the names of the image files plus the file type (but not the typical dot that is used in the full file name), the labels for links, and the titles of the web pages.
Design the method images that produces one String that has in it all names of images on this web site, given with their file types, and separated by comma and space.
So for the example above this String would start with
"team.jpeg, coach.png, jesse.jpeg"
Note: You can combine two Strings with a + operator, or by invoking the method concat (e.g. s1.concat(s2) produces a new String appending the String s2 to the String s1.)
Note: There is a comma and space between any two entries, but not after the last one.
Problem 2
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 subtraction), 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.
In the ExamplesExcelCells class define an example of the following spreadsheet segment:
| A | B | C | D | E |
--+------------+----------+-------+-------+------------+
1 | 8 | 3 | 4 | 6 | 2 |
--+------------+----------+-------+-------+------------+
2 | max(B1,D1) | -(A1,C1) | | | -(A3,A2) |
--+------------+----------+-------+-------+------------+
3 | *(A1,A2) | *(B2,E1) | | | max(A3,D1) |
--+------------+----------+-------+-------+------------+
4 | | *(B3,B2) | | | max(B4,E3) |
--+------------+----------+-------+-------+------------+
5 | | -(B4,B3) | | | -(E4,B5) |
--+------------+----------+-------+-------+------------+
Define each cell with the name of the type cellA1 or cellE4.
Our test program will use this data to test your methods.
Add to your examples one more example cell for each of the possible formulas. Do not modify the existing cells. Include a copy of the above example, with your additional sample cells shown as a block comment in your ExamplesExcelCells class.
Before you start designing the methods, draw this spreadsheet on paper and fill in the values that should show in each cell. Alternately, you can actually build an Excel spreadsheet (or use some other spreadsheet application) with the cell values as shown.
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 each time it is referenced.
So the value of the cell B4 is computed by applying the formula
B3*E2 = (B2*E1)*E2 = ((A1-C1)*E1)+E2 = ((A1-C1)*E1)+(A3-A2) = ((A1-C1)*E1)+((A1*B1)-MAX(B1,D1))
and the result will be 7. If we added a cell D2 with formula B1*B1, the result for that cell would be 2.
Design the method countFuns that computes the number of function applications involved in computing the value of this cell.
So to compute the value of the cell B4 we apply the formulas:
B3*E2 = (B2*E1)*E2 = ((A1-C1)*E1)+E2 = ((A1-C1)*E1)+(A3-A2) = ((A1-C1)*E1)+((A1*B1)-MAX(B1,D1))
and the result will be 6 formulas.