Section16.16Group Work - Classes, constructors, attributes, and methods
It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home 1 .
Note16.16.1.
If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.
Learning Objectives
Students will know and be able to do the following.
Content Objectives:
Identify the parts of a class
Explain what a constructor is and what it does
Explain what the __str__ method does
Explain the difference(s) between a function and method.
Create new objects and name them
Create new methods
Look the code below. It defines a class. it also declares methods which are functions that are defined inside of a class. One of the methods is automatically called when a new object is created by the class. One of the methods is automatically called when you print an object of the class.
Subsection16.16.1A Book Class
Checkpoint16.16.2.
Run the following code
Look at the class definition. It starts with the keyword class. Then answer the following questions.
Checkpoint16.16.3.
Q-2: What is the name of this class?
Checkpoint16.16.4.
Q-3: What is the name of the method that is called when the object is created?
Checkpoint16.16.5.
Q-4: Describe in your own words what the __init__ method does.
Checkpoint16.16.6.
Q-5: What is the name of the method that is called when the object is printed?
Checkpoint16.16.7.
Q-6: Describe in your own words what the __str__ method does.
Checkpoint16.16.8.
Q-7: How many attributes does an object of the Book class have?
Checkpoint16.16.9.
Click on all of the method names in the code below.
Method names are the names of functions that are defined in a class.
Q-9: Describe in your own words what is/are the difference(s) between a function and a method.
Checkpoint16.16.11.
Q-10: What is the name of the first parameter in all of the methods?
Checkpoint16.16.12.
Put the code blocks in order below to create a class Person with a constructor (__init__) method that takes a first and last name and a __str__ method to return a string with the first and last name separated with a space.
class Person:
---
Class Person: #paired
---
def __init__(self, first, last):
---
def __init__(first, last): #paired
---
self.first = first
self.last = last
---
def __str__(self):
---
def __str__(): #paired
---
return self.first + " " + self.last
---
return first + " " + last #paired
Checkpoint16.16.13.
Q-12: Describe in your own words what self means.
Subsection16.16.2Create More Book Objects
Checkpoint16.16.14.
Change the following main function to create a third book object called b3 with a title of “1984” and author “George Orwell”. Print out the values using the print function in the main.
Checkpoint16.16.15.
Change the following main function to add a person object with your first and last name.
Subsection16.16.3Add a Method to a Class
Checkpoint16.16.16.
Change the following Person class to add an initials method that returns a string with the first letter in the first name and the first letter in the last name in lowercase.
If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="groupsub" id=class_basics_groupsub data-size_limit=3> <div class="col-sm-6"> <select id="assignment_group" multiple class="assignment_partner_select" style="width: 100%"> </select> </div> <div id="groupsub_button" class="col-sm-6"> </div> <p>The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.</p> </div> </div>