On this page:
Instructions
Practice Problems
Problem 1: “Webpages”
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!

As always, you may only use techniques that have been covered in lectures so far in your solutions.

Due Dates:
  • Part 1: Monday, January 20th, 9:00 pm (with self-eval due Tuesday January 21st by 10pm)

  • Part 2: 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: “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.

Interpretive Reflection

Web pages have a natural structure that allows programs, often called bots (or robots) to go from page to page by following links. An old standard file, robots.txt, which indicates what pages or parts of websites that certain bots should not inspect, has gained new popularity recently. Why might that be? Since 2020, increasing numbers of sites and companies have marked their sites off limits to OpenAI’s GPTBot in particular, as they believe that, unlike the “crawling” done by, e.g., Google’s search engine bot in the past, what OpenAI is doing to build its models (to power ChatGPT) constitutes actual theft of information.

Submit your work in a file called WebPage.java