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

Post on 19-Apr-2020

6 views 0 download

Transcript of 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

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

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.

“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)

Standard engineering design

vs.

Object-Oriented design

Discussed in class

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

Inheritance: reuse code

Everything (except a primitive) is an object

An object has an interface and a state

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

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();

Circle

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

Circle(float,int,int);

float computeArea();

float getRadius();

void setRadius(float);

// etc.

Type name

Public

interface

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;

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

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”

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”

Retrieves the current value of an instance variable

public float getRadius()

{

return radius;

}

changes the value of an instance variable

public void setRadius(float newRadius)

{

this.radius = newRadius;

}

Implementing computeArea()

need to calculate

how? Make good use of Javadoc

2rΠ

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?