Homework 1
This homework is in two parts. Part A can be completed after lecture on 2/4. Part B will require knowledge learned in lecture on 2/11. You will turn in one file that contains both parts.
Part A
Prompt the user for the path to a file to open for reading. Open that file. The file contains three lines. The first line contains a file name in all lowercase. The second line contains two numbers, separated by a comma. The third line contains a directory name.
Create the directory specified by the third line (you may assume it does not already exist, and that you have permissions to create it). Create a file named by the first line after converting all but the first character to uppercase, inside the new directory. Write to this file the sum, product, difference, quotient, and exponentiation of the two numbers.
For example, if the file contained:
bar.txt 5,2 Foo
Then you would create the directory Foo, and open a new file
for writing named Foo/bAR.TXT. You would print the following
lines to that file:
Sum: 7 Product: 10 Difference: 3 Quotient: 2.5 Exponentiation: 25
Note that if the second number is 0, you should instead print a message to the file that you can't divide by 0, rather than attempting to print the quotient (which would cause your program to crash).
Part B
Obtain a list of names from the command line. You do not know, when writing your script, how many names will be on the command line. For each name listed, prompt the user for the student's Id Number. Read in and store the Id number for each student. For each student, create their RCS Id - first five letters of the last name, followed by first initial, all lowercase. (If the last name has less than five letters, use the whole last name, followed by first initial).
Prompt the user for a sorting method, either L, F, I,
or R, for Last Name, First Name, Id Number, or RCS Id. Print out each student's
full name, Id number, and RCS id, sorted by the method the user chose. (names and RCS Ids are
sorted alphabetically a-z, Id Numbers are sorted numerically 0-9).
Assumptions
You may make the following assumptions when writing Part B:
- Every student will have a first and last name, separated by a single space
- No two students will have the same first name, last name, RCS Id, nor Id Number
Sample I/O
if the user runs your script like so:
hw1.pl "Paul Lalli" "John Smith" "Chong Wu" "Abbey McAlistair" "Bobby Smitherson"
Then your script will print the following prompts (user input is in bold):
Please enter the Id Number of Paul Lalli: 1234 Please enter the Id Number of John Smith: 683410 Please enter the Id Number of Chong Wu: 85742 Please enter the Id Number of Abbey McAlistair: 27841 Please enter the Id Number of Bobby Smitherson: 542 Please choose a sorting method: (L)ast Name, (F)irst Name, (I)d Number, (R)CS Id:At this point, the user will enter one of L, F, I, or R. And your script will print out one of the following, based on which letter was entered:
All Sorted Students: Paul Lalli, lallip, 1234 Abbey McAlistair, mcalia, 27841 John Smith, smithj, 683410 Bobby Smitherson, smithb, 542 Chong Wu, wuc, 85742 All Sorted Students: Abbey McAlistair, mcalia, 27841 Bobby Smitherson, smithb, 542 Chong Wu, wuc, 85742 John Smith, smithj, 683410 Paul Lalli, lallip, 1234 All Sorted Students: Bobby Smitherson, smithb, 542 Paul Lalli, lallip, 1234 Abbey McAlistair, mcalia, 27841 Chong Wu, wuc, 85742 John Smith, smithj, 683410 All Sorted Students: Paul Lalli, lallip, 1234 Abbey McAlistair, mcalia, 27841 Bobby Smitherson, smithb, 542 John Smith, smithj, 683410 Chong Wu, wuc, 85742
Combining the Parts
Submit your solution as one complete file, not two individual files. If the user runs your program with any command line arguments provided, execute Part B. If the user runs your program without any command line arguments at all, execute Part A.
Grading Criteria
| A: Obtain file to read | 5 |
|---|---|
| A: Get three lines | 10 |
| A: Create the directory | 5 |
| A: Create the file | 5 |
| A: Print five calculations | 10 |
| B: Obtain Command line arguments | 2.5 |
| B: Store Id Numbers | 1.5 |
| B: Determine & Store RCS Ids | 7 |
| B: Last Name Sort | 6 |
| B: First Name Sort | 6 |
| B: Id Number Sort | 6 |
| RCS Id Sort | 6 |
| Combining the Parts | 10 |
| Error Checking | 5 |
| Output Style | 5 |
| Code style | 5 |
| No warnings | 5 |
Penalties
- Late
- A submission turned in within 14 hours past the deadline will lose 20 points. A submission turned in more than 14 hours past the deadline will be graded a 0
- Compilation
- If your program fails to compile, it will be graded subjectively based on the code written, and then lose 50% of the remaining points.
No Warnings
No compilation nor runtime warnings can be generated by Perl evaluating your code. That is,
I should never see "use of uninitialized value" or "illegal division by 0" or similar.
(You are, of course, welcome and encouraged to use the warn
and die functions to tell the user when he/she has done something wrong). Your
code will be executed with warnings enabled, even if you don't explicitly
use warnings in your program.
Error Checking
You must check to make sure the user has not done something wrong. "Wrong" in this case means making sure files and paths exist, directory creation and file creation is successful, files in Part A have the right number of lines, the first line contains two values, the user enters a valid sort method letter, etc. Your program should never crash due to unexpected input. A sensible error message should be printed to the user, telling him/her what went wrong.
Code Style
Your code must be easily read by a human being. Most important are three
facets: consistent indentation, meaningful variable names, and explanatory
comments. For a larger guide to writing well-styled Perl code, please
read perldoc perlstyle
Output Style
Your output must be easily read by a human being. Values and data should be labeled. Prompts should be explicit. White space and newlines should be used for visual distinction. Debugging statements should be removed before submitting.
Submission Instructions
Your program must be executable on either rcs-sun4.rpi.edu
or solaris.remote.cs.rpi.edu. Whichever you chooose, run
the program ~lallip/public/submit.pl and follow the prompts.
Your submission is due at 11:59:59pm on Wednesday, February 17, 2009. You may submit infinite times. Only the last submission will be graded.
