Homework 7 (due 11/12)
CSC 243

We talked about aspects of OO-programming in Python, covering classes, objects and methods, instance and class variables (Section 8.1), overloading operators (Section 8.4), and inheritance (Section 8.5). We did not go into full depth on all of the following subjects which I recommend to your attention:

We started on HTML, web-pages and parsers using OO programming (Sections 11.1 and 11.2). We'll continue that topic next week.

Submission: The homework is due by midnight (I will not accept late homeworks). You can submit your homework through d2l into the drop-box for this homework.

Please prepare your homework as a single file containing all answers (e.g. doc, docx, or pdf, not a zip file). When submitting programs, include the programs, together with screenshots of test-runs of your program (make sure screenshots are sized so the text is legible; resize and/or crop the images). For an example, see hwexample.docx. How to take screenshots? Check out screenshots for MAC, Windows, Linux. Unless I ask for it specifically, you do not have to submit separate files with program code or executables. If multiple files are necessary, please do not zip them up, just submit them separately.

1. (Reading Assignment) Read Sections 8.3 and 8.5, as well as Sections 11.1 and 11.2. We will skip Chapter 9 on GUIs entirely, feel free to check that out, it's good (if intricate) material. We will continue with more material from chapter 11 (including the case study).

2. (My Own Bank Account, 15pt) For this problem you need the second edition of the book, both problems are about bank accounts. Do Exercises 8.20 (page 281) and 8.40 (page 288) in the book. Read through both problem first, before starting on solving them, they are related.

3. (Overloading, 15pt) Write a class Interval which implements an interval of the real line: e.g. Interval(-3.5,10.2) represents all the (real) numbers strictly between -3.5 and 10.2, that is, upper and lower bound are excluded (mathematicians call this an open interval). Implement the following functionality:

Below is a test-run. Note: don't worry about implementing getter and setter methods for Interval, your methods are allowed to work directly with the underlying data.

4. (HTML Parser, 10pt) Write a parser that reports unclosed tags on your web-page. To do so, the parser needs to keep track of which tags are currently open (and in what order). When a tag is closed, check that the closed tag corresponds to the currently open tag. If it doesn't, then the currently open tag was not properly closed. Report the currently open tag, and discard it.Keep doing this, until the closing tag matches an open tag. At that point, proceed. For example, look at

<html><head><title>My Page</title>
<body>Hello World<br>How are you
Rest of Text. Lorem Ipsum

Everything is fine, up to </body>. At that point, we have the following open tags: <html>, <body>, <br>, <hr>. Since body doesn't close <hr>, <hr> is an unclosed tag. We report and discard it. At that point </body> doesn't match <br>, so we we report and dicard <br>. At that point the open tags are html, body, and </body> closes body. Now only <html> is open, and </html> dpes close <html>. This is the first test-run below. Note that <br> gets reported after <hr>, because we notice that <br> hasn't been closed first.

Hint: this is a bit trickier; I would start with our code for PrettyParser. Instead of keeping track of indentation level, keep track of open tags. In a first step, make the parser stop once you have found (and reported) _one_ unclosed tag. Then extend the handle_endtag method to report/discard all unclosed tags at that point before moving on (that will require a while loop). Partial credit for a working solution to the first-step approximation.


Marcus Schaefer
Last updated: November 6th, 2019.