Assignment 2: Designing methods for complex data Solution

$35.00 $30.80

Description

Goals: Learn to design methods for complex class hierarchies. Practice designing the representation of complex data.

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 1

// 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)

Your submission sould consist of the following files:

· pair-user1-user2.txt – your log file

· WP.java – the data definitions and examples for Problem 1

· ExcelCells.java – the data definitions and examples for Problem 2

all combined into one HW2.zip file.

Due Date: Monday, September 23rd, 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 — make sure you use these names to define your interfaces and classes.

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 with at least two of each kind of items and at least two levels of links.

· 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 page:

 WP: with title “My Friends” at url “myfriends.org”

     that contains the following items:

     — text “This is Annie”

     — image of Annie in the file “annie” of size 230

          and file type “jpeg”

     — text “This is Kevin”

     — image of Kevin in the file “kevin” of size 400

          and file type “png”

     — link with the label “Here are Bob’s friends” to the

          web page named “Bob’s Friends”

 

WP: with the title “Bob’s Friends” at url “bob-friends.org”

    that contains the following items:

    — text “My friend Jackie”

    — image of Jackie in the file “jackie” of size 300

         and file type “png”

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

“annie.jpeg, kevin.png”

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 addition), min (producing the minimum 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 | min(B1,E1) | +(A1,C1) |       |       |  *(B2,D1)  |

–+————+———-+——-+——-+————+

3 |  *(A1,A2)  | +(B2,E1) |       |       | min(A3,D1) |

–+————+———-+——-+——-+————+

4 |            | +(B3,B2) |       |       | min(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, and E1, and so the method countArgs will return 3.