Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment is designed to give you experience with object reuse by employin

ID: 3645866 • 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.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote