Assignment 2: Designing methods for complex data
Goals: Learn to design methods for complex class hierarchies. Practice designing the representation of complex data.
Instructions
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,
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.
Remember that you should avoid accessing fields of fields and using any type-checkers. Design your methods systematically using the Design Recipe as we have been doing in class!
Due Date: Thursday, January 26th, 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_1\) 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: XML
XML, the "Extensible Markup Language", is a ubiquitous format for exchanging data on the internet. It is also used extensively in modern office-productively software produced by Microsoft, Apple, and the open-source community. XML is fairly simple: it consists of content that is “marked up” with tags.
Here are some simple examples of XML:
I am XML!
The above is just plain content with no tags. We can tag certain parts of the content with a pair of open and close tags to delimit the tagged region:
I am <yell>XML</yell>!
Here the content XML is tagged with the yell tag. The tags can nest, as in:
I am <yell><italic>X</italic>ML</yell>!
Here the content XML is again tagged with yell, but the X is also tagged with the italic tag.
Tags can also carry attributes that associate data with a tag. For example, we may want to yell at a certain volume:
I am <yell volume="30db"><italic>X</italic>ML</yell>!
Here the yell tag carries a volume attribute with the value 30db. Moreover, you can add an arbitrary number of attributes to a tag, so we can specifiy both the volume and the duration of a yell:
I am <yell volume="30db" duration="5sec"><italic>X</italic>ML</yell>!
If we step back and think about how to represent XML, we arrive at the following sketch:
XML is either just plain content (no tags), or it is tagged XML, i.e. it is some tag, some list of attributes and values, and some XML.
The following DrRacket data definition describes XML:
;; An XML is one of: ;; - (make-plaintext String) ;; - (make-untagged [Listof XML]) ;; - (make-tagged Tag [Listof XML]) ;; A Tag is a (make-tag String [Listof Att]) ;; An Att is a (make-att String String) (define-struct plaintext (txt)) (define-struct untagged (content)) (define-struct tagged (tag content)) (define-struct tag (name atts)) (define-struct att (name value))
We are giving you the names of the classes or interfaces you will need
—
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 on a piece of paper. You do not have to submit this, but this is the first assignment with a complex class structure, and understanding it is essential before proceeding.
Define Java classes that represent XML as defined above. Translate all of the above examples using your representation. Name your examples class ExamplesXML. Name your examples xml1, xml2, etc. Note that the Untagged constructor should only be called when absolutely necessary.
Design a contentLength method which computes the length (number of characters) of the content in an XML document. The tags and attributes should not contribute to the length.
Design a hasTag method that determines if a piece of XML contains a Tag with the given name.
Design a hasAttribute method that determines if a piece of XML contains an attribute with the given name.
Design a renderAsString method that converts XML to a String without tags and attributes. HINT: We asked you to implement renderAsString but you might want to also implement renderAsXmlString which includes the tag names and attribute names. That way you can check if your data matches our examples! This is more coding work that will save you debugging work.