Skip to content

Commit 4168455

Browse files
authored
Merge pull request #256 from barbarer/master
ITiCSE study materials for class-exp and dclass-exp
2 parents 80c972e + 608025e commit 4168455

20 files changed

Lines changed: 1922 additions & 0 deletions
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
Creating Classes
2+
-----------------------------------------------------------------
3+
4+
Look the code below. It defines a class. it also declares *methods* which are
5+
functions that are defined inside of a class.
6+
One of the methods, ``__init__``, is automatically called when a new object is
7+
created by the class. One of the methods, ``__str__``, is automatically
8+
called when you print an object of the class. These methods start and end with two underscores.
9+
10+
A Book Class
11+
======================================================
12+
13+
.. activecode:: class_book_ac1_v2
14+
:caption: A class to represent a book
15+
16+
Run the following code
17+
~~~~
18+
class Book:
19+
""" Represents a book object """
20+
21+
# initializes the values in a new object called self
22+
def __init__(self, title, author):
23+
self.title = title # set title in self to the passed title
24+
self.author = author # set author in self to the passsed author
25+
26+
# returns a string with information about the object self
27+
def __str__(self):
28+
return "title: " + self.title + " author: " + self.author
29+
30+
def main():
31+
# calls the __init__ method
32+
b2 = Book("A Wrinkle in Time", "M. L'Engle")
33+
34+
# calls the __str__ method
35+
print(b2)
36+
37+
# calls the __init__ method
38+
b1 = Book("Goodnight Moon", "Margaret Wise Brown")
39+
40+
# calls the __str__ method
41+
print(b1)
42+
43+
main()
44+
45+
46+
Creating More Objects
47+
======================================================
48+
49+
Once you have defined a class you can use it to create many objects.
50+
51+
.. activecode:: class_person_ac2
52+
:caption: A class to represent a Person
53+
54+
Change the following main function to add a new person object.
55+
~~~~
56+
class Person:
57+
""" Represents a person object """
58+
59+
# initializes the values in a new object called self
60+
def __init__(self, first, last):
61+
self.first = first # set first in self to the passed first
62+
self.last = last # set last in self to the passed last
63+
64+
# returns a string with information about the object self
65+
def __str__(self):
66+
return self.first + " " + self.last
67+
68+
def main():
69+
# calls the __init__ method
70+
p1 = Person("Barbara", "Ericson")
71+
72+
# calls the __str__ method
73+
print(p1)
74+
75+
# create an object for another person (calls the __init__ method)
76+
77+
# print the new object (calls the __str__ method)
78+
79+
main()
80+
81+
Add a Method to a Class
82+
======================================================
83+
84+
You can add a new method to a class by adding a new function inside the class. For example, you can add the ``initials``
85+
method to the Person class. The name of the function
86+
doesn't need to have any underscores in it. It only needs to start and end with double
87+
underscores if it is a special method like ``__init__`` or ``__str__``. It does need to take
88+
the current object which is by convention referred to as ``self``.
89+
90+
.. activecode:: class_person_init_ac1_v2
91+
:caption: A class to represent a Person
92+
93+
The following Person class has an ``initials`` method that returns
94+
a string with the first letter in the first name and the first letter in
95+
the last name in lowercase.
96+
~~~~
97+
class Person:
98+
""" Represents a person object """
99+
100+
# initializes the values in a new object called self
101+
def __init__(self, first, last):
102+
self.first = first # set first in self to the passed first
103+
self.last = last # set last in self to the passed last
104+
105+
# returns a string with information about the object self
106+
def __str__(self):
107+
return self.first + " " + self.last
108+
109+
# returns the first characters of the first and last name in lowercase
110+
def initials(self):
111+
return self.first[0].lower() + self.last[0].lower()
112+
113+
def main():
114+
# calls the __init__ method
115+
p1 = Person("Barbara", "Ericson")
116+
117+
# calls the __str__ method
118+
print(p1)
119+
120+
# calls the initials method
121+
print(p1.initials())
122+
123+
main()
124+
125+
====
126+
from unittest.gui import TestCaseGui
127+
class myTests(TestCaseGui):
128+
129+
def testOne(self):
130+
p1 = Person("Barbara", "Ericson")
131+
self.assertEqual(p1.initials(),'be',"testing initials for Barbara Ericson")
132+
p2 = Person("Enoch", "Obe")
133+
self.assertEqual(p2.initials(),"eo", "testing initials for Enoch Obe")
134+
135+
myTests().main()
136+
137+
Feedback
138+
==================================
139+
140+
.. shortanswer:: class-intro-classes-ps-sa
141+
142+
Please provide feedback here. Please share any comments, problems, or suggestions.
143+
144+
What to do next
145+
============================
146+
147+
.. raw:: html
148+
149+
<p>Click on the following link to take the pre survey : <b><a id="class-survey"> <font size="+2">Pre Survey</font></a></b></p>
150+
151+
.. raw:: html
152+
153+
<script type="text/javascript" >
154+
155+
window.onload = function() {
156+
157+
a = document.getElementById("class-survey")
158+
a.href = "class-presurvey.html"
159+
};
160+
161+
</script>
162+
163+
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
Introduction to the Problem Types
2+
-----------------------------------------------------
3+
4+
Please read the following, watch the videos, and try to solve the problems.
5+
6+
Solving Mixed-up Code Problems
7+
==================================
8+
9+
If you see a problem like the one below you will need to put the mixed-up
10+
code in the correct order on the right side. You
11+
may need to indent the blocks as well. There may also be extra blocks that are not
12+
needed in a correct solution that you can leave on the left side. Click the "Check" button
13+
to check your solution.
14+
15+
See the video below for an example.
16+
17+
.. youtube:: Rf7oWHlo-e0
18+
:divid: classexp-parsons1-p3-v2
19+
:optional:
20+
:width: 650
21+
:height: 415
22+
:align: center
23+
24+
Try to solve the following mixed-up code problem. This problem doesn't require any indentation.
25+
26+
.. parsonsprob:: intro-simple-parsons-no-indent-classexp
27+
:numbered: left
28+
:adaptive:
29+
:practice: T
30+
:order: 3, 1, 2, 0
31+
32+
Drag the blocks from the left and put them in the correct order on the right. The text in each block
33+
defines the order.
34+
-----
35+
First block
36+
=====
37+
Second block
38+
=====
39+
Third block
40+
41+
Try to solve the following mixed-up code problem. This problem requires indentation.
42+
43+
.. parsonsprob:: intro-simple-parsons-indent-classexp
44+
:numbered: left
45+
:adaptive:
46+
:practice: T
47+
:order: 3, 1, 2, 0
48+
49+
Drag the blocks from the left and put them in the correct order on the right with the correct indentation.
50+
The text in each block defines the order and indentation.
51+
-----
52+
First block
53+
=====
54+
Second block
55+
=====
56+
Third block that needs to be indented
57+
58+
Try to solve the following mixed-up code problem. This problem requires indentation and has extra blocks that are not needed in a correct solution.
59+
60+
.. parsonsprob:: intro-simple-parsons-indent-with-dist-classexp
61+
:numbered: left
62+
:adaptive:
63+
:practice: T
64+
:order: 3, 1, 2, 0
65+
66+
Drag the blocks from the left and put them in the correct order on the right with the correct indentation.
67+
There is an extra block that is not needed in the correct solution.
68+
-----
69+
First block
70+
=====
71+
Second block
72+
=====
73+
Extra block that is not needed #paired: This block is not needed
74+
=====
75+
Third block that needs to be indented
76+
77+
The mixed-up code problems have a "Help me" button at the bottom of the
78+
problem. Once you have checked at least three incorrect solutions you can
79+
click the button for help. It will remove an incorrect code block, if you used
80+
one in your solution, or combine two blocks into one if there are more
81+
than three blocks left.
82+
83+
See the video below for an example.
84+
85+
.. youtube:: QejZ7u642IU
86+
:divid: classexp-parsons2-p3
87+
:optional:
88+
:width: 650
89+
:height: 415
90+
:align: center
91+
92+
Solving Write Code Problems
93+
==============================
94+
95+
If you see a problem like the one below, you will need to write code. The problem
96+
will have unit tests that you can run to check that your code is working
97+
correctly. Click on the "Run" button to compile and run your code. Look after
98+
the code area for compiler errors and/or unit test results.
99+
100+
See the video below for an example.
101+
102+
.. youtube:: w9hTOJ7iJpE
103+
:divid: classexp-write-code-video-ex
104+
:optional:
105+
:width: 1020
106+
:height: 826
107+
:align: center
108+
109+
Finish writing the code for the following problem.
110+
111+
.. activecode:: intro-sample-write-code-triple-p3
112+
:practice: T
113+
:autograde: unittest
114+
115+
Write a function called ``triple(num)`` that takes a number ``num`` and
116+
returns the number times 3. For example, ``triple(2)`` should return 6 and
117+
``triple(-1)`` should return -3. Look below the code to check for any
118+
compiler errors or the results
119+
from the test cases. Be sure to ``return`` the result.
120+
~~~~
121+
def triple(num):
122+
# write code here
123+
124+
print(triple(2))
125+
print(triple(-1))
126+
127+
====
128+
from unittest.gui import TestCaseGui
129+
class myTests(TestCaseGui):
130+
131+
def testOne(self):
132+
self.assertEqual(triple(2),6,"triple(2)")
133+
self.assertEqual(triple(3),9,"triple(3)")
134+
self.assertEqual(triple(-1),-3,"triple(-1)")
135+
self.assertEqual(triple(0),0,"triple(0)")
136+
self.assertEqual(triple(11),33,"triple(11)")
137+
138+
myTests().main()
139+
140+
Feedback
141+
==================================
142+
143+
.. shortanswer:: classexp-ex1-intro-ps-sa
144+
145+
Please provide feedback here. Please share any comments, problems, or suggestions.
146+
147+
What to do next
148+
============================
149+
.. raw:: html
150+
151+
<p>Click on the following link to learn how to create new classes : <b><a id="intro-class"><font size="+2">Creating Classes</font></a></b></p>
152+
153+
.. raw:: html
154+
155+
<script type="text/javascript" >
156+
157+
window.history.pushState(null, null, window.location.href);
158+
window.onpopstate = function () {
159+
window.history.go(1);
160+
}
161+
162+
window.onload = function() {
163+
164+
a = document.getElementById("intro-class")
165+
a.href = "class-intro-classes.html"
166+
167+
};
168+
169+
</script>

