Next: , Up: (dir)

Scripting Languages G22.3033-002 Summer 2008


Previous: Top, Up: Top

Scripting Languages G22.3033-002 Summer 2008 hw05

Assigned Th 6/19/2008, due Fr 6/27 at 9pm. 50 points.

http://www.cs.nyu.edu/courses/summer08/G22.3033-002/


Next: , Up: hw05

Homework instructions

Homeworks are due on Fridays at 9pm. This deadline will be strictly enforced.

Email your answers to Robert Soulé robert.soule@gmail.com. Please put your solutions to VBA programming problems in a powerpoint presentation. For all other questions (including programming problems in other languages), just send a simple text file, such as what you get when using Emacs, Vi, Notepad, or the "save as text only" feature in Word.

Please make sure that your code works with the compilers and tools installed at CIWW. Specifically, please test:

JavaScript
Your JavaScript code must run with both Mozilla's Firefox browser and with Microsoft's Internet Explorer browser.
Php
Your PHP code must run with PHP 5.1.6 on the CIMS web servers, see http://www.cims.nyu.edu/systems/userservices/webhosting/.
Perl
Your Perl code must run with Perl 5.8.8 for Linux/x86, for example on doowop1 (see http://www.cims.nyu.edu/systems/resources/computeservers/).
Vba
Your VBA code must run with Microsoft Office 2003 for Windows on the machines in the labs CIWW 502 or CIWW 624.


Next: , Previous: Instructions, Up: hw05

Reading assignments

Read for lecture on 6/26:
W3Schools JavaScript Tutorial. Read the part “JavaScript Basics”, which should start with “JS Introduction” and end with “JS Guidelines”. Available online at http://www.w3schools.com/js/.


Next: , Previous: hw05-readings, Up: hw05

Concept questions


Next: , Up: hw05-concepts

hw05-1 Properties

(4+4+4 = 12 points) Consider the following PHP code.

     <?php
     class Vector {
       var $x = 0;
       var $y = 0;
       function __construct($x, $y) {
         $this->x = $x;
         $this->y = $y;
       }
       function __get($propName) {
         if ($propName == 'length')
           return sqrt($this->x * $this->x + $this->y * $this->y);
       }
       function __set($propName, $value) {
         if ($propName == 'length') {
           $oldLen = sqrt($this->x * $this->x + $this->y * $this->y);
           $this->x *= $value / $oldLen;
           $this->y *= $value / $oldLen;
         }
       }
     }
     $v = new Vector(2,3);
     printf("|%.2f, %.2f| = %.2f<br/>\n", $v->x, $v->y, $v->length);
     $v->length = 5;
     printf("|%.2f, %.2f| = %.2f<br/>\n", $v->x, $v->y, $v->length);
     ?>
  1. Run the script. What does it print?
  2. Are properties in PHP easier or harder to use than in VBA? Why?
  3. Are properties in PHP more or less expressive than in VBA? Why?


Previous: hw05-1, Up: hw05-concepts

hw05-2 Call-backs

(4+4+4 = 12 points) Consider the following PHP code.

     <?php
     class Vector {
       var $x = 0;
       var $y = 0;
       function __construct($x, $y) {
         $this->x = $x;
         $this->y = $y;
       }
       function length() {
         return sqrt($this->x * $this->x + $this->y * $this->y);
       }
     }
     
     $a = array(new Vector(0,4), new Vector(-3,4), new Vector(2,3));
     function cmp_vector($a, $b) {
       $aa = $a->length(); $bb = $b->length();
       return $aa < $bb ? -1 : $aa == $bb ? 0 : 1;
     }
     usort($a, "cmp_vector");
     foreach ($a as $i => $v)
       printf("%2d => (%d, %d)<br/>\n", $i, $v->x, $v->y);
     ?>
  1. Run the script. What does it print?
  2. Explain where and how the code uses call-backs.
  3. What would you need to change to sort by x-coordinates instead of by length?


Previous: hw05-concepts, Up: hw05

Programming exercises


Next: , Up: hw05-programming

hw05-3 Installing script on server (PHP)

(5+5+5 = 15 points)

  1. Follow the instructions from the lecture slides to create an HTML page that can be viewed from any web browser using the following URL:

    http://www.cs.nyu.edu/~your_cims_id/hw05-3a.html

    For example, if your_cims_id is hirzel, then the URL would be:

    http://www.cs.nyu.edu/~hirzel/hw05-3a.html

  2. Use a .htaccess file to create a password protected HTML page that can be viewed from any web browser using the following URL:

    http://www.cs.nyu.edu/~your_cims_id/php/hw05-3b.html

    For example, with your_cims_id as hirzel, you get the following URL, which requires you to enter the user name student and the password P_8sh_P:

    http://www.cs.nyu.edu/~hirzel/php/hw05-3b.html

    Please create a user name grader. Please indicate the URL and the password for grader in your submission, so that Robert Soulé can access your web page.

  3. In the same directory that you secured using a .htaccess file, create a PHP script hw05-3c.php with the following code:
              <html><body>
              <?php
                if(!empty($_GET['who'])) { echo "Hi, {$_GET['who']}.";}
              ?>
              <form action="<?php echo $_SERVER[PHP_SELF]; ?>" method=get>
                Who shall be greeted: <input type="text" name="who" />
              </form>
              </body></html>
         

    (See also http://www.cs.nyu.edu/~hirzel/php/hw05-3c.phps). Test it and make sure it works. For example, for your_cims_id hirzel, user name student, and password P_8sh_P, this URL runs the script for you:

    http://www.cs.nyu.edu/~hirzel/php/hw05-3c.php


Previous: hw05-3, Up: hw05-programming

hw05-4 Cooking conversions (PHP)

(11 points) Write a PHP script that converts cooking quantities, units, and ingredients to metric units. For data input, use a form that provides three text fields, one each for Quantity, Unit, and Ingredient. When the user enters values for all three and clicks the submit button, your script should print the converted result at the top of the web page. Your script should be a self-processing page, in other words, use the same page for input and output. For example, if the user enters the values 3, cup, and flour in the three input fields and clicks the Submit button, your script should return a page that looks like this:

Your PHP script should offer the same kinds of conversions as the Perl example from the lecture slides. Please copy-and-paste the source code of your script into your homework submission. In addition, please put the script online at the following URL (replacing your_cims_id by your CIMS id):

http://www.cs.nyu.edu/~your_cims_id/php/hw05-4.php