Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf ·...

18
Object-Oriented Programming Java Basics 8-28-2013

Transcript of Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf ·...

Page 1: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

Object-Oriented Programming

Java Basics

8-28-2013

Page 2: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

Object-Oriented Programming Review

Java Basics:

public methods: constructors, accessors, mutators, etc.

instance variable vs. argument vs. local variable

Class variables/constants: “static”

Javadoc

Design Issues

HW#1 posted; due Wednesday, 9/4/13

Page 3: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

key idea: dynamic objects interact by sending messages to each other; how something is done depends on who is asked to do it

OOP languages: Simula, Smalltalk, C++, Lisp/Scheme, Java, C#, etc.

Page 4: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

“object” collection of private data & public

operations

“class” description of a set of objects

“instance” an instance of a class is an object

in that class

“method” procedure which carries out some

operation

“message” request to execute a method (like

a procedure call)

Page 5: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

Standard engineering design

vs.

Object-Oriented design

Discussed in class

Page 6: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

Encapsulation: keep classes separated and prevent them from having to know too much about each other (hide the implementation)

Inheritance: reuse code

Page 7: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

Everything (except a primitive) is an object

An object has an interface and a state

Page 8: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

Consider the following perspectives:

Class designer/implementer

- designs & implements a class

vs.

Client programmer

- uses a class for an application

vs.

End-user

- uses the application

Page 9: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

representation:

center Point

radius float (primitive type)

public interface:

constructor(s): Circle(x,y) // unit circle

Circle(radius,x,y)

float getRadius();

void setRadius(float);

float computeArea();

Page 10: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

Circle

constructor(s): Circle(int,int),

Circle(float,int,int);

float computeArea();

float getRadius();

void setRadius(float);

// etc.

Type name

Public

interface

Page 11: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

Circle c1 = new Circle(1.58f, 10,20);

float z = c1.getRadius();

Circle c2 = new Circle(z, 57, 42);

float y = c2.computeArea();

Circle c3;

c2 = c1;

What is c1? c1 is a “reference” to an

instance of the class Circle; so is c2;

c3 is a null reference;

Page 12: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

private instance variables:

private float radius;

private Point center;

public methods:

constructor(s)

accessor methods (get)

mutator methods (set)

… etc.

note: must repeat keyword “private” for each instance variable

Page 13: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

Implementing constructors: a common problem occurs when a parameter is the same name as an instance variable; e.g.

public Circle(float radius, int x, int y) { radius = ??? solution: use keyword “this”

Page 14: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

public Circle(float radius, int x, int y) { this.radius = radius; center = new Point(x,y); } It’s typical to have multiple ways to

create an object; the constructors are distinguished by their “signatures”

Page 15: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

Retrieves the current value of an instance variable

public float getRadius()

{

return radius;

}

Page 16: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

changes the value of an instance variable

public void setRadius(float newRadius)

{

this.radius = newRadius;

}

Page 17: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

Implementing computeArea()

need to calculate

how? Make good use of Javadoc

2rΠ

Page 18: Object-Oriented Programming Java Basics 8-28-2013jsearlem/cs242/fa13/lectures/02.OOP.pdf · Object-Oriented Programming Java Basics 8-28-2013 Object-Oriented Programming Review ...

javadoc used to find PI

public static final double PI

“class” constant

(1 per class, not per instance)

Math.PI note: Math is a classname

There’s a problem in computeArea() with

return radius * radius * Math.PI;

Why?