_sources/experiments/class-pnd.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Practice Problems
2+
-----------------------------------------------------
3+
4+
Please answer
5+
the following problems to the best of your ability without any
6+
outside help. You can stop working on a problem after you worked
7+
on it for about five minutes without solving it.
8+
9+
.. selectquestion:: class-song-pnd-sq
10+
:fromid: Classes_Basic_Song_nd_pp
11+
:points: 10
12+
13+
.. selectquestion:: class-cat-pnd-sq
14+
:fromid: Classes_Basic_Cat_nd_pp
15+
:points: 10
16+
17+
.. selectquestion:: class-book-pnd-sq
18+
:fromid: Classes_Basic_Book_nd_pp
19+
:points: 10
20+
21+
.. selectquestion:: class-account-pnd-sq
22+
:fromid: Classes_Basic_Account_nd_pp
23+
:points: 10
24+
25+
.. selectquestion:: class-fortune-pnd-sq
26+
:fromid: Classes_Basic_FortuneTeller_nd_pp
27+
:points: 10
28+
29+
Feedback
30+
============================
31+
32+
.. shortanswer:: class-pnd-sa
33+
34+
Please provide feedback here. Please share any comments, problems, or suggestions.
35+
36+
37+
What to do next
38+
============================
39+
.. raw:: html
40+
41+
<p>Click on the following link to go to the post test: <b><a id="class-post"><font size="+2">Post Test</font></a></b></p>
42+
43+
.. raw:: html
44+
45+
<script type="text/javascript" >
46+
47+
window.history.pushState(null, null, window.location.href);
48+
window.onpopstate = function () {
49+
window.history.go(1);
50+
}
51+
52+
window.onload = function() {
53+
54+
a = document.getElementById("class-post")
55+
a.href = "class-post.html"
56+
};
57+
58+
</script>

0 commit comments

Comments
 (0)