Object - Stts

carenextSoftware and s/w Development

Nov 18, 2013 (3 years and 8 months ago)

87 views

Lecture 2



Object
-
oriented Programming Concepts

Erick
Pranata

© Sekolah Tinggi Teknik Surabaya

1

»
Object

»
Class

»
Inheritance

»
Technical Details

2

© Sekolah Tinggi Teknik Surabaya

© Sekolah Tinggi Teknik Surabaya

3

4

© Sekolah Tinggi Teknik Surabaya

5

© Sekolah Tinggi Teknik Surabaya

»
Modularity

»
Information
-
hiding

»
Code re
-
use

»
Pluggability and debugging ease

6

© Sekolah Tinggi Teknik Surabaya

© Sekolah Tinggi Teknik Surabaya

7




A class is a blueprint or prototype from
which objects are created

8

© Sekolah Tinggi Teknik Surabaya

class Bicycle {



int cadence = 0;


int speed = 0;


int gear = 1;



void changeCadence(int
newValue) {


cadence = newValue;


}

9

© Sekolah Tinggi Teknik Surabaya


void changeGear(int newValue) {


gear = newValue;


}



void speedUp(int increment) {


speed = speed + increment;


}



void applyBrakes(int decrement) {


speed = speed
-

decrement;


}



void printStates() {


System.out.println("cadence:" +


cadence + " speed:" +


speed + " gear:" + gear);


}

}

10

© Sekolah Tinggi Teknik Surabaya

class BicycleDemo {


public static void main(String[] args) {


Bicycle bike1 = new Bicycle();


Bicycle bike2 = new Bicycle();



bike1.changeCadence(50);


bike1.speedUp(10);


bike1.changeGear(2);


bike1.printStates();



bike2.changeCadence(50);


bike2.speedUp(10);


bike2.changeGear(2);


bike2.changeCadence(40);


bike2.speedUp(10);


bike2.changeGear(3);


bike2.printStates();


}

}

11

© Sekolah Tinggi Teknik Surabaya

© Sekolah Tinggi Teknik Surabaya

12

13

© Sekolah Tinggi Teknik Surabaya

class
MountainBike

extends Bicycle {




// new fields and methods defining


// a mountain bike would go here


}

14

© Sekolah Tinggi Teknik Surabaya

© Sekolah Tinggi Teknik Surabaya

15

class
MyClass


extends
MySuperClass


implements
YourInterface


{




// field, constructor, and


// method declarations



}

16

© Sekolah Tinggi Teknik Surabaya

»
Class, Field, Constructor, Method


17

© Sekolah Tinggi Teknik Surabaya

Modifier

Class

Package

Subclass

World

public

Y

Y

Y

Y

protected

Y

Y

Y

N

no modifier

Y

Y

N

N

private

Y

N

N

N

public class DataArtist {


...


public void draw(String s) {


...


}


public void draw(int i) {


...


}


public void draw(double f) {


...


}


public void draw(int i, double f) {


...


}

}

18

© Sekolah Tinggi Teknik Surabaya

»
Every class can have more than one constructor
(overloading constructor)


public Bicycle(


int startCadence,


int startSpeed,


int startGear

) {


gear = startGear;


cadence = startCadence;


speed = startSpeed;

}


public Bicycle() {


gear = 1;


cadence = 10;


speed = 0;

}

19

© Sekolah Tinggi Teknik Surabaya

public Polygon polygonFrom(Point... corners){


int numberOfSides = corners.length;


double squareOfSide1, lengthOfSide1;


squareOfSide1 =


(corners[1].x
-
corners[0].x) *


(corners[1].x
-

corners[0].x) +


(corners[1].y
-

corners[0].y) *


(corners[1].y
-

corners[0].y);


lengthOfSide1 =


Math.sqrt(squareOfSide1);



// more method body code follows that


// creates and returns a polygon


// connecting the Points

}

20

© Sekolah Tinggi Teknik Surabaya

Point
originOne

= new Point(23, 94);



21

© Sekolah Tinggi Teknik Surabaya

Rectangle rectOne =


new Rectangle(originOne, 100, 200);



22

© Sekolah Tinggi Teknik Surabaya

»
Methods can return value

˃
Primitive Data Type

˃
Object


Point getPoint() {


return new Point(5,8);

}

23

© Sekolah Tinggi Teknik Surabaya

public class Bicycle {



private int cadence;


private int gear;


private int speed;



// add an instance variable for the object ID


private int id;



// add a class variable for the


// number of Bicycle objects instantiated


private static int numberOfBicycles = 0;

}

»
Can also be “class method”

24

© Sekolah Tinggi Teknik Surabaya

»
Object
-
oriented Programming
Concepts,
http://docs.oracle.com/javase/tutorial/j
ava/concepts/object.html

25

© Sekolah Tinggi Teknik Surabaya

© Sekolah Tinggi Teknik Surabaya

26

»
Create Class Mahasiswa

»
Instantiate Mahasiswa

»
Manipulate beberapa object Mahasiswa
(Reference Concept)

»
Asisten, inherit Mahasiswa

27

© Sekolah Tinggi Teknik Surabaya