Basics -- exercises revised, part 1

part of Internet Application Workbook by Eve Andersson, Philip Greenspun, and Andrew Grumet
revisions by Michael Olan for CSIS 4135 at Stockton College

Exercises

After solving these problems you will know  

Exercise 1: Finding your place in the world

Create a new website in Visual Studio (NOT an empty one this time). Open Site.master and add some information that will identify this as your web site.

Add a folder called Basics1 to the site. This is where all files related to these exercises will be stored. You'll use similar folders for each of the subsequent parts of these exercises.

Exercise 2: Your first programs

Add a Web Form called DivByZero to the Basics1 folder - be sure to select the master page. Add a Button and a Label to this page in the MainContent area. Change the text of the button to say "Divide by zero".

In the event handler for the button, put a statement that will assign the result of dividing 6 / 0 to the Text property of the label. Try to build the project. What happens? The compiler is pretty smart, but you can outsmart it. by using a variable in the divisor. Now view the page and note the result. We want a more robust application, so add code that will handle the exception and assign an appropriate error message to the label. Also change the font color of the label to red.

Exercise 3: Processing Forms

Visit http://www.photo.net/photo/tutorial/lens and look at the focal length calculator under "Exactly how long a lens do you need?"

Make this service work on your site. Note that this will involve (1) learning a bit about HTML forms, (2) following the "view the source code" link on the results page at photo.net and pulling the mathematical formula out of the program there, (3) parking a web application to process the form on your server.

A sample of the completed form is available at lens-calculator. Your page does not have to look exactly the same, but should include the same functionality.

Add a WebForm called lens-calculator to the Basics1 folder.

Put Labels, Textboxes, a BulletedList and a Button on the page. Use a table for positioning the items. When finished, view it. Make changes to the properties and styles of the controls to get the proper labeling and a nice appearance. Choose good variable names for important controls, and don't just use the default names assigned by VS.

Now double-click the Submit button to access the code behind file and add functionality to the event handler for the submit button. Enter code in the method to do the focal length calculations as follows:

Convert distance to inches
Convert height to inches
Magnification is 1.5 / Height in inches
Lens length in inches is Distance in inches / (1 / Magnification + 1)
Lens length in mm is 25.4 * length in inches (rounded to nearest mm)

Make sure that the page title changes to show the lens size after the submit button is pressed.

3a: Add a View Source Link

Part of our work this semester is looking at other folks' source code. We do this so that we can examine alternative approaches to the same problem. You can facilitate this by adding a "view source" link to the bottom of the page that you just made. A user who clicks on this link ought to be served a file showing all of the source code behind the page in question but not including procedures shared with other pages on the site.

Hint: This is easy done in VS .NET by adding a Hyperlink control to the page and setting its NavigateUrl to the URL of the source code file. But IIS will not allow viewing of .cs source code (usually this is a good thing). So we need to make a copy of it in a text file. You can do this in VS .NET with the Save As command, and selecting Text files as the file type. Use the file name as the NavigateUrl for the link.

3b: Input validation

Add validators to the text boxes so that the user is required to provide numbers before any calculations are done.

Exercise 4: Comparative Book Shopping - Servers that query foreign servers

Add another Web Form to the Basics1 folder. This form should take the ISBN of a book, query several online bookstores to find price and stock information, and displays the results in a table.

We suggest querying barnesandnoble.com and www.powells.com. Your program should be robust to timeouts, errors at the foreign sites, and network problems. In other words, in no situation should your user ever get a "Server Error 500" page. To ensure this you'll have to use exception handling. Test your program with the following ISBNs: 0590353403, 0140260404, 0679762906, 1588750019. Try other ISBN numbers of your own choosing.

To do an ISBN search at barnesandnoble.com, you can use a URL like: http://search.barnesandnoble.com/books/product.aspx?ISBN=1588750019

Try adding more bookstores, but you may have trouble getting them to work. For example, amazon.com tends to respond with a 302 redirect if the client doesn't give them a session ID in the query. But most likely Amazon will work with the ISBN number appended to this url: http://www.amazon.com/exec/obidos/ASIN/.

A sample ISBN search page is available at books.

Extra credit (0 points): Which of the preceding books states that "The obvious mathematical breakthrough would be development of an easy way to factor large prime numbers"?

"Remember that it is a mistake to compare Harry Potter to Shakespeare... That's because Harry Potter is a fictional character whereas Shakespeare was an author. What you really ought to be doing is comparing J.K. Rowling to Shakespeare" -- Jin S. Choi.

Enhancement:

Add a validator to the textbox that will check for a valid ISBN number. There is a formula for this. Also allow ISBN numbers with or without dashes. You only need to include these features for 10 digit ISBN's.

Optional Enhancement:

Also allow searching by title.

Going live

 Add add hyperlinks to each of the exercise pages in the Default.aspx page.

Copy your web site to the CSIS server as described here.  

 

More fun exercises to come...