Download presentation
Presentation is loading. Please wait.
Published byKatherine Marsh Modified over 9 years ago
1
1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"
2
University of Utah – School of Computing University of Utah 2 Procedural Programming What we've done so far: -variables -methods that operate on those variables
3
University of Utah – School of Computing University of Utah 3 Large-Scale Programming Hard to write BIG programs using procedural programming techniques WHY?
4
University of Utah – School of Computing University of Utah 4 Problem #1 Global Variables!
5
University of Utah – School of Computing University of Utah 5 Problem #2 Limits of human comprehension
6
University of Utah – School of Computing University of Utah 6 Problem #3 Brittle dependencies
7
University of Utah – School of Computing University of Utah 7 Conceptual Leap 1. Dividing a large job into small methods 2. Dividing several large jobs (or concepts) into multiple classes!
8
University of Utah – School of Computing University of Utah 8 Analogy Toy blocks
9
University of Utah – School of Computing University of Utah 9 Classes Have variables and methods Or "attributes" and "behaviors"
10
University of Utah – School of Computing University of Utah 10 10 Example Modify our circle drawing program What attributes does a circle have?
11
University of Utah – School of Computing University of Utah 11 11 Circle class public class Circle { int x, y; int radius; Color color; }
12
University of Utah – School of Computing University of Utah 12 12 Circle class What do you want to do with a circle? -Draw it? -Move it? -Resize it? -Calculate area? -Calculate circumference?
13
University of Utah – School of Computing University of Utah 13 13 Methods class Circle { int x, y; int radius; Color circle; void draw(Graphics g) { //do stuff } double computeArea() { //do stuff }
14
University of Utah – School of Computing University of Utah 14 14 How to draw a circle? void draw(Graphics g) { }
15
University of Utah – School of Computing University of Utah 15 15 How to find the circle's area? double computeArea() { }
16
University of Utah – School of Computing University of Utah 16 16 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();
17
University of Utah – School of Computing University of Utah 17 17 Block example revisited Wrap up all attributes and methods inside a class Easier to manage! Safer too? Why?
18
University of Utah – School of Computing University of Utah 18 18 Car example You can drive a car without knowing how it works internally "public" user interface: -steering wheel -gas/brake pedals -ignition
19
University of Utah – School of Computing University of Utah 19 19 Car example You can drive a car without knowing how it works internally "private" inner workings -don't know, don't care
20
University of Utah – School of Computing University of Utah 20 20 "public" explained public methods and variables -Other classes can use them So far, this is all we've used
21
University of Utah – School of Computing University of Utah 21 21 "private" also! private methods and variables -Other classes may NOT access them Used for internal data
22
University of Utah – School of Computing University of Utah 22 22 A General Rule private variables -they are our internal data public methods -they are how other classes use our class
23
University of Utah – School of Computing University of Utah 23 23 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 }
24
University of Utah – School of Computing University of Utah 24 24 Why public and private? Secrecy? No. Simplicity? Yes. Order? Yes. Maintainability? Yes.
25
University of Utah – School of Computing University of Utah 25 25 Examples Math.random() JPanel.add()
26
University of Utah – School of Computing University of Utah 26 26 Tomorrow Work on "Circle" example together
27
University of Utah – School of Computing University of Utah 27 27 Reminder Homework 5 is due tomorrow, not Wednesday, due to Fourth of July Office hours today from 9:45 to noon
28
University of Utah – School of Computing University of Utah 28 28 Request for Comments Please fill out online survey!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.