Skip to content

Commit

Permalink
Merge pull request #160 from xinyinghou/master
Browse files Browse the repository at this point in the history
Materials for the Win 24 study on writing code with scaffolding
  • Loading branch information
barbarer authored Jan 2, 2024
2 parents f31e9af + 1916cab commit 63c5cbf
Show file tree
Hide file tree
Showing 11 changed files with 436 additions and 172 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified _sources/.DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion _sources/functions/fl-write.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Problems
:fromid: get-middle
:points: 10


.. selectquestion:: fl-list_loop_two_lists-ps-sq-write
:fromid: list_loop_two_lists
:points: 10
Expand Down
10 changes: 5 additions & 5 deletions _sources/objects/intro-class-206.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ Hi! We are a team of researchers in Professor Ericson’s Lab at UMSI!
adopted way. <br> </p>

<h4> What Will Occur </h4>
<p>This study takes 50 minutes to one hour to finish. The study materials include:</p>
<p>This study takes 60 minutes to one hour to finish. The study materials include:</p>
<ul>
<li>Knowledge Review - A basic knowledge review about <b>Class</b> (~5 mins)</li>
<li>Knowledge Introduction - A basic knowledge introduction about Python <b>Class</b> (~5 mins)</li>
<li>System Introduction - Materials to get you familiar with the types of problems in this study (~5 mins)</li>
<li>Pre Survey - Questions about your level of programming self-efficacy (~5 mins)</li>
<li>Practice - Four code practice problems (~20 mins)</li>
<li>Posttest - A short posttest consists of 4 programming questions (~15 mins)</li>
<li>Practice - Five code practice problems (~30 mins)</li>
<li>Posttest - A short posttest consists of 5 programming questions (~15 mins)</li>
</ul>
<p>Each section must be completed in order. After finishing one part, you can click the link provided under <b>"What to do next"</b> to the next part.</p>

Expand All @@ -33,6 +33,6 @@ Hi! We are a team of researchers in Professor Ericson’s Lab at UMSI!
.. note::
.. raw:: html

<h4>Click on the following link 👉 <b><a href="intro-fl-cls.html">Basic Knowledge Review</a></b> 👈 to start!</h4>
<h4>Click on the following link 👉 <b><a href="intro-cls.html">Basic Knowledge Introduction</a></b> 👈 to start!</h4>


170 changes: 170 additions & 0 deletions _sources/objects/intro-cls.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
Introduction to Python Class
-----------------------------------------------------------------

Creating Classes
======================================================

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, ``__init__``, is automatically called when a new object is
created by the class. One of the methods, ``__str__``, is automatically
called when you print an object of the class. These methods start and end with two underscores.

.. activecode:: class_exp2_book_ac1_v2
:caption: A class to represent a book

Run the following code
~~~~
class Book:
""" Represents a book object """

# initializes the values in a new object called self
def __init__(self, title, author):
self.title = title # set title in self to the passed title
self.author = author # set author in self to the passsed author

# returns a string with information about the object self
def __str__(self):
return "title: " + self.title + " author: " + self.author

def main():
# calls the __init__ method
b2 = Book("A Wrinkle in Time", "M. L'Engle")

# calls the __str__ method
print(b2)

# calls the __init__ method
b1 = Book("Goodnight Moon", "Margaret Wise Brown")

# calls the __str__ method
print(b1)

main()


Creating More Objects
======================================================

Once you have defined a class you can use it to create many objects.

.. activecode:: class_exp2_person_ac2
:caption: A class to represent a Person

Change the following main function to add a new person object.
~~~~
class Person:
""" Represents a person object """

# initializes the values in a new object called self
def __init__(self, first, last):
self.first = first # set first in self to the passed first
self.last = last # set last in self to the passed last

# returns a string with information about the object self
def __str__(self):
return self.first + " " + self.last

def main():
# calls the __init__ method
p1 = Person("Barbara", "Ericson")

