Part 1. Intro to Objects
1. | Write a module called Rightnow.pm that contains three methods:
A constructor called "new". A method called "set_time" to set the time. Use the localtime function. A method called "print_time" to print the time. This method will take an argument to determine whether the time is printed in military or standard time; e.g., print_time("Military"); In another Perl script, use the Rightnow module to create a Rightnow object, and call the "print_time" method to produce output as follows:
Time now: | 2:48:20 PM | Time now: | 14:48:20 |
|
Part 2. More Objects
1. | In a class, create a Student object. The attributes for the Student object will be sent as arguments to the constructor method. The Student object will have three attributes: the name of the student, the student's major, and a list of courses he is taking.
Create an instance method called "show_student" that will display a Student object.
The user of the module will create two Student objects and display each.
| 2. | Add three new attributes to the Student object: the student's address, the student's ID number, the start date, and tuition; e.g.,
Address: 140 Kennedy Drive, Luxembourg City, Luxembourg ID: 123A StartDate: 01/10/07 Tuition: 5400.55
How will you manage this? If the user has so much information to pass to the constructor, it may be a good time to create an access method called "set_student".
Create three new Student objects.
| | | 3. | Create two new access methods that take arguments. One is called "add_courses" and the other is called "drop_courses".
The user interface will allow the user to add or drop any number of courses by sending a list of parameters to the methods; e.g., $ptr->add_courses(["C++", "Java"]);
| 4. | You will use a "class" variable to keep track of the number of new students. Each time you add a student, update the counter. Before exiting the program, print the number of new students. Use the DESTROY method.
| 5. | From now on, send the data for each student to a file. It should contain a line that looks like this:
John Doe:14 Main St:3456IX:Math:Trigonometry, Calculus, French:01/01/06:4500
| 6. | Create another file that keeps track of the number of students. Each time you start your script, read the number from the file. When you add a new student, tell him "Welcome, John D.
|
Part 3. Create an Object-Oriented Module
1. | Make Checking.pm object oriented. The object will be "the balance" and the subroutines will be "methods." The constructor will contain at least two attributes: the balance, account number. The account number will be passed to the constructor as an argument. The balance will be retrieved from the register, initially set to 0.
When you create the register file, append the account number to the filename. Include the account number, balance, and date in the register file.
Use the Checking module in the ATM user script you created earlier.
| 2. | Can you make more that one instance of the Checking object and keep track of the balance for each account?
|
Part 4. Using @ISA and Inheritance
1. | Create a Pet class with a constructor and one access method.
The constructor provides attributes for a generic pet, such as:
owner name sex
The access method is called eat(). It takes one argument: the type of food a specific pet eats. For example, the dog eats Alpo. The dog will not have an eat() method and will inherit from this class. Create two classes that will inherit from the Pet class; for example, a Dog and a Cat class. They will use the Pet's constructor and add new attributes of their own. They will have a speak() method but not an eat() method.
| 2. | Now we will create a base class called Bank.pm and two modules that use it: Checking.pm and Savings.pm.
The Bank.pm parent class may or may not have a constructor but will contain the deposit(), withdraw(), and get_balance() methods from the Checking.pm module. Remove deposit() and withdraw() from Checking.pm. The program that uses Checking.pm will inherit these methods from Bank.pm via @ISA. Create another module called Savings.pm. Both Checking.pm and Savings.pm will use the Bank module and inherit its methods. Each will gave its own constructor and attributes. One attribute is the status of the account. It can be "active" or "closed." The Savings account accrues compounded daily interest 1% and must start with a minimum balance of $200. The Checking account has overdraft protection and charges $35 for each bounced check. It will not allow an overdraft of over $300. It can be opened with a starting balance of $25. The Checking.pm and Savings.pm modules will each have its own account numbers and registers. The ATM script will use both modules. The user script will have a main menu allowing the user to select either of the two accounts. After getting a new account object, the user can select from the types of transactions (sub-menu in your original Checking.pm module) for that account and continue transactions until he is ready to quit. When he exits, his account register will be updated and he will be asked if he wants to return to the main menu. If he says "yes," he will see the main menu again, and if he says "no," the program will exit. You will have to uniquely name the register for each account so you can differentiate between savings and checking accounts.
Example:
perl user.pl Welcome! Select an account type: 1) Checking 2) Savings 1 Select a function: 1) deposit 2) withdraw 3) get balance 4) exit 1 How much do you want to deposit? 5 Select a function: 1) deposit 2) withdraw 3) get balance 4) exit 3 Your balance is $30.00 Select a function: 1) deposit 2) withdraw 3) get balance 4) exit 2 perl user.pl Welcome! Select an account type: 1) Checking 2) Savings 1 Select a function: 1) deposit 2) withdraw 3) get balance 4) exit 1 How much do you want to deposit? 5 Select a function: 1) deposit 2) withdraw 3) get balance
4) exit 3 Your balance is $30.00 Select a function: 1) deposit 2) withdraw 3) get balance 4) exit 2 Return to the main menu? y Welcome! Select an account type: 1) Checking 2) Savings 2 Select a function: 1) deposit 2) withdraw 3) get balance 4) exit 3 Your balance is $100.00 Select a function: 1) deposit 2) withdraw 3) get balance 4) exit 1 How much do you want to deposit? 25 Select a function: 1) deposit 2) withdraw 3) get balance 4) exit 4 After interest balance is 127.50
|
Part 5.
Go to the pod directory in the standard Perl library. Look for perlpod.html. The file contains Larry Wall's user interface for using pod commands to document your Perl programs.
Go to your browser and in the Location box, type:
file:/<directory-to -your-library-file>/Pod/pod.html
Now you have the instructions for creating pod documentation.
Create a published interface for your Checking.pm module. Embed pod commands in your Checking.pm script explaining how the module should be used. Follow the guidelines of the modules in the library; for example, there should be a NAME, SYNOPSIS, DESCRIPTION, AUTHOR, etc. Run the pod file through the pod2html filter and display the documentation in your browser. Use the perldoc command to print your documentation on the terminal screen.
|