Question: You have been given a Java source file html.java that includes a data structure that represents an HTML page. The code includes an interface HTML
You have been given a Java source file html.java that includes a data structure that represents an HTML page. The code includes an interface HTML and four classes that implement the interface and contain a constructor and getters. These four classes are:
--A class Text the stores a string
--A class Heading that stores an integer indicating the heading number as well as a string with the heading text
--A class Image which includes two integers for the width and height of the image as well as a string with the path to the file containing the image
--A class HTMLList containing a list of HTML objects
You will be responsible for modifying html.java by writing your own code that prints an HTML data structure using two approaches: (a) adding an instance method to the class, and (b) creating and overriding a visitor.
QUESTIONS.
1.In main, create a value myPage of type HTML that stores the following HTML data (string
arguments to your constructors should not include the HTML tags):
My Page
This is my page.My Photo
This is my photo.My Favorite Programming Language
Standard ML.
2.Add an instance method printHTML to HTML and its subtypes that prints the HTML object.
3. In main, test the instance method printHTML by using it to print myPage.
4. Create a visitor HTMLVisitor that visits HTML and its subtypes and modify HTML and its subtypes to accept HTMLVisitor.
5. Use HTMLVisitor to define a custom visitor called HTMLPrinter that prints an HTML object.
6. In main, test the custom visitor HTMLPrinter by using it to print myPage.
/////////////////////////////////////////////////////////////////
import java.util.*;
interface HTML {
}
class Text implements HTML {
private String text;
public Text(String t) { text = t; }
public String getText() { return text; }
}
class Heading implements HTML {
private int level;
private String text;
public Heading(int l, String t) { level = l; text = t; }
public int getLevel() { return level; }
public String getText() { return text; }
}
class Image implements HTML {
private int width;
private int height;
private String path;
public Image(int w, int h, String p) { width = w; height = h; path = p; }
public int getWidth() { return width; }
public int getHeight() { return height; }
public String getPath() { return path; }
}
class HTMLList implements HTML {
private List list;
public HTMLList(List l) { list = l; }
public List getList() { return list; }
}
public class Main {
public static void main(String[] args) {
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
