On this page:
Practice Problems
Problem 1: Circuit Components
Problem 2: “Webpages”

Assignment 2: Designing methods for complex data

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


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 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: “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.

Submit your work in a file called WebPage.java