CS 120 Project 5 – Files, Classes, and Objects
Fall 2014 – due Mon Nov 24
Write Python programs that involve files, classes and objects. The programs involving files should have most of the file processing activities encapsulated in a separate function. Each class definition with its member variables and methods should be in its own file.
http://www.pme.gov.sa/en/en_airpollution.asp
1. Input text data from a file: The main program should allow the user to choose a data file. The program should then open the file for reading. Input the contents of the file into a single string S, and then close the file..
Your main program should next have the user input a single word W (e.g. “who”). The program should call the function countWord(S,W), which will return the number of occurrences of word W in string S. The count should be case insensitive, so convert string S and word W to lower case before performing the word count. Note that W sometimes may be part of a longer word, which increases the count.
Output the word (in lower-case) along with the search frequency. For each of the files listed below, run your program for all of the following words (one word at a time):
Words: the, and, he, she, his, her
Files: (a) DeclarationOfIndependence.txt
(b) PrideAndPrejudiceCh1.txt
(c) SilverBlaze.txt
2. More text file analysis: The main program should allow the user to choose a data file. The program should then open the file for reading. Input the contents of the file into a single string S, and then close the file.
Your main program should next have the user input a single word W with no repeating characters (e.g. “abcXYZ”). The program should call the function countChars(S,W). This function will return a list F containing frequencies for each character in W. The frequencies in the list should appear in the same order as the characters in W.
The counts should be case insensitive, so convert string S and word W to lower case before performing the character counts.
Output each character in word W paired with the frequency for that character. Run your program for the following test cases:
(a) File: RoadNotTaken.txt Word: “TRaeiou”
(b) File: IfByKipling.txt Word: “YFaeiou”
(c) File: Jabberwocky.txt Word: “JBaeiou”
3. Encryption string class: Write a Python class called EString to implement a class of strings that can be encrypted and decrypted. The class has two private member variables:
(a) a string W that represents the string to be encrypted or decrypted.
(b) an integer variable K that is the key to be used to encrypt or decrypt the string.
Write code for the following public methods in this class:
(1) A constructor EString(W) that creates an EString object S and sets the first member variable to the lower-case version of string W. The initial key value is set to 0.
(2) A S.setKey(K) method that sets the key value to integer K (-25 <= K <= 25).
(3) A S.encrypt() method that returns an encrypted version of string S, using the current value of key K. The encryption algorithm is a Caesar cipher with shift K. Only lower-case letters are encrypted by the cipher. Note: This method can be used to both encrypt (using K) and decrypt (using –K).
4. Encryption strings: Write a Python test program that demonstrates the methods of the EString class. Input a string W and a key value K. Then create an EString object S1.
Perform the following encryption and decryption activities:
(1) Encrypt your string using key K. Call the resulting string S2.
(2) Then decrypt string S2 using key – K. Call the result S3.
Your test program should then output strings S2 and S3.
Run your test program for the following cases, and display the results.
(a) W = Have a nice day! K = 3
(b) W = I love dark chocolate. K = 5
(c) W = My password is “meatballs”. K = -8
(d) W = We attack the enemy at dawn! K = 17