7.2

#### Assignment 2: Designing methods for complex data

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

##### Instructions

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.

Due Date: Thursday, January 23rd, 9:00 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:Circuit Components

A circuit component is a piece of hardware with two terminals. Much like legos, circuit components can be connected to each other to create electrical circuits.

A multi-meter is an instrument used by technicians and engineers. It is used to measure voltages and currents. A multi-meter has two probes: red and black. When they are connected to the two terminals of a circuit component, it can measure the voltage difference between them, or the current passing from one to the other.

A circuit component can be many different things. A simple type of component is a battery. A battery offers a constant (DC) voltage between its terminals. Each battery has a nominal resistance (so that connecting its terminals to each other doesn’t burn the battery). A battery is represented visually as follows:

Specifically, the longer vertical line is the positive terminal. The voltage (measured in Volts) is the voltage of the left terminal with respect to the right terminal. That is why the voltage of the second example above is -5V.

Another simple component is a resistor. It is a passive component (i.e. it does not produce any voltage or current) with a fixed resistance, measured in ohms ($$\Omega$$). It is represented visually as follows:

A circuit component can be created by combining two circuit components. This can be done in two ways: series and parallel combinations.

In a series combination the two components are connected “one after the other”, as shown below:

In a parallel combination the two components are connected by connecting their respective matching terminals, as shown below:

Using these combinations, one can create more complicated components, such as the one below:

The following DrRacket data definition describes a circuit:

 ;;A Circuit is one of ;; -- Battery ;; -- Resistor ;; -- Series ;; -- Parallel ;; A Battery is a (make-battery String Double Double) (define-struct battery (name voltage nominal-resistance)) ;; A Resistor is a (make-resistor String Double) (define-struct resistor (name resistance)) ;; A Series is a (make-series Circuit Circuit) (define-struct series (first second)) ;; A Parallel is a (make-parallel Circuit Circuit) (define-struct parallel (first second))
• Draw a class diagram for the classes that represent this data definition. (It is optional to submit your diagram. You can draw this on paper, or in ASCII art as a comment in your submitted file.)

• Define Java classes that represent the definitions above.

• Describe (in English, or in a diagram, or in code...) the contents of a circuit that has at least two batteries, two resistors and one way of combining circuits. It should be different from the ones below.

• Design the data representation of the example you just described.

• Name your examples class ExamplesCircuits

In the ExamplesCircuits class design at least 5 examples of circuit components, including the following:
• batteryOne: A battery named "B 1", with a voltage of 10V and a nominal resistance of 25$$\Omega$$.

• resistorOne: A resistor named "R 1", with a resistance of 100$$\Omega$$.

• simpleSeries: A circuit component mimicking the figure above illustrating the series combination.

• complexCircuit: A circuit component mimicking the figure above illustrating the complicated circuit. The parallel combinations are built from top to bottom, and series combinations are built from left to right (i.e. the series circuit with batteries $$BT_4$$ and $$BT_2$$ is made first. Then a series circuit with $$R_4$$ and $$R_5$$ is made, and is connected in parallel with $$R_1$$, and the resulting circuit in parallel to $$R_2$$, and so on.

Name the different components exactly as in the figures, but replace subscripts with spaces. For example, change $$BT_2$$ with "BT 2".

• Design the method countComponents that counts the number of simple components (batteries and resistors) in this component. For example, the number of simple components in the batteryOne and complexCircuit are 1 and 7 respectively.

• Design the method totalVoltage that computes the voltage between the two terminals of this circuit component. For example, the voltages in batteryOne and complexCircuit are 10 and 30 respectively.

If two components are combined in series, then their voltages are added (remembering that the voltage may be negative). In a parallel combination, the voltage is the same as the voltage across any component (combining two different voltages in parallel is a bad idea, and should never be done. For now, we will avoid this by creating valid examples).

• Design the method totalCurrent that computes the current flowing from the left terminal to the right terminal of this circuit component. The current is defined as the ratio of voltage and resistance. For example, the currents in simpleSeries and complexCircuit circuit are 0.08 and 0.6 respectively.

If two resistances $$R_1$$ and $$R_2$$ are combined in series, the total resistance is $$R_1$$+$$R_2$$. If they are connected in parallel, their resistance is $$R$$ which is computed using the equation $$\frac{1}{R} = \frac{1}{R_1} + \frac{1}{R_2}$$.

• Design the method reversePolarity that computes a circuit component identical with this component, but with the voltages reversed.

Submit your work in a file called Circuit.java.

### Problem 2:“Webpages”

The following DrRacket data definition describes the contents of a webpage:

 ;;A Web Page is (make-web-page String String [Listof Item]) (define-struct web-page (title url 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 WebPage (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.

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.

• Draw a class diagram for the classes that represent this data definition.

• Define Java classes that represent web pages as defined above.

• Describe (in English, or in a diagram, or in code...) the contents of a webpage that has at least one text, two images and three links to pages.

• Design the data representation of the example you just described.

• Name your examples class ExamplesWebPage

In the ExamplesWebPage class design the example of the following web pages:

 WebPage: with title "Fundies II" at url "ccs.neu.edu/Fundies2" that contains the following items: -- text "Home sweet home" -- image of the lab in the file "wvh-lab" of size 400 and file type "png" -- text "The staff" -- image of the professors in the file "profs" of size 240 and file type "jpeg" -- link with the label "A Look Back" to the web page named "HtDP" -- link with the label "A Look Ahead" to the web page named "OOD" WebPage: with the title "HtDP" at url "htdp.org" that contains the following items: -- text "How to Design Programs" -- image of the book cover in the file "htdp" of size 4300 and file type "tiff" WebPage: with the title "OOD" at url "ccs.neu.edu/OOD" that contains the following items: -- text "Stay classy, Java" -- link with the label "Back to the Future" to the web page named "HtDP"

Name the "Fundies II" example fundiesWP. Our test program will check that the field fundiesWP in the class ExamplesWebPage represents this information. (You may name the other two web pages, and all the items inside them, anything you like, though the names should be reasonably descriptive.)

• Design the method totalImageSize that computes the total size of all images in the fundiesWP web page and all web pages that are linked to it.

• Design the method textLength that computes 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.

• Tricky! Design the method images that produces one String that has in it all names of images on this web page and all web pages linked to it, given with their file types, and separated by comma and space.

So for the example above this String would be

 "wvh-lab.png, profs.jpeg, htdp.tiff, htdp.tiff"

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.

• We will eventually see how to prevent this duplication from happening, but not for quite a while!

In a comment in your file, explain why htdp.tiff appears twice in the results of the images method. Also tell us if there are any other places in your code where this duplication occurs. (Hint: look very carefully at the output from the tester library when it shows your data.)

Submit your work in a file called WebPage.java