User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3...

23
Department of Information Technology Uppsala University User Interface Programming OOP/Java Primer Step 1

Transcript of User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3...

Page 1: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Department of Information Technology Uppsala University

User Interface Programming OOP/Java Primer

Step 1

Page 2: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #2 @ UU/IT

Object-Oriented Programming

  Focus on OOP rather than Java to start with

  What is an Object?

Page 3: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #3 @ UU/IT

Object-Oriented Programming

  Focus on OOP rather than Java to start with

  What is an Object?

”I Exist! Therefore I am!”

Page 4: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #4 @ UU/IT

Object   An entity, a collection of data*

  A blueprint for the creation of

INSTANCES

* And some other stuff too

Page 5: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #5 @ UU/IT

Objects (instances)   Knows ”all” about themselves

•  Data •  Available ”messages” •  What they need

Page 6: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #6 @ UU/IT

Inheritance

  Objects can inherit from each other •  Object hierarchies •  X inherits from Y ==> ”X is a type of Y”

  Inheritance is a strong programming concept •  But can be overused

Page 7: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #7 @ UU/IT

Basic structure

public class <Name> { //class variables

//instance variables

//constructors

//accessor methods

//class methods }

Page 8: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #8 @ UU/IT

Concepts   Class variables = common to all instances   Instance variables = unique in every instance

  Constructor = Initializing method   Accessor = returns contents of variables

•  Provides for encapsulation   Methods = performs the ”real work”

Page 9: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #9 @ UU/IT

class Ship { private int speed = 0; private int course = 0;

public void setCourse(degree) { course = degree; } public int getCourse () { return course;}

public void move() { … } public report() {

System.out.println( ”Course is ” + course); …}

}

Page 10: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #10 @ UU/IT

class ShipMain1 { public static void main(String [] args){ Ship argo = new Ship(); argo.setCourse(90); argo.move(); argo.report(); } }

// ”setCourse” is an accessor method.

Page 11: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #11 @ UU/IT

OOP vs. Procedures   An object encapsulates a certain ”aspect” of a

system   It defines what we may do with this ”aspect”   It has a limited scope

•  only works with its own responsibility

  It communicates with other objects to make things happen

Page 12: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #12 @ UU/IT

Good programming practice   Make small objects

•  Clear definition of what the object is.   Make small methods

•  Clear definition of what the method does

  Give the object a simple structure •  Don’t mix too much in a single object

  Use inheritance (with some constraints)

Page 13: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #13 @ UU/IT

Private data

  Modifiers: ensure class integrity

  Private, Protected, Public – controls access   Static – declares class variables and methods   Final – declares constant values

Page 14: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #14 @ UU/IT

Good Programming Practice   Do not let other classes modify variable

values directly!   Use Accessor methods!

•  boat.speed = 10; // Bad! •  boat.setSpeed(10); // Good!

  The accessor method can make integrity checks.

Page 15: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #15 @ UU/IT

Packages

  A collection of classes that belong together

  A package defines a special part of the application

  Files within a package are stronger related to each other

Page 16: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #16 @ UU/IT

Relations

•  Knows about

•  Has – contains other objects

•  Is –  Inheritance relation

Page 17: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #17 @ UU/IT

Geometry   Simple package to calculate geometric

shapes •  Cylinders, balls, pyramids, cones and boxes

  Simple arithmetic calculations   In- and output

Page 18: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #18 @ UU/IT

Sphere radius : double;

volume : double; surface : double;

Cylinder radius : double; height : double; volume : double; surface : double;

Etc.

Page 19: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #19 @ UU/IT

public class Sphere {!!private double radius; !!private static final double PI = Math.PI;!

!!!public Sphere () { radius = 1; }!

public Sphere (double r) { radius = r;}!

!public void setRadius (double r) { // Accessor!! !radius = r; !

!} !! public double getRadius () {!

! !return radius;! !}!

public double volume () { !! !return 4 * PI * radius * radius * radius / 3;!

!}!!public static double volume(double r) { // Class method that can be used!! !return 4 * PI * r * r *r / 3; ! // directly without an instance!! ! ! !}!

} !

Page 20: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #20 @ UU/IT

Sphere Cylinder Cone Pyramid Box Kube

Geometric objects

Page 21: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #21 @ UU/IT

Sphere Cylinder Cone

Round objects

Square objects

Geometric objects

Pyramid Box

Kube

Page 22: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #22 @ UU/IT

Choose Shape

Start

Choose parameter

Requests >

< Param

eters

Sphere

Cylinder

Cone

Cube

.

.

.

Round O

bjects

Square O

bjects

Geo

metric O

bjects Object-Oriented

Design

Page 23: User Interface Programming OOP/Java Primer Step 1 · Uppsala University @ UU/IT 1/17/11 | #3 Object-Oriented Programming Focus on OOP rather than Java to start with What is an Object?

Uppsala University

1/17/11 | #23 @ UU/IT

Good programming practice

  Classes should be: •  Correct – good overview •  Efficient – small routines, little overhead •  Reusable •  Changeable