1 university of utah – school of computing computer science 1021 "object-oriented...
TRANSCRIPT
11University 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
University of Utah – School of Computing
University
of Utah
33
Large-Scale Programming
• Hard to write BIG programs using procedural programming techniques
• WHY?
University of Utah – School of Computing
University
of Utah
44
Problem #1
• Global Variables!
University of Utah – School of Computing
University
of Utah
55
Problem #2
• Limits of human comprehension
University of Utah – School of Computing
University
of Utah
66
Problem #3
• Brittle dependencies
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!
University of Utah – School of Computing
University
of Utah
88
Analogy
• Toy blocks
University of Utah – School of Computing
University
of Utah
99
Classes
• Have variables and methods• Or "attributes" and "behaviors"
University of Utah – School of Computing
University
of Utah
1010
Example
• Modify our circle drawing program• What attributes does a circle have?
University of Utah – School of Computing
University
of Utah
1111
Circle class
public class Circle {int x, y;int radius;Color color;
}
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?
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
}
}
University of Utah – School of Computing
University
of Utah
1414
How to draw a circle?
void draw(Graphics g) {
}
University of Utah – School of Computing
University
of Utah
1515
How to find the circle's area?
double computeArea() {
}
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();
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?
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
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
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
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
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
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
}
}
University of Utah – School of Computing
University
of Utah
2424
Why public and private?
• Secrecy? No.• Simplicity? Yes.• Order? Yes.• Maintainability? Yes.
University of Utah – School of Computing
University
of Utah
2525
Examples
• Math.random()• JPanel.add()
University of Utah – School of Computing
University
of Utah
2626
Tomorrow
• Work on "Circle" example together
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
University of Utah – School of Computing
University
of Utah
2828
Request for Comments
• Please fill out online survey!