On this page:
Instructions
Practice Problems
Problem 1: Circuit Components
Problem 2: XML
8.5

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.

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

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 — 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.