95-712 lecture 2: object oriented programming 1 95-712 object oriented programming java lecture 2:...

22
95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

Upload: rhoda-miller

Post on 18-Jan-2018

272 views

Category:

Documents


1 download

DESCRIPTION

Lecture 2: Object Oriented Programming 3 Grady Booch Says Every object has state, behavior and identity. Consider the Light class that follows. Identify the state, behavior and identity.

TRANSCRIPT

Page 1: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

1

95-712 Object Oriented Programming Java

Lecture 2: Object Oriented Programming

Page 2: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

2

Alan Kay’s Summary of OOP

• Everything is an object• A program is a bunch of objects telling each

other what to do by sending messages.• Each object has its own memory made up of

other objects.• Every object has a type.• All objects of a particular type can receive the

same messages.

Page 3: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

3

Grady Booch Says

• Every object has state, behavior and identity.

• Consider the Light class that follows. Identify the state, behavior and identity.

Page 4: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

4

public class Light { private int brightness; public Light() { brightness = 0; } public void on() { brightness = 10; } public void off() { brightness = 0; } public void brighten() { brightness = brightness + 5; } public void dim() { brightness = brightness - 5; if(brightness < 0) brightness = 0; } public int getBrightness() { return brightness; }}

The class has a name.Instances of the class will have private data. When an object is created

the object’s constructor will run.

Other objects will be ableto send five messages toobjects of this class.

Private data is only available to membersof the class.

A class may have invariants - statementsabout internal objectstate that must always be true.

So, how dowe create anobject?

Save in file Light.java.

Page 5: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

5

public class TestLight {

public static void main(String params[]) { Light lt = new Light(); lt.on(); Light outside = new Light(); outside.on(); for(int k = 1; k <= 5; k++) { outside.brighten(); } }}

A class thatonly has a main routine.

Save it in TestLight.java.

Create a Lightobject. Its constructor will run. Create a second Light

object and run its constructor.

“Please turn both lights on andbrighten the outside light.”

If Light.java and TestLight.javaare both in the same directory thenjavac TestLight.java and java TestLightwill do it all.

Page 6: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

6

The Light Class• has high cohesion. The class does one thing well but

does not try to do too much.• has access control. The private data is not directly

accessible to users.• may be used as a base class for inheritance or composition.• Inheritance example: A ColoredLight is-a-kind-of

Light…• Inheritance Example: A LowPowerLight is-a Light…• Composition example: A TrafficLight has three

ColoredLights…

Page 7: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

7

public class ColoredLight extends Light{ private int color; public static final int RED = 2; public static final int YELLOW = 1; public static final int GREEN = 0; public ColoredLight(int c) { color = c; } public int getColor() { return color; } public String display() { if(color == RED) return "RED"; if(color == YELLOW) return "YELLOW"; if(color == GREEN) return "GREEN"; return "Error"; }}

ColoredLight inherits all the features of Light.

Save this in ColoredLight.java.

Every ColoredLight object will have a brightness and a color.

Before this constructor runsthe base class constructor is run.

There is no way to change the color after it has been set.

A ColoredLight is-a-kind-of Light

Page 8: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

8

A LowPowerLight is-a Light

public class LowPowerLight extends Light { public LowPowerLight() { } public void brighten() { brightness = brightness + 5; if(brightness > 20) brightness = 20; }}

“Is-a” means the exact same interface.

For Eckel,” is-a-kind-of”means the derived classhas additional methods.

As is, this won’t work.The variable brightness,in the Light class, must be marked protected rather than private.

Page 9: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

9

