On this page:
Instructions
Practice Problems
Problem 1: Embroidery
Problem 2: A Picture is worth a thousand words
7.7

Assignment 2: Designing methods for complex data

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

Instructions

This homework should be done with your lab partner.

Be very, very careful with naming! Again, the solution files expect your submissions to be named a certain way, so that they can define their own Examples class with which to test your code. Therefore, whenever the assignment specifies:
  • the names of classes,

  • the names and types of the fields within classes,

  • the names, types and order of the arguments to the constructor,

  • the names, types and order of arguments to methods, or

  • filenames,

...be sure that your submission uses exactly those names. Additionally, make sure you follow the style guidelines that the handin server enforces. For now the most important ones are: using spaces instead of tabs, indenting by 2 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 deadlines using the course handin server. Follow A Complete Guide to the Handin Server for information on how to use the handin server. You may submit as many times as you wish. Be aware of the fact that close to the deadline the server may slow down to handle many submissions, so try to finish early. 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 you work on.

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 1: Embroidery

The art of creating decorative designs on fabric using a needle and thread is called embroidery. For instance, the following designs for a tree motif, a flower motif or bird motif may be created by an embroidery stitch such as a cross stitch or a chain stitch:

The following DrRacket data definition describes a simple piece of embroidery:

;;An EmbroideryPiece is a (make-embroidery-piece String Motif)
(define-struct embroidery-piece (name motif))
 
;; Motif is one of
;; -- CrossStitchMotif
;; -- ChainStitchMotif
;; -- GroupMotif
 
;; A CrossStitchMotif is a (make-cross-stitch-motif String Double)
(define-struct cross-stitch-motif (description difficulty))
;; interpretation: difficulty is a number between 0 and 5, with 5 being the most difficult
 
;; A ChainStitchMotif is a (make-chain-stitch-motif String Double)
(define-struct chain-stitch-motif (description difficulty))
;; interpretation: difficulty is a number between 0 and 5, with 5 being the most difficult
 
;; A GroupMotif is a (make-group-motif String [List-of Motif])
(define-struct group-motif (description motifs))

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.

A reminder on naming conventions: For lists of data, the names of the interface should always start with ILo, while the two classes’ names start with MtLo for the empty lists and ConsLo for the nonempty lists; all three of these names should be 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.

In the ExamplesEmbroidery class design the example of the following:

Name this example pillowCover Our test program will check that the field pillowCover in the class ExamplesEmbroidery represents this information. (You may name the other motifs, and all the items inside them, anything you like, though the names should be reasonably descriptive.)

Problem 2: A Picture is worth a thousand words

Define the file Pictures.java that will contain the entire solution to this problem.

For this problem, you’re going to implement a small fragment of the image library you’ve been using for Fundies 1 and will be using in Fundies 2. Each picture is either a single Shape or a Combo that connects one or more pictures. Each Shape has a kind, which is a string describing what simple shape it is (e.g., "circle" or "square"), and a size. (For this problem, we will simplify and assume that each simple shape is as tall as it is wide.) A Combo consists of a name describing the resulting picture, and an operation describing how this image was put together.

There are three kinds of operations: Scale (takes a single picture and draws it twice as large), Beside (takes two pictures, and draws picture1 to the left of picture2), and Overlay (takes two pictures, and draws top-picture on top of bottom-picture, with their centers aligned).