1 university of utah – school of computing computer science 1021 "object-oriented...

28
1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

Upload: katherine-marsh

Post on 30-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

11University of Utah – School of Computing

Computer Science 1021 "Object-Oriented Programming"

Page 2: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

22

Procedural Programming

• What we've done so far:- variables- methods that operate on those variables

Page 3: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

33

Large-Scale Programming

• Hard to write BIG programs using procedural programming techniques

• WHY?

Page 4: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

44

Problem #1

• Global Variables!

Page 5: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

55

Problem #2

• Limits of human comprehension

Page 6: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

66

Problem #3

• Brittle dependencies

Page 7: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

77

Conceptual Leap

• 1. Dividing a large job into small methods• 2. Dividing several large jobs (or

concepts) into multiple classes!

Page 8: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

88

Analogy

• Toy blocks

Page 9: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

99

Classes

• Have variables and methods• Or "attributes" and "behaviors"

Page 10: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

1010

Example

• Modify our circle drawing program• What attributes does a circle have?

Page 11: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

1111

Circle class

public class Circle {int x, y;int radius;Color color;

}

Page 12: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

1212

Circle class

• What do you want to do with a circle?- Draw it?- Move it?- Resize it?- Calculate area?- Calculate circumference?

Page 13: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

1313

Methods

class Circle {

int x, y;

int radius;

Color circle;

void draw(Graphics g) {

//do stuff

}

double computeArea() {

//do stuff

}

}

Page 14: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

1414

How to draw a circle?

void draw(Graphics g) {

}

Page 15: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

1515

How to find the circle's area?

double computeArea() {

}

Page 16: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

1616

Classes vs. Objects

• A class is just the "definition" or the code• An object is when you make a variable

whose type is a class• Example:

- JPanel p;- p = new JPanel();

Page 17: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

1717

Block example revisited

• Wrap up all attributes and methods inside a class

• Easier to manage!

• Safer too? Why?

Page 18: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

1818

Car example

• You can drive a car without knowing how it works internally

• "public" user interface:- steering wheel- gas/brake pedals- ignition

Page 19: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

1919

Car example

• You can drive a car without knowing how it works internally

• "private" inner workings- don't know, don't care

Page 20: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

2020

"public" explained

• public methods and variables- Other classes can use them

• So far, this is all we've used

Page 21: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

2121

"private" also!

• private methods and variables- Other classes may NOT access them

• Used for internal data

Page 22: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

2222

A General Rule

• private variables- they are our internal data

• public methods- they are how other classes use our class

Page 23: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

2323

Circle class revisited

class Circle {

private int x, y;

private int radius;

private Color circle;

public void draw(Graphics g) {

//do stuff

}

public double computeArea() {

//do stuff

}

}

Page 24: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

2424

Why public and private?

• Secrecy? No.• Simplicity? Yes.• Order? Yes.• Maintainability? Yes.

Page 25: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

2525

Examples

• Math.random()• JPanel.add()

Page 26: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

2626

Tomorrow

• Work on "Circle" example together

Page 27: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

2727

Reminder

• Homework 5 is due tomorrow, not Wednesday, due to Fourth of July

• Office hours today from 9:45 to noon

Page 28: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"

University of Utah – School of Computing

University

of Utah

2828

Request for Comments

• Please fill out online survey!