CSCI-1101A Object-Oriented Programming (OOP) Mohammad T . Irfan - - PDF document

csci 1101a
SMART_READER_LITE
LIVE PREVIEW

CSCI-1101A Object-Oriented Programming (OOP) Mohammad T . Irfan - - PDF document

12/11/14 CSCI-1101A Object-Oriented Programming (OOP) Mohammad T . Irfan Reading: Ch 16 (Guzdial) Optional: Ch 8 (Guttag) OOP u A different style of programming from functional decomposition u Focuses on objects u Why


slide-1
SLIDE 1

12/11/14 ¡ 1 ¡

CSCI-1101A

Object-Oriented Programming (OOP)

Mohammad T . Irfan

Reading: Ch 16 (Guzdial) Optional: Ch 8 (Guttag)

OOP

u A different style of programming from

functional decomposition

u Focuses on “objects” u Why OOP?

slide-2
SLIDE 2

12/11/14 ¡ 2 ¡

Flappy Bird

http://youtu.be/YHH2101OFfI

slide-3
SLIDE 3

12/11/14 ¡ 3 ¡

Objects vs. Classes

u Objects are the “things” (nouns) that we

see/perceive/model

u Properties/attributes (nouns) – instance variables u Behavior/actions (verbs) – methods

u Classes

u Factory for creating objects u Template or blueprint for creating objects

Turtle Objects

Demo

slide-4
SLIDE 4

12/11/14 ¡ 4 ¡

Codes

def turtleDemo(): zoo = World() bob = Turtle(zoo) bob.setColor(blue) ann = Turtle(zoo) ann.setColor(red) bob.forward() ann.turnRight() ann.forward(200) bob.turnLeft() bob.backward(-150) bob.forward()

Key points

u Two objects of the same class have different

properties/attributes

u Two turtles with different colors, position, etc.

u But they share the same behavior

u forward(), turn(), etc.

slide-5
SLIDE 5

12/11/14 ¡ 5 ¡

Writing our own class

Hannaford Inventory

u Hannaford’s inventory consists many items.

Each item has a name, barcode, price, and quantity (in stock). Hannaford can change the price of an item anytime. The quantity is also changed when Hannaford sells an item

  • r restocks it.
slide-6
SLIDE 6

12/11/14 ¡ 6 ¡

Item class

>=

Item class (continued)

slide-7
SLIDE 7

12/11/14 ¡ 7 ¡ main() function

  • utside of

the Item class

Problems

u Bank account

u A bank account has a name and a balance, among

  • ther information. The balance gets changed when

the account holder withdraws or deposits money. u Slide show

u Each slide in a slide show has a picture and some

  • music. Each slide also has a show functionality,

which shows the picture and plays the music. u Smart turtle

u Special type of turtle that can show tricks u Inherits the Turtle class

slide-8
SLIDE 8

12/11/14 ¡ 8 ¡

Announcement

u Reminder Exam 5 on Tuesday, Dec 9 u Topics

u Recursion (see the slides on recursion)

u Watch the video lectures

u OOP (up to today’s class)

u Practice the codes from the slides and the textbook

(Guzdial)

u Additional practice (optional): Guttag’s book

Class Participation HW 6 Work in groups of 2 students Due: Thursday

u Create another class named Inventory that

keeps a list of Item objects. The list is initially empty, but objects of the Item class can be added to it. The Inventory class will have certain functionalities like finding the lowest price item and finding the minimum stock item.

slide-9
SLIDE 9

12/11/14 ¡ 9 ¡

A card game

u This card game is called “all or nothing.”

This is a human vs. computer game. The game starts with a deck of cards, which has 52 cards in it. Each card has one of four possible suits and one of 13 possible ranks.

u This is how the game is played.

1.

The dealer shuffles the deck of cards. Neither the computer nor the human can see any of the cards.

2.

The computer picks a card from the deck randomly.

3.

The human can choose any of the 51 remaining cards from the deck (without seeing, of course).

4.

Whoever has the better suit wins. If both suits are the same, the player with the better rank wins.

Questions

u What classes would you design? u What would be their instance variables and

methods?

u How would you simulate playing the game?

slide-10
SLIDE 10

12/11/14 ¡ 10 ¡

slide-11
SLIDE 11

12/11/14 ¡ 11 ¡

Inheritance

“Is-A” relationship

u Bowdoin College MealPlan

slide-12
SLIDE 12

12/11/14 ¡ 12 ¡

Modeling one week’s meal plan

u Generic meal plan – point/cash based u Special meal plans

u Res 19 plan u Res 14 plan u Res 10 plan

u Examples

u Rob lives off-campus and uses the generic plan. He

is charged on a per meal basis.

u Jon enrolls into the Res 19 plan. Additional meals

in a week will be charged according to the generic meal plan.

Key point: Res 19 plan is a special type of meal plan

slide-13
SLIDE 13

12/11/14 ¡ 13 ¡

Demo

Another Problem: Create smart turtles

u A smart turtle is a turtle that has a high IQ

  • level. It can show tricks like this:
slide-14
SLIDE 14

12/11/14 ¡ 14 ¡

SmartTurtle class Demo

slide-15
SLIDE 15

12/11/14 ¡ 15 ¡

Final exam

u Thursday, 12/18 at 9am in Searls 223

classroom

u Topics

u The basics of Python (Guttag’s Ch 2, 3, Section

4.1)

u Text and files (Chapter 10, class + lab) u Recursion (Chapter 15.4, class + lab, Optional:

Guttag’s Section 4.3– see pdf)

u Object-oriented programming (Chapter 16,

Optional: Guttag’s Ch 8– see pdf) u [Nothing on picture/sound]

For Data Structures

u Head start

u Learn the syntax (grammars) of C and C++ u Python to C++ transition guide:

http://personal.denison.edu/~krone/cs173/files/ PythontoC++.pdf

u C/C++ Tutorial: Practice codes from

http://www.cprogramming.com/tutorial/c- tutorial.html

u C++ compiler

u Netbeans (among a host of other options) u https://netbeans.org/features/cpp/index.html

slide-16
SLIDE 16

12/11/14 ¡ 16 ¡

Outlook

u NY Times: May 2, 2014

slide-17
SLIDE 17

12/11/14 ¡ 17 ¡

Thank You!