# calls the __str__ method
print(p1)

# create an object for another person (calls the __init__ method)

# print the new object (calls the __str__ method)

main()

Add a Method to a Class
======================================================

You can add a new method to a class by adding a new function inside the class. For example, you can add the ``initials``
method to the Person class. The name of the function
doesn't need to have any underscores in it. It only needs to start and end with double
underscores if it is a special method like ``__init__`` or ``__str__``. It does need to take
the current object which is by convention referred to as ``self``.

.. activecode:: class_exp2_person_init_ac1_v2
:caption: A class to represent a Person

The following Person class has 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.
~~~~
class Person:
""" Represents a person object """

# initializes the values in a new object called self
def __init__(self, first, last):
self.first = first # set first in self to the passed first
self.last = last # set last in self to the passed last

# returns a string with information about the object self
def __str__(self):
return self.first + " " + self.last

# returns the first characters of the first and last name in lowercase
def initials(self):
return self.first[0].lower() + self.last[0].lower()

def main():
# calls the __init__ method
p1 = Person("Barbara", "Ericson")

# calls the __str__ method
print(p1)

# calls the initials method
print(p1.initials())

main()

====
from unittest.gui import TestCaseGui
class myTests(TestCaseGui):

def testOne(self):
p1 = Person("Barbara", "Ericson")
self.assertEqual(p1.initials(),'be',"testing initials for Barbara Ericson")
p2 = Person("Enoch", "Obe")
self.assertEqual(p2.initials(),"eo", "testing initials for Enoch Obe")

myTests().main()


Glossary
======================================================

**class**
A template that can be used to construct an object. Defines
the attributes and methods that will make up the object.

**attribute**
A variable that is part of a class.

**constructor**
An optional specially named method ( ``__init__`` ) that is called at the
moment when a class is being used to construct an object.
Usually this is used to set up initial values for the object.

**inheritance**
When we create a new class (child) by extending an
existing class (parent). The child class has all the attributes
and methods of the parent class plus additional attributes and
methods defined by the child class.

**method**
A function that is contained within a class and the objects
that are constructed from the class. Some object-oriented
patterns use 'message' instead of 'method' to describe this concept.


What to do next
============================

.. raw:: html

<h4>Click to see an introduction about our system 👉 <b><a href="system-into-cls.html">System Introduction</a></b></h4>
107 changes: 0 additions & 107 deletions _sources/objects/intro-fl-cls.rst

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@ Post Test
-----------------------------------------------------

Please try to solve each of the following problems. This test is **ungraded**.
Please do not ask for external help and try your best to finish it! It is OK to not know the correct answers!
Please do not ask for external help and try your best to finish it!
It is OK to not know the correct answers!

Problems
==============

.. selectquestion:: fl-post-1-Movie
.. selectquestion:: we-pp-post-1-Movie
:fromid: Classes_Basic_Movie_fix_v3_ac
:points: 10

.. selectquestion:: fl-post-2-Horse
.. selectquestion:: we-pp-post-2-Rectangle
:fromid: Classes_Basic_Rectangle_ac_fix_v2
:points: 10

.. selectquestion:: we-pp-post-3-Horse
:fromid: Classes_Basic_Horse_v2_ac
:points: 10

.. selectquestion:: fl-post-3-GasTank
.. selectquestion:: we-pp-post-4-GasTank
:fromid: Classes_Basic_GasTank_ac
:points: 10

.. selectquestion:: fl-post-4-Dice
.. selectquestion:: we-pp-post-5-Dice
:fromid: Classes_Basic_Dice_fix_v2_ac
:points: 10

Feedback
==================================

.. shortanswer:: fl-posttest-sa-cls

Please provide feedback here. Please share any comments, problems, or suggestions.

Thank You 🤗
============================
Expand Down
Loading

0 comments on commit 63c5cbf

Please sign in to comment.