关键词 > Python代写

Intro to Programming Fall 2022 Assignment 9

发布时间:2022-11-29

Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Intro to Programming Fall 2022

Assignment 9

Objectives: Approach old problems using Object Oriented Programming. Write classes and methods.

Note: Include DocStrings in each script you submit analogous to the following:

"""Creates a directory of representatives using OOP

Submitted by Mauricio Arias. NetID: ma6918

The same problem handled before but using object oriented programming.

In particular, constructors, get-methods and set-methods are used.

Two classes are defined: Seat, which contains the information for the seat

and Person, which contains the information for the representative.

"""

Make single-line DocStrings for all methods and multiline DocStrings for all classes. For the latter use a similar format as for the DocStrings used for the scripts: in the lower part of the DocString  include a simple description for each attribute and a simple description for each method: in many cases PyCharm should prompt you.

Note: In all the scripts below, you are to catch the exceptions when opening files and tell the user there was a problem opening the file. Catch only the exception for files not found.

Full assignment. House of representatives

For this assignment, we’ll make classes to represent key types of information to be stored. The general script for the search will remain similar as the one we used before: see below. Keep track of what goes into the classes and what goes in the main script.

Key aspects: Use csv.DictReader. Make a Seat class for the seats in the house of representatives: these  seats will correspond to a district in a state or a state at large. We will assume that the phone number and the office number are fairly constant and associated with the seat rather than with the person holding the seat. We will also make a class Person that will be associated with the positions: that is, the                 representatives as persons.

For simplicity, collect all the labels manually and make appropriate variables to be able to extract the information from the mini-dictionary read with the dictionary reader.

state_label = "State or Territory"

district_label = "District or Representation Type"

phone_label = "Phone "

office_label = "Office Room "

given_name_label = "Given name"

family_name_label = "Family name"

party_label = "Party "

Task 1 (2 points). Reading the csv file in the main script and creating objects.

a)  Make a class Seat.

b)  The constructor for this class (__init__) should set only the state and the district via two arguments: state and district.

The phone number and office number attributes will be set to empty strings.

c)  In the main script, read the house of representatives file and make an object seat for each state and district: include the at-large and the numbered districts but ignore the rest.

d)  In the class, make a method that sets (stores) the phone number and another method that sets the office number.

e)  In the class, make also a method that gets (retrieves) the phone number and another method that gets the office number. Notice that at this point, the objects do not have a person associated with them.

f)  In the main script, set the phone number and the office number using these methods. (As a test print the phone number and office number stored in the object.)

Name the file [NetID]_house_OOP_1.py.

Task 2 (1 point). Displaying objects and storing them in a dictionary.

Note: __str__ is a special type of method, a dunder method, that is not called directly. Python calls it on our behalf when we print an object of that class: say by writing print(current_seat).

a)  In the class Seat make a method __str__ that returns a string analogous to the following: Seat for district 12 in the state of New York.

Phone number: (202) 225-7944

Office: 2308 RHOB

(Don’t forget to use self.)

b)  In the main script store all the seats in a dictionary: the tuple of state and district as the key, and the seat object as the value. This is similar to what we did in previous assignments but instead of the mini-dictionary we store the object. (You can reuse code from previous assignments.)

c)  Print the dictionary using a pythonic loop to verify it is all working correctly. (Don’t do anything special about printing the objects. The code we wrote takes care of that.)

(For your own learning print the dictionary directly to observe the differences: print(directory).)

Name the file [NetID]_house_OOP_2.py.

Task 3 (2 points). Making another class: Person.

a)  In the main script, while reading the house of representatives file, assign the information for the representatives to objects of a class Person. The constructor for this class should set the given   name and the family name for the representative. (We could also include other personal            information here but it will become unnecessarily complex.) Set other attributes as you think of them to empty strings if necessary.

b)  Initially, make three methods for this class and provide only two attributes (one for the family name and one for the given name). The first method allows us to get the given name, the second one allows us to get the family name and the third one allows us to get the full name as a single  string. (In general it would be a good idea to also make methods to set the first two values.        However, we won’t be doing that here for simplicity.)

c)  Write the __str__ dunder method for the Person class, call the method to get the full name that you made before. Return the result; don’t print it from inside the method. The output in the example of New York 12 should be:

Representative: Carolyn Maloney

d)  In the main script, as you are reading the csv file, make a Person object with the information for the representative. This object is made in addition to the Seat object. They are separate objects at this point.

e)  Print each representative object as it is made to verify your code is working correctly. Name the file [NetID]_house_OOP_3.py.

Task 4 (3 points). We are going meta: associating one object (Person) with another object (Seat).

a)  Make a method in the Seat class that allows the association of an object of the Person class     with the seat by providing an attribute named rep. This method sets rep equal to the object of the class Person passed as an argument. Think of it as a variable (attribute) to store the object  person: notice that this is a name that references the object. In this sense it is as we know for   any object in Python.

b)  Make a method in the class seat that allows us to get the full name for the representative         associated with the seat: this method will call the corresponding method in the Person class for the corresponding rep object. (Think of it as you would regarding any method for a list or a   dictionary.)

c)  Modify the Person class so that in the constructor a new attribute party is set to an empty       string. Make an additional method that allows us to set the party and another one that gets the party information.

d)  Modify the __str__ method so that it now includes the party information but only if it has been set: i.e., it is not an empty string.

Representative: Carolyn Maloney (D)  # or

Representative: Carolyn Maloney  # If party not set

Notice how contained the changes now are.

e)  Print the house dictionary to verify it is all working correctly.

Name the file [NetID]_house_OOP_4.py.

Task 5 (2 points). Queries and results.

To make this similar to previous assignments, we will add another part to the script to retrieve information about specific representatives. We used objects before but they were not as fancy as the ones we have       now. The task is done just as we did before but using our fancy classes/objects. As part of the main script

a)  read the file of representatives, only once, one row at a time. (This should be done by now.)

b)  store the information for the seats in the appropriate objects. (This should be done by now.)

c)  make the appropriate person object for each seat to store the info for the representative. (This should be done by now.)

d)  associate the person with the seat by setting the representative for the seat. (This should be done by now.)

e)  store each seat in a dictionary using a tuple with the state and the district as the key. (This should be done by now.)

f)   ask the user to input a state and district.

g)  retrieve the information of the seat and the representative and display it by directly printing the seat object.

h)  Modify the __str__ method for the Seat class to make the information for At-large representatives be printed out better.

At large seat for the state of Alaska.

Phone number: (202) 225-5765

Office: 2314 RHOB

Representative: Don Young (R)

But for the others it should be as follows:

Seat for district 12 in the state of New York.

Phone number: (202) 225-7944

Office: 2308 RHOB

Representative: Carolyn Maloney (D)

Name the file [NetID]_house_OOP.py.

Only submit the most advanced file you generated: a single file. For example, submit only                  [NetID]_house_OOP.py if you finished the last task, otherwise [NetID]_house_OOP_3.py if you finished Task3 (but not tasks 4 and 5) and so on and so forth.

Take some time to appreciate what you have done. It is remarkable. Think about this approach and how it compares to the other approaches we have used. Rewatch the lecture on OOP and assess whether we       accomplish what OOP is about.