CSC 131: Introduction to Computer Science Dr. Curry Guinn
Quick Info Dr. Curry Guinn CIS 2025 guinnc@uncw.edu www.uncw.edu/people/guinnc 962-7937 Office Hours: MTR: 10:00am-11:00m and by appointment Teaching Assistant: Zachary Krepps Tuesday, 8:00am-9:30am, 12:45pm-3:45pm Thursday, 8:00am-9:30am Office: GTA office – off of CIS 2004 lab
For Next Class, Thursday Homework 10 is due, Wednesday. We will do most of the work in class. It’s extra credit too, replacing your lowest Homework grade. Final exam next week, Wednesday, December 6, 11:30am. Final exam is cumulative First midterm + second midterm + UML diagram questions Tracing a try-except block Coding: Create a class in Python based on a UML diagram.
Today IDEA : https://uncw.campuslabs.com/courseeval/ Catching exceptions Object-oriented programming
Handling Exceptions What happens when you run this code? answer = "5.5" number = int(answer) print(number)
Catching Exceptions answer = "5.5" try: number = int(answer) print(number) except: print(answer, "is not a valid integer.")
Catching Exceptions answer = "5.5" try: number = int(answer) print(number) except ValueError: print(answer, "is not a valid integer.")
Raising Exceptions print("I will compute the square root") number = input("Enter a number: ") try: num = float(number) if num < 0: raise RuntimeError("You can’t use a negative number") print(math.sqrt(num)) except ValueError: print("That is not a number!") except RuntimeError as e: print(e)
UML for BankAccount
What other methods might be good for a BankAccount What other methods might be good for a BankAccount? (Roughly where we were: BankAccount.py and MakeAccounts.py) def deposit(self, amt): self.balance += amt def withdraw(self, amt): self.balance -= amt account1 = BankAccount(100, "Jane Doe") account2 = BankAccount(3000, "Mary Smith") print(account1) print(account2) account1.deposit(75.25) account2.withdraw(317.18)
More on Bank Account Methods that return values Write calculateInterest such that it returns 0.015 times the balance. Test it!
Making a list of BankAccounts Create a loop that iterates 100 times. Inside of the loop, create a bank account object and add it to a list. Now, iterate through the list, printing out each bank account.
Making random accounts Here is a list of common first names: FirstNames.txt Here is a list of common last names: LastNames.txt Let’s randomly select a first name and a last name to create a random person. Also, we’ll randomly generate a bank account balance Now, write this to a file called “accounts.txt”.
Reading in some objects Create a new file, ProcessAccounts. At the top put from BankAccount import * Now have your program open the file accounts.txt Read in all the bank accounts, creating a corresponding BankAccount object and putting it in a list Read in a line and split() it. Use those pieces to build a BankAccount object.
Homework 10 http://people.uncw.edu/guinnc/courses/Fall17/131/hw/Homework1 0.htm
Another Example Defining a Student object
Defining a Student Step 1: What would a UML diagram for a student look like? Let’s assume we need to store the student’s first name, last name and gpa. What methods might you want to support?
Step 2: Defining the class and the constructor Create a Python file called “Student”. Add a constructor that has three instance variables: firstName lastName gpa In another Python file within the same project (call it Roster), let’s create a few students and print them out. You’ll need to “from Student import *” to use the Student class in Roster. Now, test it out by creating a few students and printing out those objects.
Step 3: Overriding the __str__ method Let’s make life easier by re-defining the str method so that it prints out a Student object in a pretty way.
Step 4: Reading in students from a file I’ve created a file roster.txt that contains a list of students in the following format <last name><SPACE><first name><SPACE><gpa> In the Roster file, let’s open up the roster.txt file and read in each line. Now, how do we break up each line so that we can get the individual information? split() By splitting the input, we can get each string. Now, you can create a Student object for each line in the input file. Once you’ve created a Student object, add it to a list of students. When you’ve finished reading in the whole file, write another loop to print out each student. How hard would it be to find the student with the greatest GPA? Try it.