1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"
-
Upload
katherine-marsh -
Category
Documents
-
view
214 -
download
1
Transcript of 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"
![Page 1: 1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/1.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/2.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/3.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/4.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/5.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/6.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/7.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/8.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/9.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/10.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/11.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/12.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/13.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/14.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/15.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/16.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/17.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/18.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/19.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/20.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/21.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/22.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/23.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/24.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/25.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/26.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/27.jpg)
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"](https://reader030.fdocuments.us/reader030/viewer/2022032708/56649e8f5503460f94b93050/html5/thumbnails/28.jpg)
University of Utah – School of Computing
University
of Utah
2828
Request for Comments
• Please fill out online survey!