AlgoMaster Logo
AlgoMasterDesign Document Exportereasy

Design Document Exporter

easy

A document holds paragraphs, headings and images. Three separate reports run over the same document: one counts words, one exports HTML, one exports plain text. Each report treats each element type differently.

The document nodes and DocumentExporter are already provided. Implement only these visitor components:

  • DocVisitor declares one visit method for each node type.
  • WordCountVisitor counts words in paragraphs and headings while ignoring images.
  • HtmlExportVisitor produces the HTML line for each node type.
  • PlainTextVisitor produces the plain text line for each node type.

The provided DocumentExporter exposes this behavior:

  • DocumentExporter() creates an empty document.
  • boolean addParagraph(String text) appends a paragraph and returns true. It returns false once 20 elements are stored.
  • boolean addHeading(String text, int level) appends a heading and returns true. A level below 1 or above 6 changes nothing and returns false.
  • boolean addImage(String url, String altText) appends an image and returns true.
  • int elementCount() returns how many elements are stored.
  • int wordCount() returns the total number of words in the document.
  • String[] exportHtml() returns one HTML line per element, in insertion order.
  • String[] exportPlainText() returns one plain text line per element, in insertion order.

A word is a run of characters with no spaces in it. Paragraph text and heading text contribute words. An image contributes none.

The HTML lines are <p>text</p> for a paragraph, <h3>text</h3> for a heading at level 3, and <img src="url" alt="altText" /> for an image.

The plain text lines are the paragraph text unchanged, the heading text in capitals, and the image alt text wrapped in square brackets.

An empty document counts zero words and exports no lines.

DocumentExporter is the only type the tests call. Complete the visitor interface and visitor classes so each pre-implemented element can hand itself to the report rather than being examined by it.

Example 1:

Input:

Output:

Explanation: The document holds a heading, a paragraph and an image. The word count reads the heading and the paragraph and skips the image, so 2 words plus 8 words gives 10. The HTML export walks the same three elements and produces a different line shape for each one.

Example 2:

Input:

Output:

Explanation: The plain text export reads the same three element types and answers differently from the HTML export. The heading comes back in capitals, the paragraph comes back unchanged, and the image comes back as its alt text in square brackets.

Constraints

  • 0 <= text.length <= 100
  • Text, urls and alt text contain letters, digits, spaces and punctuation only.
  • At most 20 elements are stored in one document.
  • At most 100 calls in total are made across all methods.

How the design is graded

needs 7/10 to pass
  • Double dispatch

    Full marks when every element's `accept` calls the one method on the report contract that matches its own type, and each report implements all three of those methods. Lose points heavily when a report inspects an element with a type check or a stored tag before deciding what to produce, or when one method handles every element type alike.

  • Reports are separate objects

    Full marks when the word count, the HTML export and the plain text export are three classes behind one contract, and none of the element classes changed to accommodate the third. Lose points when one report is derived from another's output, or when an element carries a single rendering that every report reuses.

  • Structure and naming

    Full marks when a heading renders at the level it was given, an image adds no words but still produces a line in both exports, a heading level outside `1` to `6` is refused, and an empty document exports nothing. Lose points for printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new DocumentExporter()null
addHeading("My Document", 1)true
addParagraph("This is the first paragraph with some text.")true
addImage("photo.jpg", "A photo")true
elementCount()3
wordCount()10
exportHtml()["<h1>My Document</h1>","<p>This is the first paragraph with some text.</p>","<img src=\"photo.jpg\" alt=\"A photo\" />"]

The document holds a heading, a paragraph and an image. The word count reads the heading and the paragraph and skips the image, so 2 words plus 8 words gives 10. The HTML export walks the same three elements and produces a different line shape for each one.

Run checks these cases. Submit also runs a larger hidden set.