CSC111 Homework 10

From dftwiki3
Revision as of 06:05, 8 April 2010 by Thiebaut (talk | contribs) (Problem #1: A list of students)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

For this assignment, I provide you with a main program, you provide the missing class definition. This assigment is due Thursday, 4/15/10 at 11:59 p.m. + 1 minute.


Problem #1: A list of students

""" 
hw10a.py
D. Thiebaut
 
A program with Classes and Objects.  You describe
the program here.
"""
 
text="""
111c-ca, Liz Cloud, xxyzzyK, lcloud@email.smith.edu
111c-cb, Fiona Poory, ahlhla, fpoory@email.smith.edu
111c-ck, Alex Doe, aluoiid, adoe@gmail.com
"""
 
def parseInput( text ):
    lines = text.split( "\n" )
    List = []
    for line in lines:
        words = line.split( ',' )
        if len( words )==4:
            (account, name, passw, email) = words
        List.append( (account, name, passw, email) )
 
def main():
    List = parseInput( text )
    print "List = ", List
 
main()



The program above does not use OOP. It takes a string of characters, text, splits it up into fields, and creates a list of tuples with 4 elements, where the first element is the account number of the student, the second her/his name, the fourth his/her password, and teh forth the email address.

We are going to apply OOP to it!

The missing link

The program below has the same purpose as the one above, but it uses classes and objects. The information relating to a student is now stored in an object.

.
""" 
studentClass.py
D. Thiebaut
 
A program for the Exercises on Class/Objects
Creates a class to keep information about a student.
The class keeps track of the student's name, account number, 
password, and email address.
 
Object issued from this class have methods to
- print the student information (everything we know about a student)
- create a new student with all the information
- get the name of a student
- set the name of the student
- get the account number
- set the account number
- get the password
- get the email
- set the email of the student
"""
import os
 
#--------------------------------------------------------
# original list of students 
#--------------------------------------------------------
text="""
111c-ca, Liz Cloud, xxyzzyK, lcloud@email.smith.edu
111c-cb, Fiona Poory, ahlhla, fpoory@email.smith.edu
111c-ck, Alex Doe, aluoiid, alexDoe@email.smith.edu
"""
 
class Student:
 
     # missing code
     # all the methods of the class Student are missing
     # and it is your job to recreate them!
 
 
 
#--------------------------------------------------------
# parseInput: you are not allowed to change the
#                   code of this function
#--------------------------------------------------------
def parseInput( text ):
    """ parses the original text and creates a list of student objects"""
    lines = text.split( "\n" )
    List = []
    for line in lines:
        words = line.split( ',' )
        if len( words )==4:
            (account, name, passw, email) = words
            #--- create a new object and add it to the list of objects ---
            List.append( Student( account.strip(), name.strip(), passw.strip(), email.strip() ) )
    return List
 
#--------------------------------------------------------
# main
#--------------------------------------------------------
def main():
    """ creates a list of students, displays the list, changes the email
    address to reflect new change """
 
    #--- create a list of student objects---
    List = parseInput( text )
 
    #--- display the information for all the students in the list ---
    print "Original List"
    for stu in List:
        stu.printAll()  
 
    #--- Smith college decided to change its email address from "email.smith.edu" to 
    #--- "smithcollege.edu", so all the email addresses currently on record for students
    #--- have to change...
    #--- change all email addresses ---
    for stu in List:
        email = stu.getEmail().replace( "email.smith.edu", "smithcollege.edu" )
        stu.setEmail( email )
 
    #--- show the list again ---
    print "\n\nNew List after change of email address"
    for stu in List:
        stu.printAll() 
 
    #--- user Fiona Poory has changed her last name to Litmus... her record must be changed ---
    for stu in List:
        if stu.getName().find( "Fiona" )!= -1 and stu.getName().find( "Poory" )!=-1:
             stu.setName( "Fiona Litmus" )
             email = stu.getEmail()
             email = email.replace( "fpoory", "flitmus" )
             stu.setEmail( email )

    #--- show the list again ---
    print "\n\nNew List after change of name"
    for stu in List:
        stu.printAll() 

    #--- a new student has just joined Smith ---                                                                                       
    List.append(  Student( "111c-bz", "Rachel Maddow", "msnbcms", "rmaddow@smithcollege.edu" ) )

    #--- show the list again ---                                                                                                       
    print "\n\nNew List after addition of student"
    for stu in List:
       stu.printAll()

main()

.


The output of the program is shown below:

Original List
account: 111c-ca name: Liz Cloud 	passw: xxyzzyK	email: lcloud@email.smith.edu
account: 111c-cb name: Fiona Poory 	passw: ahlhla	email: fpoory@email.smith.edu
account: 111c-ck name: Alex Doe 	passw: aluoiid	email: alexDoe@email.smith.edu


New List after change of email address
account: 111c-ca name: Liz Cloud 	passw: xxyzzyK	email: lcloud@smithcollege.edu
account: 111c-cb name: Fiona Poory 	passw: ahlhla	email: fpoory@smithcollege.edu
account: 111c-ck name: Alex Doe 	passw: aluoiid	email: alexDoe@smithcollege.edu


New List after change of name
account: 111c-ca name: Liz Cloud 	passw: xxyzzyK	email: lcloud@smithcollege.edu
account: 111c-cb name: Fiona Litmus 	passw: ahlhla	email: flitmus@smithcollege.edu
account: 111c-ck name: Alex Doe 	passw: aluoiid	email: alexDoe@smithcollege.edu


New List after addition of student
account: 111c-ca name: Liz Cloud 	passw: xxyzzyK	email: lcloud@smithcollege.edu
account: 111c-cb name: Fiona Litmus 	passw: ahlhla	email: flitmus@smithcollege.edu
account: 111c-ck name: Alex Doe 	passw: aluoiid	email: alexDoe@smithcollege.edu
account: 111c-bz name: Rachel Maddow 	passw: msnbcms	email: rmaddow@smithcollege.edu

Submission

Submit your program as follows:

  submit hw10 hw10a.py


Optional and Extra-Credit

Make the program output the list of student by alphabetical order of their last name.

If you do this part, simply add a new function to your program (don't forget its header) and call it from main. This function will get the list of student objects as parameters and will output the list in alphabetical order of the last names. Then submit hw10a.py as shown above.