CSC 131: Introduction to Computer Science

Slides:



Advertisements
Similar presentations
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
General Programming Introduction to Computing Science and Programming I.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Head First Python: Ch 3. Files and Exceptions: Dealing with Errors Aug 26, 2013 Kyung-Bin Lim.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 231: Introduction to Data Structures Python and Objects – Day 3 Dr. Curry Guinn.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Lesson 06: Functions Class Participation: Class Chat:
Exceptions in Python Error Handling.
Introduction to Computing Science and Programming I
CompSci 101 Introduction to Computer Science
Lesson 08: Files Class Participation: Class Chat: Attendance Code 
similar concepts, different syntax
Introduction to Programming
CSc 020: Programming Concepts and Methodology II
Example: Vehicles What attributes do objects of Sedan have?
CSC 131: Introduction to Computer Science
Java Course Review.
CSC 131: Introduction to Computer Science
Lesson 04: Conditionals Topic: Introduction to Programming, Zybook Ch 3, P4E Ch 3. Slides on website.
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CSC 131: Introduction to Computer Science
CSC 131: Introduction to Computer Science
CSC 131: Introduction to Computer Science
CSC 131: Introduction to Computer Science
CMSC201 Computer Science I for Majors Lecture 14 – For Loops
CSC 380: Design and Analysis of Algorithms
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
Object Oriented Programming
Exceptions and files Taken from notes by Dr. Neil Moore
CS190/295 Programming in Python for Life Sciences: Lecture 1
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Introduction to Programming
Topics Introduction to File Input and Output
Writing Methods AP Computer Science A.
CompSci 101 Introduction to Computer Science
Functions and Procedures
Lesson 08: Files Topic: Introduction to Programming, Zybook Ch 7, P4E Ch 7. Slides on website.
Exceptions and files Taken from notes by Dr. Neil Moore
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Introduction to Programming
Fundamentals of Programming I Commonly Used Methods More Modeling
Lesson 08: Files Class Chat: Attendance: Participation
Python programming exercise
CSC 380: Design and Analysis of Algorithms
Recursion Taken from notes by Dr. Neil Moore
Seating “chart” Front Back 4 rows 5 rows 5 rows 4 rows 2 rows 2 rows
CISC101 Reminders All assignments are now posted.
CMSC201 Computer Science I for Majors Final Exam Information
Topics Sequences Lists Copying Lists Processing Lists
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Introduction to Computer Science
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSC 380: Design and Analysis of Algorithms
Introduction to Programming
CSC 380: Design and Analysis of Algorithms
Topics Introduction to File Input and Output
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
More Basics of Python Common types of data we will work with
IST256 : Applications Programming for Information Systems
CSC 380: Design and Analysis of Algorithms
Presentation transcript:

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.