Python Computer Science

Question

COSC 1336 Programming Fundamentals 

Lab 5: Strings & Classes                    

You will modify your Lab to handle a different date format from the input file and to work with the data as objects in a one-dimensional list rather than values in two parallel lists.

NOTE: Make sure you do only what you are asked, do not use concepts that have not been covered. Show me you can follow directions and that you can use the material covered in your book. This lab will use decisions, repetition, functions, files, lists and exception handling.

Remember that program input and output is not the same thing as function input and output.  Function input is what is passed in on the parameter list, NOT reading data from the keyboard.  Function output is anything returned to the calling function whether it is on the parameter list or in a return statement.

Your main will still read the contact information out of a file. The file, contactsLab5.txt, is with the assignment attached below. This file contains records with 2 fields, name and birthdate, each field value is on a separate line. You do not know how many records are in the file so you will read util you get to the end of the file.

You will include exception handling to keep your program from crashing if the file does not exist. You will not be able to complete the program tasks if the file cannot be opened. Your program will just display a message and the program will end if the file cannot be opened.

You will not use your parallel lists any more, you will use one list of objects.

You will create a class for your contact information. Here are the details of the class:

  1. The class is called Contact
  2. The class will have two private data attributes – name, birthdate – both strings
  3. The class will have an init method that initializes the two data attributes to an empty string (“”)
  4. The class will have an accessor and mutator for each of the two data attributes: get_name, set_name, get_birthdate, set_birthdate.
  5. The class will have a method called find_season that will return the season the birthdate is in. You will have to modify this code to process the birthdate in a different format. (You will remove this function from your program and replace it with code to invoke the new class method find_season for each object.)
  6. The class will have a method called calculate_age that will return the age of the contact stored in the object. You will have to modify this code to process the birthdate in a different format. (You will remove this function from your program and replace it with code to invoke the new class method calculate_age for each object.)

 

 

The format of the birthdate in the contact file has changes as shown here:

Index

Name

Birthdate

0

Henry Bemis

August 4, 2008

1

Penthor Mul

May 3, 1928

 

After displaying the contact information you will still display the average age of your contacts.

You will modify the code that finds the season of the birth month since it is now stored as a string rather than an integer. You CANNOT use a list to process the birthdate – you must process it as a string. (You CANNOT  use the string split method since it creates a list.) YOU CANNOT USE A LIST TO PROCESS THE BIRTHDATE STRING – YOU MUST PROCESS IT AS A STRING. You will have to figure out a different way of isolating, or picking off, the characters you want to process. (Hint: You want to process the characters stored from the beginning of the string up to the first space by isolating them in a variable then deciding which season the belong to.) You could convert the string month to its integer equivalent and use the same decision structure you used in the previous lab.

You will modify the code that determines whether the year is a leap year or not. You CANNOT use a list to process the birthdate – you must process it as a string. (You CANNOT  use the string split method since it creates a list.) YOU CANNOT USE A LIST TO PROCESS THE BIRTHDATE STRING – YOU MUST PROCESS IT AS A STRING. You will have to figure out a different way of isolating, or picking off, the characters you want to process. (Hint: You want to process the digits stored after the comma character by isolating them in a variable then converting them to the integer equivalent.

You will modify the code that calculates the age of the contact. You will have to pass in the current date to this class method in the same format as the dates coming from the file (e.g. May 3, 1928.) You CANNOT use a list to process the birthdate – you must process it as a string. (You CANNOT  use the string split method since it creates a list.) YOU CANNOT USE A LIST TO PROCESS THE BIRTHDATE STRING – YOU MUST PROCESS IT AS A STRING. You will have to figure out a different way of isolating, or picking off, the characters you want to process. (Hint: You have written new code to get the birth month and the birth year, now all you have to do is isolate the day and convert it to its integer equivalent. Now you have all of the pieces to calculate the age.

Once you have read all records from the file, created a new object, set the two data attributes and appended the new object to the list, you will pass the list to a function display_contacts that will display the following information in table format with column headings and columns lined up just as in Lab 4:

Name                       Age               Season           Leap Year

Henry Bemis              12                 Summer         Yes
Penthor Mul              92                 Spring            Yes
etc….

The display_contacts function will ask the user for the current date in the format (month-spelled-out day, year) as shown above. The current date may be any date from the current date to any date in the future. The display_contacts function will invoke the class methods calculate_age(), find_season() and is_leap_year() for each object in the list and display the information in table form as shown above just as in Lab 4.

 

Your class definition should be in a separate module. You will import that module into your program file. Use names specified below under “You will submit the following in Blackboard”.

You will have the following data attributes in your class Contact:

  1. name - string
  2. birthdate – string

You will have the following methods in your class Contact:

  1. init method – sets name and birthdate to an empty string – “”
  2. set_name(string contact_name)
  3. get_name() – returns a string
  4. set_birthdate(string contact_birthdate)
  5. get_birthdate() – returns a string
  6. find_season() – returns a string
  7. is_leap_year() – returns “yes” or “no”
  8. calculate_age() – takes as input the current date - return an integer

You do not have to pass anything to the find_season, is_leap_year, or the calculate_age methods because the class code has direct access to the private data attributes through the “self” parameter.

You will submit the following:

Your class definition should be in a separate module named “lab5_class_Lastname.py”.
Your program source code named “lab5_FirstName_Lastname.py”.
 

NOTE – Violating any of the following will get you a 20 point deduction in your lab grade :

  1. You will NEVER call main() more than once.
  2. You will NEVER use break, exit, quit, stop, end or anything to leave a loop, function, or other construct prematurely.
  3. You will NEVER have a function call itself.
  4. You will NEVER use global variables.  However, you may use global “constants” if used properly.
  5. You will have only one return statement in a function if it returns a value.

 

NOTE – Once your lab grade has been posted in Blackboard, you will have access a copy of my solution attached to your grade.

Lab Grade Points                 

  1. Comments – 20 points
    1. program description - include your name, lab number and description intended for a programmer to read to know what your program does
    2. function descriptions (starting with lab 3)
    3. major algorithm steps (the main steps to accomplish the task)
  1. Followed directions – 25 points
  2. Correct output – 25 points
  3. Structured program design – 20 points
  4. Meaningful identifier names – 10 points

 

Program descriptions should look similar to this:

########################################

# Your first and last name

# Which lab this is – Lab 1              

# This program does this…… blah blah blah                                                    

########################################

 

Function descriptions should look similar to this:

###############################################

# Function name: function_name

# Input: Describe values passed on the parameter list

# Output: Describe the value(s) returned

# Purpose: This function does this…    

###############################################

Details
Attachments
Purchase An Answer Below

Have a similar question?