28
242-210 Programming Fundamentals 2: Interaction/4 242-210 F II Objectives introduce modularization and abstraction explain how an object uses other objects compare object and primitive types Semester 2, 2012-2013 4. Object Interaction Original Slides by Dr. Andrew Davison

242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

Embed Size (px)

Citation preview

Page 1: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 1

242-210 F II

• Objectives– introduce modularization and abstraction– explain how an object uses other objects– compare object and primitive types

Semester 2, 2012-2013

4. Object Interaction

Original Slides by Dr. Andrew Davison

Page 2: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 2

Topics

1. Modularization and Abstraction

2. A Digital Clock

3. Using ClockDisplay

4. A More Graphical Clock

Page 3: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 3

1. Modularization and Abstraction

• Modularization divides a problem into simpler sub-parts, which can be built separately, and which interact in simple ways.

• Abstraction is the ability to ignore low level details of a problem to focus on the higher levels.

Page 4: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 4

Use in Programming

• Use Use modularizationmodularization to split a programming to split a programming problem into sub-parts (modules).problem into sub-parts (modules).– implement the modulesimplement the modules

• The implementation of the complete The implementation of the complete program will be easier, since program will be easier, since abstractionabstraction can be used to write software in terms of the can be used to write software in terms of the modules. modules.

Page 5: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 5

e.g. Robot SoftwareModules("black boxes")

Abstraction"links" themodulestogetherusing theirvisibleinterfaces.

Page 6: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 6

Use in OOP

• Use modularization to split a programming Use modularization to split a programming problem into problem into objectsobjects..

• Implement the Implement the classesclasses for the objects. for the objects.

• The implementation of the class for the complete The implementation of the class for the complete program will be easier, since abstraction can be program will be easier, since abstraction can be used to write the class in terms of other classes used to write the class in terms of other classes (yours and predefined ones). (yours and predefined ones).

Page 7: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 7

e.g. Airport Control System

Classes for: Plane, Gate, Luggage,Passenger, etc.

Use them to create objectssuch as plane1, plane2,gate2, myLuggage

Abstraction simplifies thecommunication betweenthe objects; only use theirvisible interface.

Page 8: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 8

2. A Digital Clock

• Implement a digital clock display, which shows the hours (0-23) and minutes (0-59).

Page 9: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 9

Modularizing the Clock Display

• Divide the clock display problem into two Divide the clock display problem into two partsparts– how to display the hourshow to display the hours– how to display the minuteshow to display the minutes

• We need two number display objectsWe need two number display objects

• We need a NumberDisplay classWe need a NumberDisplay class

two NumberDisplayobjects

Page 10: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 10

Objects Diagram

ClockDisplay object

NumberDisplay object(for hours)

NumberDisplay object(for minutes)

Page 11: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 11

NumberDisplay Interface

• What kind of interface is needed for a What kind of interface is needed for a NumberDisplay class?NumberDisplay class?– get and set the numberget and set the number– return the number as a stringreturn the number as a string

• useful for printinguseful for printing

– increment the numberincrement the number• the number will 'loop' the number will 'loop' • e.g. 0, 1, 2, ..., 59, e.g. 0, 1, 2, ..., 59, 00, 1, ... for the minutes display, 1, ... for the minutes display

Page 12: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 12

The NumberDisplay Class

public class NumberDisplaypublic class NumberDisplay

