Spring Semester 2005


Due date
Wednesday March 23, 11:59pm
Late policy
Try to turn everything on time, no solutions will be accepted late.

Work policy
Working in groups is encouraged but please turn in your own version of the assignment.

Also, please make sure you

the Computer Science Department's Statement on Academic Integrity before turning in your assignment.

Task

Choose one of the tasks below. (Complete freedom of choice).

Provide yet another implementation of the Midterm Exam.

Provide a stage of the Semester Project (see example below).

Given the following seven stages of the TWP registration system:

  1. stage one
  2. stage two
  3. stage three
  4. stage four
  5. stage five
  6. stage six
  7. stage seven

Define a database and create the tables, enter data in them.

Design (but don't implement) an administrative part to the system.

You can choose the account management system above even if you don't have it as a project.

Finally take this program, determine what it should do, and make it work.

#!/usr/bin/perl

use CGI; 

use ___;
use MD5;

$DB = "DBI:_____:a348"; 

$________ = "a348"; 
$password = "______"; 

$DB_TABLE = "___________"; 
$SECRET = "something secret"; 

_______ = 30 * 60 * 60 * 24; # one month

$MAX_TRIES = 10; 
$ID_LENGTH = _;

__ = new CGI; 

$DBH = DBI->_______($DB, $username, $password, { PrintError => 0 }) 
       || die "Couldn't open database: ", $DBI::errstr; 

my ($session_id) = &get_session_id(); 

print $q->______, $q->start_html; 

my $state          = &get_state(___________); 
if (! $state->{message}) { 
  $state = &__________($state); 
} else { 
  $number = $q->param('number'); 
  $_____ = &calculate(______, _______); 
} 


&save_state($state, $session_id); 

&report($state); 

&show_form(); # get ready for more input 

print $q->end_html;

sub ______________ {
  &expire_old_sessions(); 
  my ($id) = $q->path_info =~ m:^/([a-h0-9]{$ID_LENGTH}):o;
  return $id if $id and &check_id($id); 
  my $session_id = &generate_id; 
  die "Couldn't make a new session_id" unless $session_id; 
  print $q->________($q->script_name() . "/$session_id"); 
  exit(0);   
}

sub ___________________ {
  $DBH->do(<<END);
DELETE FROM $DB_TABLE
  WHERE (unix_timestamp() - unix_timestamp(modified)) > $EXPIRE
END
}

sub ___________ {
  my $tries = 0; 
  my $id = &hash($SECRET . rand()); 
  while ($tries++ < __________) {
    last if 
      $DBH->do("INSERT INTO $DB_TABLE (session_id) VALUES ('$id')"); 
    $id = &hash($SECRET . rand()); 
  }
  return undef if $tries >= $MAX_TRIES;
  return $id; 
}
sub hash {
  my $value = shift; 
  return substr(MD5->hexhash($value), 0, $ID_LENGTH);   
}

sub ________ {
  my $id = shift; 
  return $id 
    if $DBH->do("SELECT 1 FROM $DB_TABLE WHERE session_id = '$id'") > 0; 
  return $id 
    if $DBH->do("INSERT INTO $DB_TABLE (session_id) VALUES ('$id')"); 
  return ''; 
}

sub show_form {
  print $q->start_form(),  qq{
        ______ _____ ____ ______: _________________________________ <p> }, 
        "When done please press ", 
        $q->submit(-value=>'Proceed'),
        $q->end_form(); 
}

sub _________ {
  my $id = shift; 
  my $query = "SELECT * FROM _________ WHERE session_id = '$id'"; 
  my $sth = $DBH->prepare($query) || die "Prepare: ", $DBH->errstr; 
  $sth->execute || die "Execute: ", $sth->errstr;
  my $state = $sth->fetchrow_hashref;
  $sth->finish; 

  return $state; 

}

sub _________ { 

  my $state = shift; #  first param
  my $number = shift; # second param

  my $balance = $state->{_______}; 
  my $_______ = $state->{mess___}; 

  if ($number > 0 && $number < 11) {
      $balance += $number; 
      if ($balance >= 100) {
          $message = "Congratulations, you ____\n"; 
	  $balance = 0; 
	  $message .= "The current balance is $balance.\n";
      } else {
          $computer = int(rand(10)) + 1; 
	  ________________________________________________ 
	  $balance += $computer; 
	  if ($balance >= 100) {
              $message .= "The computer just won the game.\n"; 
	      _____________ 
	      $message .= "The current balance is $balance.\n"; 
	  } else {
	      $message .= "The current balance is $balance.\n"; 
	  }
      }
  } else {
      $message = "Sorry, you lost.\n"; 
      ____________; 
      $message .= "The current balance is $balance.\n";
  }

  $state->{_______} = $balance; 
  $state->{message} = ________; 

  return $state; 
}

sub __________ {
  my ($state, $id) = @_; 
  my $sth = $DBH->prepare(<<END) || die "Prepare: ", $DBH->errstr; 
UPDATE $DB_TABLE
  SET _______ = ?, _______ = ?   
  WHERE session_id = '$id'
END

  $sth->execute(@{$state}{qw(balance message)}) 
  || die "Execute: ", $DBH->errstr; 

  $sth->finish; 
}

sub ______ {
  my ($state) = @_; 
  print  qq{
          _______: $state->{balance} <p> 
          Message: $_____->{message} <p> 
  }; # note that computer choice printed is just a number 
}

sub initialize {
  my $state = shift; 
  $state = {} unless $state; 
  $state->{_______} = 0;  
  $________message_ = "_______. The current balance is: ". $state->{balance}; 

  return $state; 

}
Grading
Feedback will be provided within a week, grades will be posted on-line.