This assignment is designed to give you experience with object reuse by employin
ID: 3645570 • Letter: T
Question
This assignment is designed to give you experience with object reuse by employing inheritance to extend and modify the behaviour of Java classes. You will use the SimpleURLReader class from the cs1 package. Create your lab02 JCreator project from the CS101 console template, as usual, then download this cs1 package (right click and save it) to a convenient location on your local machine (e.g. the folder you are using for this lab assignment.) You now need to add this to your project's classpath so Java can find and use it. To do this, in JCreator select Project|Project_Properties from the main menu. The Project_Properties dialog will open. Select the "Required Libraries" tab. Click the "New" button and, in the Set_Library dialog that appears, enter the Name: as "cs1" and select Add|Add_Archive. A file dialog will open; locate the cs1.jar file you just downloaded & click OK to close the dialog. Finally, click the checkbox next to the "cs1" line in the Project_Properties dialog and click OK there too. Note that, to complete the assignment, you do not need to know how the SimpleURLReader class works, just treat it like you have been doing with other Java classes. One of the key points of this lab is to demonstrate that you can add to and modify the behaviour of an existing class, even without having its source code. Now that is neat!(a) Write a test program that will read the contents of this url and print it and the number of lines it contains, on the console. To read the contents of a url you can use the SimpleURLReader class (see above!) Its constructor takes the desired url as a String, for example "http://www.cs.bilkent.edu.tr/~david/housman.txt". The class has only two methods: getPageContents() that returns the contents of the url (the webpage) as a String, and getLineCount() that returns an int corresponding to the number of lines read from the url.
(b) Design, implement and test a new class, MySimpleURLReader, that extends the SimpleURLReader class, adding two methods to it: getURL() that returns the url String used to create the SimpleURLReader object, and getName() that returns the filename part of the url, that is, the part of the url following the last '/' character ("housman.txt" in the previous example). Once this is working, fix the bug in SimpleURLReader's getPageContents() method whereby the String "null" is added to the beginning of the String it returns. Do this by overriding the corresponding method in your new sub-class.
(c) A customer wants to be able to print the contents of this other url (an html version of the original plain text) and have it appear as in the part (a) without any of the html code in it! Clearly, it is necessary to read the contents of the url and then filter out the html code so that only the visible text is left. Rather than write an entirely new class from scratch, you realise that the MySimpleURLReader class does most of what you want and so decide to use it. Design, implement & test a new class, HTMLFilteredReader, that extends MySimpleURLReader. Its getPageContents() method should return only the text, without the html. A new method, getUnfilteredPageContents() can be called to return the original page complete with html codes. Assume that anything between "<" and ">" is html code and should be omitted from the filtered output. Solve the problem using charAt(i) first, then check the Java documentation to see how you might adopt the same approach using an instance of the StringTokenizer class to perform the filtering operation.
(d) The customer is impressed and immediately asks you to add a method that computes the overhead due to the html code (the percentage increase in size between the html and no-html contents.) They also want a list of the url's that the page links to! A little research shows that html links have the form href="link_url". Use String class methods to extract all of the link_url's and put them into an ArrayList. Return this as the result of a getLinks() method. Add these methods by sub-classing HTMLFilteredReader in a class called SuperHTMLFilteredReader. Modify your test program to exercise these new facilities. Experiment with Java's extended type checking mechanism by calling the methods using variables of both HTMLFilteredReader and SuperHTMLFilteredReader types. Note: the previous url's do not have any html links in them, so try using this url for testing this class.
(e) Design and implement a simple menu-driven program that will maintain a collection of MySimpleURLReader objects. The main menu should have three options: (1) Enter the url of poem to add to collection, (2) List all poems in the collection, and (3) Quit. Option 2 should display only the index number & (file) name for each of the poems. The user should then be able to enter the index number of a poem to view it (and then return to the same list.) If the user enters the last index number + 1 they should be returned to the main menu, anything else should be ignored. In option 1, if the user enters the url of a text file you should create a MySimpleURLReader object and add it to the collection, whereas if they enter the url of an html file you should create an HTMLFilteredReader object and add it to the collection. You should always call the getPageContents() method of the corresponding object to view it from option 2. If you have done everything properly, you should always see the non-html version of the poem... that's neat, that's polymorphism!
--------------------------------------------------------------------------------
Explanation / Answer
import java.io.*; // class implementation begins here public class HTMLFilteredReaderDriver { // instance constant URL to store the URL and easy-access private static final String URL = "http://www.cs.bilkent.edu.tr/~david/housman.htm"; /** * Constructor for objects of class SimpleURLReaderDriver -- not needed * since class is just a driver for another class; no instances will be * created of this class */ public HTMLFilteredReaderDriver() {} /** * main method to test the SimpleURLReader class */ public static void main( String args[]) { // create an onject of HTMLFilteredReaderDriver class using the constant // string that contains the url address HTMLFilteredReader myReader = new HTMLFilteredReader( URL); // print out the URL of which contents will be displayed System.out.println( "Address: " + URL); System.out.println( "Filtered Result"); System.out.println( "--------------------------------------------------------"); // print out the contents using the pubic method of the child HTMLFilteredReader class System.out.print( myReader.getPageContents()); System.out.println( " =========================END============================"); System.out.println( " Unfiltered Result"); System.out.println( "--------------------------------------------------------"); // print out the contents of the html page using the method of the parent SimpleURLReader class System.out.print( myReader.getUnfilteredPageContents()); } // end main method } // end HTMLFilteredReaderDriver