CSC111 Homework 10
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 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.
The missing link
The program below assumes that the information relating to a student is stored in objects.
.
"""
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
- get the account number
- get the password
- get the email
- change the email address
"""
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, name, passw, email ) )
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()
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