{{

private int currValue;private int currValue;

private int maxValue; private int maxValue; // number at which currValue goes back to 0 // number at which currValue goes back to 0

public NumberDisplay(int max)public NumberDisplay(int max)

{ maxValue = max;{ maxValue = max;

currValue = 0;currValue = 0;

}}

continued

Page 13: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 13

public void setValue(int newValue)public void setValue(int newValue)

/* Set currValue to the new value. /* Set currValue to the new value. If the new value is less than If the new value is less than zero or over maxValue, don't set it. zero or over maxValue, don't set it.

*/*/

{ if ((newValue >= 0) && (newValue < maxValue)){ if ((newValue >= 0) && (newValue < maxValue))

currValue = newValue;currValue = newValue;

}}

public int getValue()public int getValue()

{ return currValue; }{ return currValue; }

continued

Page 14: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 14

public String getDisplayValue()public String getDisplayValue()

// return currValue as a string// return currValue as a string

{{

if (currValue < 10)if (currValue < 10)

return "0" + currValue; //pad string with leading 0return "0" + currValue; //pad string with leading 0

elseelse

return "" + currValue;return "" + currValue;

}}

public void increment()public void increment()

/* Increment currValue, rolling over to zero if the/* Increment currValue, rolling over to zero if the

maxValue is reached. */maxValue is reached. */

{ currValue = (currValue + 1) % maxValue; }{ currValue = (currValue + 1) % maxValue; }

} // end of NumberDisplay class} // end of NumberDisplay class

Page 15: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 15

ClockDisplay Interface

• What kind of interface is needed for a What kind of interface is needed for a ClockDisplay class?ClockDisplay class?– initialize the clock and set the timeinitialize the clock and set the time

– return the current time as a string return the current time as a string • useful for printinguseful for printing

– increment the time by one minuteincrement the time by one minute

• The time will be represented using two The time will be represented using two NumberDisplay fields.NumberDisplay fields.

Page 16: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 16

The ClockDisplay Classpublic class ClockDisplaypublic class ClockDisplay

{{

private NumberDisplay hours;private NumberDisplay hours;

private NumberDisplay minutes;private NumberDisplay minutes;

private String currTimeString; private String currTimeString; // the current time as a string // the current time as a string

public ClockDisplay()public ClockDisplay()

// intialize the clock to 00:00// intialize the clock to 00:00

{{

hours = new NumberDisplay(24);hours = new NumberDisplay(24);

minutes = new NumberDisplay(60);minutes = new NumberDisplay(60);

setTimeStringsetTimeString();();

}}

two privateNumberDisplayfields

create twoNumberDisplayobjects

continued

Page 17: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 17

privateprivate void setTimeString() void setTimeString()

/* store the current time as a string /* store the current time as a string of the form "hours:minutes" */ of the form "hours:minutes" */

{ { currTimeString = hours.getDisplayValue() + currTimeString = hours.getDisplayValue() + ":" + ":" + minutes.getDisplayValue(); minutes.getDisplayValue(); }}

a private method isone that only othermethods in the classcan call

continued

method callingin NumberDisplayobjects

Page 18: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 18

public void setTime(int hour, int minute)public void setTime(int hour, int minute)

// set time to the specified hour and minute// set time to the specified hour and minute

{{

hours.setValue(hour);hours.setValue(hour);

minutes.setValue(minute);minutes.setValue(minute);

setTimeStringsetTimeString();();

} // end of setTime()} // end of setTime()

public String getTime()public String getTime()

// return the current time as a string// return the current time as a string

{ return currTimeString; }{ return currTimeString; }

continued

method callingin NumberDisplayobjects

Page 19: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 19

public void minIncrement()public void minIncrement()

// increment the clock by one minute;// increment the clock by one minute;

// hour increments when minutes roll over to 0// hour increments when minutes roll over to 0

{{

minutes.increment();minutes.increment();

if (minutes.getValue() == 0) // mins rolledif (minutes.getValue() == 0) // mins rolled

hours.increment();hours.increment();

setTimeStringsetTimeString();();

} // end of minIncrement()} // end of minIncrement()

} // end of ClockDisplay class} // end of ClockDisplay class

Page 20: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 20

Classes Diagram

uses

Page 21: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 21

3. Using ClockDisplaypublic class ClockDemopublic class ClockDemo

{{

public static void main(String[] args)public static void main(String[] args)

{{

ClockDisplay clock = new ClockDisplay();ClockDisplay clock = new ClockDisplay();

clock.setTime(14, 10); // set time to 14:10clock.setTime(14, 10); // set time to 14:10

while(true) {while(true) {

clock.minIncrement();clock.minIncrement();

System.out.println(" tick...");System.out.println(" tick...");

System.out.println("Current time: "+clock.getTime());System.out.println("Current time: "+clock.getTime());

waitwait(100); // slow down the looping(100); // slow down the looping

}}

} // end of main()} // end of main()

Page 22: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 22

private private staticstatic void wait(int milliseconds) void wait(int milliseconds)

/* stop execution for milliseconds /* stop execution for milliseconds amount of time */ amount of time */

{{

try {try {

Thread.sleep(milliseconds);Thread.sleep(milliseconds);

} }

catch (Exception e) { }catch (Exception e) { }

} // end of wait()} // end of wait()

} // end of ClockDemo class} // end of ClockDemo class

sleep() is a methodin Java's Thread class

wait() is a static methodso it can be called by main()without main() having to create an object first.

Page 23: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 23

Compilation and Execution

$ javac *.java

Compile NumberDisplay.java, ClockDisplay.java, and ClockDemo.java

I typed ctrl-c tostop the looping.

Page 24: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 24

Objects Diagram for ClocksDemo

hours

minutes

currTimeString

clockClockDisplay object

currValue

maxValue

NumberDisplay object

24

14

currValue

maxValue

NumberDisplay object

60

19

14:19

String object

Page 25: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 25

4. A More Graphical Clock

• Michael Kölling and Bruce Quig have Michael Kölling and Bruce Quig have developed a simple Canvas class for developed a simple Canvas class for displaying text and basic shapes in a displaying text and basic shapes in a window.window.

• We can use Canvas to display the changing We can use Canvas to display the changing clock display instead of using stdout.clock display instead of using stdout.

Page 26: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 26

Canvas Class Diagram

Only showing thepublic methods(the interface).

To use Canvas, we onlyneed to understand itsinterface.

I don't care how it isimplemented.

Page 27: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 27

ClockCanvasDemopublic class ClockCanvasDemopublic class ClockCanvasDemo

{{

public static void main(String[] args)public static void main(String[] args)

{{

Canvas canvas = new Canvas("Clock Demo",300,150, Color.white);Canvas canvas = new Canvas("Clock Demo",300,150, Color.white);

canvascanvas.setVisible(true);.setVisible(true);

canvascanvas.setFont( new Font("Dialog", Font.PLAIN, 96));.setFont( new Font("Dialog", Font.PLAIN, 96));

ClockDisplay clock = new ClockDisplay();ClockDisplay clock = new ClockDisplay();

clock.setTime(14, 10); // set time to 14:10clock.setTime(14, 10); // set time to 14:10

while(true) {while(true) {

clock.minIncrement();clock.minIncrement();

canvascanvas.erase(); // clear the canvas.erase(); // clear the canvas

canvascanvas.drawString( clock.getTime(), 30, 100);.drawString( clock.getTime(), 30, 100);

canvascanvas.wait(100); // slow down the looping.wait(100); // slow down the looping

}}

} // end of main()} // end of main()

} // end of ClockCanvasDemo class} // end of ClockCanvasDemo class

Using Canvas(compare to slide 21)

Page 28: 242-210 Programming Fundamentals 2: Interaction/4 1 242-210 F II Objectives – –introduce modularization and abstraction – –explain how an object uses other

242-210 Programming Fundamentals 2: Interaction/4 28

Compilation and Execution

$ javac *.java$ javac *.java

$ java ClockCanvasDemo$ java ClockCanvasDemo

Compilation includes ClockCanvasDemo.java andCanvas.java