Setup and Inputs: Class Skeleton and URL
Before coding the changes, it's common for code improvisers to first implement what we call a class skeleton, which typically consists of the following elements:
Class declaration
Main() method that gets the user's initial input
Constructor
Listing 12 shows the class skeleton for our simple vampire bot.
Listing 12Class Skeleton
using System; class vampirebot { string base_url, folder; vampirebot(string url, string dir) { int slash_loc; slash_loc = url.LastIndexOf("/"); base_url = url.Substring(0, slash_loc+1); folder = dir; } public static void Main() { string url, dir; vampirebot vbot; Console.Write("Enter starting URL: "); url=Console.ReadLine(); Console.Write("Destination folder? "); dir=Console.ReadLine(); vbot = new vampirebot(url,dir); } }
Detailed Explanation
As mentioned, the class skeleton consists of a class declaration (see Listing 13).
Listing 13Class Skeleton: Namespace and Class Declaration
using System; class vampirebot { string base_url, folder; vampirebot(string url, string dir) { int slash_loc; slash_loc = url.LastIndexOf("/"); base_url = url.Substring(0, slash_loc+1); folder = dir; } public static void Main() { string url, dir; vampirebot vbot; Console.Write("Enter starting URL: "); url=Console.ReadLine(); Console.Write("Destination folder? "); dir=Console.ReadLine(); vbot = new vampirebot(url,dir); } }
In addition to the class declaration (class vampirebot), it's common to specify that your code uses the System namespace (using System). The System namespace has all kinds of built-in goodies related to input/output and basic types.
As mentioned in the earlier section on information changes, the user needs to supply our vampire bot with at least the URL for the web page from which the bot should download the images. Additionally, the bot needs to store the images in a folder. We code this input in Main() (see Listing 14).
Listing 14Class Skeleton: Main and Input Code
using System; class vampirebot { string base_url, folder; vampirebot(string url, string dir) { int slash_loc; slash_loc = url.LastIndexOf("/"); base_url = url.Substring(0, slash_loc+1); folder = dir; } public static void Main() { string url, dir; vampirebot vbot; Console.Write("Enter starting URL: "); url=Console.ReadLine(); Console.Write("Destination folder? "); dir=Console.ReadLine(); vbot = new vampirebot(url,dir); } }
In Main(), we see an example of a simple motif, namely the Console.Write() followed by the Console.ReadLine(); the former prints out a user prompt and the latter reads whatever information the user types. The code stores the information that the user enters for the starting URL and destination folder into two string variables labeled url and dir, respectively.
With the input stored away in local variables url and dir, we build a constructor and create an instance of the vampire bot (vbot), passing url and dir as parameters to the constructor as shown in Listing 15.
Listing 15Class Skeleton: Constructor
using System; class vampirebot { string base_url, folder; vampirebot(string url, string dir) { int slash_loc; slash_loc = url.LastIndexOf("/"); base_url = url.Substring(0, slash_loc+1); folder = dir; } public static void Main() { string url, dir; vampirebot vbot; Console.Write("Enter starting URL: "); url=Console.ReadLine(); Console.Write("Destination folder? "); dir=Console.ReadLine(); vbot = new vampirebot(url,dir); } }
To understand how the constructor works, first suppose that the user types http://www.professorf.com/planets.html as the starting URL and d:\botpics\ as the destination folder. The constructor extracts the base address (http://www.professorf.com/) from the starting URL and stores it in a string variable named base_url. The destination folder gets stored in the string variable folder. The vampire bot's methods will then have access to these variables.
We have coded the setup portion of the information changes, namely getting the input:
string:URL-->string:RawHTML-->ArrayList:ImageList-->Disk:Files
Next we'll code the first of the information changes.