[an error occurred while processing this directive]

Perl

Menu

In-Class Assignment #6

In this assignment, you will create a small class.

Write a Perl Module TVGuide. It should be stored in a file named TVGuide.pm. This class will model a Television's Guide for the current time period. It contains two data elements:

  • channel - The current channel to which the TV is tuned
  • schedule - a reference to a hash in which a key is a channel numbers and the value is which program is airing on that channel.
    For example: my $s = { 2 => 'Sesame Street', 3 => 'This Old House', 10 => 'Square One' };

Create a constructor for your class named new. It should take an integer and a reference to a hash as its arguments. It will create a new TVGuide object, set the channel and schedule data elements to be the arguments passed in, and return the new object.

Create the following object methods for this class:

  • get_channel - returns the TV's current channel
  • get_show - takes a channel number and returns the program airing on that channel
  • change_channel - takes a channel number as an argument, and sets the TV's current channel to that number
  • now_showing - returns the program airing on the TV's current channel
  • add_show - takes a channel number and program name. Sets the TVGuide to air this program on this channel

Notes

  • If there is already a program on the channel passed to add_show, replace that program with the new program passed in
  • If there is no show airing on the channel passed to get_show (or the current channel, for now_showing), return the undefined value.

Bonus

For a bonus ½ point, overload the + and - operators, to change the channel by that many numbers. That is, return a new object that has the same guide data as the original, but has a current channel that number greater than (for +) or less than (for -) the original's channel.

For example:
my $guide = TVGuide->new(10, { 10 => "Program 1", 12 => "Program 2" });
my $guide2 = $guide + 2;

will result in $guide2 having the same initial schedule as $guide (Program 1 on channel 10, Program 2 on channel 12), but will have the initially tuned channel equal to 12 rather than 10.

Testing

To test your module, I have created ~lallip/perl/ica6.pl for you to use. This script takes advantage of the Test::More module that we will discuss more next week. It performs several different tests on your module's data and methods. For each test, it will either print out "ok" or "not ok". If not ok, it will print out what values it was expecting to get, and what values it got instead. It will also print out a brief description of each test.

Do not modify ica6.pl at all. Run it from within the same directory as your module. If you get any "not ok" responses, view the test file and find the test description to determine what the test was evaluating when it failed. Use that information to fix your module.

You should get a total of 37 "ok" responses. If you do not overload the operators, the last 12 of these will be marked with a "skip" token. For full credit, just the first 25 "ok"s followed by 12 skips are required. For the bonus credit, all 37 "oks"s without any skips are required.

Submission Instructions

To submit, log on to solaris.remote.cs.rpi.edu and execute the program ~lallip/public/submit.pl and follow the prompts. Do not submit the ica6.pl file (I wrote it, I know what it looks like). Submit only your TVGuide.pm file.

You may submit infinite times, only the last submission will be graded. Your final submission is due at the end of class today, 4:00pm, Thursday, March 18, 2010

Perl Quotes
Perl Quotes