// Allison Obourn // CSE 143, Autumn 2015 // A TallyDownloader object stores a URL and can download a web page // from that URL to save it to a given file on the local hard disk // and calculate the counts of each character in the file. // use this as an example of how inheritance works in practice. import java.io.*; import java.net.*; import java.util.*; public class TallyDownloader extends Downloader { // Constructs a new TallyDownloader to download from the given web site URL. // Precondition: urlString != null public TallyDownloader(String url) throws MalformedURLException { super(url); } // Saves the web page from this Downloader's URL to the given file name // and prints out the counts of the characters in the file. // Throws an IOException if the web page does not exist or if anything // goes wrong when trying to save the file. public void download(String targetFileName) throws IOException { super.download(targetFileName); Map counts = new TreeMap(); FileInputStream in = new FileInputStream(targetFileName); int n = in.read(); while (n != -1) { char ch = (char) n; if (counts.containsKey(ch)) { counts.put(ch, counts.get(ch) + 1); } else { counts.put(ch, 1); } System.out.print(ch); n = in.read(); } in.close(); System.out.println(counts); // print map of char -> int } }