220 likes | 364 Views
95-712 Object Oriented Programming Java. Lecture 2: Object Oriented Programming. 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.
E N D
95-712 Object Oriented Programming Java Lecture 2: Object Oriented Programming 95-712 Lecture 2: Object Oriented Programming
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. 95-712 Lecture 2: Object Oriented Programming
Grady Booch Says • Every object has state, behavior and identity. • Consider the Light class that follows. Identify the state, behavior and identity. 95-712 Lecture 2: Object Oriented Programming
The class has a name. Save in file Light.java. Instances of the class will have private data. When an object is created the object’s constructor will run. Other objects will be able to send five messages to objects of this class. 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; } } Private data is only available to members of the class. A class may have invariants - statements about internal object state that must always be true. So, how do we create an object?
A class that only has a main routine. Save it in TestLight.java. 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(); } } } Create a Light object. Its constructor will run. Create a second Light object and run its constructor. “Please turn both lights on and brighten the outside light.” If Light.java and TestLight.java are both in the same directory then javac TestLight.java and java TestLight will do it all.
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…
A ColoredLight is-a-kind-of Light Save this in ColoredLight.java. ColoredLight inherits all the features of Light. 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"; } } Every ColoredLight object will have a brightness and a color. Before this constructor runs the base class constructor is run. There is no way to change the color after it has been set.
A LowPowerLight is-a Light “Is-a” means the exact same interface. public class LowPowerLight extends Light { public LowPowerLight() { } public void brighten() { brightness = brightness + 5; if(brightness > 20) brightness = 20; } } For Eckel,” is-a-kind-of” means the derived class has additional methods. As is, this won’t work. The variable brightness, in the Light class, must be marked protected rather than private. 95-712 Lecture 2: Object Oriented Programming
The TrafficLight class uses composition to include three ColoredLights. 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; Each TrafficLight object has a current state (describing which of three lights is on) and faces a direction. The values TOP, MIDDLE and BOTTOM are constants associated with this class. 95-712 Lecture 2: Object Oriented Programming
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(); } This constructor allows the user to choose the direction of the traffic light. The constructor establishes an array of three lights. Each light is assigned a color and a location on the traffic light. Initially, only the top light is on. And the Traffic light is RED. 95-712 Lecture 2: Object Oriented Programming
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 method will cycle the light from RED to YELLOW to GREEN. The display method displays TrafficLight status. 95-712 Lecture 2: Object Oriented Programming
This class only has a static method. 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"); Control two lights. 95-712 Lecture 2: Object Oriented Programming
// 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(); 95-712 Lecture 2: Object Oriented Programming
// display light status eastWest.display(); northSouth.display(); Thread.sleep(10000); // halt east west and allow north south eastWest.change(); eastWest.change(); northSouth.change(); } } } 95-712 Lecture 2: Object Oriented Programming
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 }; 95-712 Lecture 2: Object Oriented Programming
public void paint(Graphics g) { g.setFont(fb); g.setColor( color[i]) ; g.drawString(msg[i],x, y); } 95-712 Lecture 2: Object Oriented Programming
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; } 95-712 Lecture 2: Object Oriented Programming
else { if(( y += 3) < 200) continue; i = ++i % 4; x = 0; y = 120; horizScroll = 1; } } System.exit(0); } } 95-712 Lecture 2: Object Oriented Programming
Text moves left to right and then top to bottom. 95-712 Lecture 2: Object Oriented Programming
MyFrame.java Two Steps: javac MyFrame.java java MyFrame 95-712 Lecture 2: Object Oriented Programming
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); } }
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. 95-712 Lecture 2: Object Oriented Programming