public class TrafficLight {

private ColoredLight bank[]; private int state; String direction;

private static final int TOP = 2; private static final int MIDDLE = 1; private static final int BOTTOM = 0;

The TrafficLight classuses composition to include three ColoredLights.

Each TrafficLight objecthas a current state (describingwhich of three lights is on)and faces a direction.

The values TOP, MIDDLEand BOTTOM are constantsassociated with this class.

Page 10: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

10

public TrafficLight(String direction) { this.direction = direction; bank = new ColoredLight[3];

bank[BOTTOM] = new ColoredLight(ColoredLight.GREEN); bank[BOTTOM].off(); bank[MIDDLE] = new ColoredLight(ColoredLight.YELLOW); bank[MIDDLE].off(); bank[TOP] = new ColoredLight(ColoredLight.RED); bank[TOP].off(); state = TOP; bank[TOP].on(); }

Initially, only the top light is on. Andthe Traffic light is RED.

This constructor allows the userto choose the direction of thetraffic light.The constructor establishes an array of three lights.

Each light is assigned a colorand a location on the traffic light.

Page 11: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

11

public void change() { bank[state].off(); state = (state + 1) % 3; bank[state].on(); }

public void display() { System.out.print(direction + " light "); System.out.print("["); for(int j = TOP; j >= BOTTOM; j--) { System.out.print("(" + bank[j].display() + ":" + bank[j].getBrightness() + ")"); } System.out.println("]"); }}

A call on the change methodwill cycle the light from RED toYELLOW to GREEN.

The display methoddisplays TrafficLightstatus.

Page 12: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

12

public class TrafficLightSimulator {

public static void main(String cmdLineArgs[]) throws Exception { TrafficLight northSouth; TrafficLight eastWest;

northSouth = new TrafficLight("North South"); eastWest = new TrafficLight("East West");

This class only has a static method.

Control two lights.

Page 13: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

13

// allow north south traffic northSouth.change();

while(true) {

// display light status eastWest.display(); northSouth.display(); Thread.sleep(10000); // halt north south and allow eastWest northSouth.change(); northSouth.change(); eastWest.change();

Page 14: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

14

// display light status eastWest.display(); northSouth.display(); Thread.sleep(10000); // halt east west and allow north south eastWest.change(); eastWest.change(); northSouth.change(); } }}

Page 15: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

15

GUI Example import java.awt.*;

class MyFrame extends Frame {

static int x = 0, y = 120; static int i = 0; static int horizScroll = 1; static int mess = 0; Font fb = new Font("TimesRoman", Font.BOLD, 36); String msg[] = { "Java", "Portable", "Secure", "Easy" }; Color color[] = { Color.blue, Color.yellow, Color.green, Color.red };

Page 16: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

16

public void paint(Graphics g) {

g.setFont(fb);

g.setColor( color[i]) ;

g.drawString(msg[i],x, y); }

Page 17: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

17

static public void main(String s[]) throws Exception { MyFrame mf = new MyFrame(); mf.setSize(200,200); int pixelsPerLine = 200, totalLines = 4; mf.setVisible(true);

for(int j = 0; j < pixelsPerLine * totalLines; j++) { Thread.sleep(25); mf.repaint(); if( horizScroll == 1) { if(( x += 3) < 200) continue; i = ++i % 4; x = 50; y = 0; horizScroll = 0; }

Page 18: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

18

else { if(( y += 3) < 200) continue; i = ++i % 4; x = 0; y = 120; horizScroll = 1; } } System.exit(0); }}

Page 19: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

19

Text moves left to right and then top to bottom.

Page 20: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

20

MyFrame.java

Two Steps:

javac MyFrame.java java MyFrame

Page 21: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

21

Another Example - Using Java’s BigInteger Class

import java.math.*;public class TestBigInts { public static void main(String args[] ) {

BigInteger x = new BigInteger("1234567890123" + "45678901234567890"); BigInteger y = new BigInteger("93939393929292" + "9191919191919192");

BigInteger z = x.multiply(y); System.out.println(z);

}}

Page 22: 95-712 Lecture 2: Object Oriented Programming 1 95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming

95-712 Lecture 2: Object Oriented Programming

22

Summary - Object-Oriented Programming

• Abstraction The focus is on “what” not “how”.• Encapsulation Information hiding is used to promote modularity and the use of abstractions.• Polymorphism We may want to treat an object not as an instance of its specific type but as an instance of its base type.• Inheritance OOP promotes code reuse via the “is-a” relationship.• Composition OOP promotes code reuse via the “has-a” relationship.