Open main menu

CDOT Wiki β

Changes

OPS445 Online Lab6

257 bytes added, 15:48, 20 January 2023
wget addr switched to github
== PART 1 - Creating a Class ==
:Each object of a class that we write in Python can contain values, functions, code, but none of the code is executed or run until the class is used to create an object. Remember that the class is a blueprint for how your object will work, the object that will be created is what will actually be processed by the python interpreter. This part of a class works in the same way as a function definition, the function doesn't run until it's executed, code inside classes doesn't get executed until they are made into objects.
 
<blockquote style="margin-left:35px;">{{Admon/note|style="padding-left:25px"|NOTE|If your professor is asking you to submit labs on GitHub, first follow their instructions on Blackboard to clone the lab6 repository.}}</blockquote>
:'''Perform the Following Steps:'''
# Add new courses for student1
student1.addGrade('uli101', 4.0)
student1.addGrade('ops235ops245', 3.5)
student1.addGrade('ops445', 3.0)
</source>
:# The method '''addGrade()''' changes the '''self.courses''' dictionary. But both student1 and student2 have their OWN courses dictionary.
:# Once an object is created the attributes inside may be modified externally. Though you may need to be careful, if a value is a string, and you change it to another type such as a integer or a list, it's possible the class was not designed to deal with the different type and may throw a error when you run a method. For example, changing the name to a integer would break the displayStudent method, because it would try and concatenate strings and integers(maybe the method should be written better).<source lang="python">
# student1.name is a string like any other
print(student1.name)
student1.name = 'Jack'
print(student1.name)
print(len(student1.name))
</source>
:# The final part to creating objects is understanding how to pass values into newly created objects, such as, '''student2 = Student('Jessica', '023384103')'''. How did it know that Jessica is the name instead of the long string of numbers? This is done when the class is written, inside the __init__() method. Look at the arguments used for in the __init__ definition:<source lang="python">
=== Create a Python Script Demonstrating Classes ===
:# The following python script is broken. It has two major problems to fix and one new feature to add: <br />'''first problem''' is providing the student number as an integer causes an error(TypeError) when displayStudent() is run, <br />'''second problem''' is in displayGPA() may divide by zero(ZeroDivisionError) if no courses are added to the dictionary or the grades added to the dictionary are 0.0 floats. <br />'''Finally''', you will add a new method to this class that prints out a formatted list of all courses the student has takenpassed.
:#Create the '''~/ops445/lab6/lab6a.py''' script.
:#Use the following as a template(warning this is NOT the same as student.py):<source lang="python">
student1 = Student('John', '013454900')
student1.addGrade('uli101', 1.0)
student1.addGrade('ops235ops245', 2.0)
student1.addGrade('ops445', 3.0)
Student Number: 013454900
GPA of student John is 2.0
['ops445', 'ops235ops245', 'uli101']
Student Name: Jessica
Student Number: 123456
cd ~/ops445/lab6/
pwd #confirm that you are in the right directory
ls CheckLab6.py || wget 'https://ictgithub.senecacollege.cacom/~eric.brauersenecaops445/ops445lab6-template/labsblob/LabCheckScriptsmaster/CheckLab6.py?raw=true' -O CheckLab6.py
python3 ./CheckLab6.py -f -v lab6a
</source>