how to think about everything an engineering and scientific viewan engineering and scientific view...
TRANSCRIPT
How To Think About How To Think About EverythingEverything
• An engineering and scientific viewAn engineering and scientific view
• Hint: think “conceptual framework”Hint: think “conceptual framework”
• Hint: think “system”Hint: think “system”
Systems ThinkingSystems Thinking
• SystemSystem is: is:• any part of anything that you want to think any part of anything that you want to think
about as an indivisible unitabout as an indivisible unit
• InterfaceInterface is: is:• a description of the “boundary” between a a description of the “boundary” between a
system and everything else, that also explains system and everything else, that also explains how to think about that system as a unithow to think about that system as a unit
• Subsystem/componentSubsystem/component is: is:• a system that is used inside, i.e., as a part of, a system that is used inside, i.e., as a part of,
another system (a relative notion!)another system (a relative notion!)
Example: Ice/Water DispenserExample: Ice/Water Dispenser
Select water, crushed ice, or cubed ice. Select water, crushed ice, or cubed ice. Place a glass against the pad and push.Place a glass against the pad and push.
Water Crushed Cubed
People's Roles wrt SystemsPeople's Roles wrt Systems
• ClientClient is: is:• a person (or a “role” played by some agent) a person (or a “role” played by some agent)
viewing a system “from the outside” as an viewing a system “from the outside” as an indivisible unitindivisible unit
• ImplementerImplementer is: is:• a person (or a “role” played by some agent) a person (or a “role” played by some agent)
viewing a system “from the inside” as an viewing a system “from the inside” as an assembly of subsystems/componentsassembly of subsystems/components
Interfaces: Describing BehaviorInterfaces: Describing Behavior
• Information hidingInformation hiding is: is:• a technique where you intentionally leave out a technique where you intentionally leave out
information that is merely an “internal information that is merely an “internal implementation detail” of the systemimplementation detail” of the system
• AbstractionAbstraction is: is:• a complementary technique where you create a complementary technique where you create
a valid “cover story” to mask the effects of a valid “cover story” to mask the effects of hiding (presumably important) information hiding (presumably important) information about “internal implementation details”about “internal implementation details”
Mapping “Systems” to JavaMapping “Systems” to Java• ““Top-level system”, i.e., main program:Top-level system”, i.e., main program:
public classpublic class HelloWorld HelloWorld{{
public static voidpublic static void main ( main (StringString args[]) args[]){{
System.out.println ("Hello, System.out.println ("Hello, world!");world!");
}}}}
• Note: “System.out” in this code is really a Note: “System.out” in this code is really a subsystem; don't let the name confuse you!subsystem; don't let the name confuse you!
visiblevisible to a client a Java “system”
a class methodclass methodreturns no value
(a.k.a. a “procedure”)Java typetype for text
• ““Top-level system”, i.e., main program:Top-level system”, i.e., main program:public classpublic class HelloWorld HelloWorld{{
public static voidpublic static void main ( main (StringString args[])args[])
{{IHelloWorld hw = IHelloWorld hw = newnew
HelloWorld_1 ();HelloWorld_1 ();hw.outputHelloWorld ();hw.outputHelloWorld ();
}}}}
Another Example: Demo01Another Example: Demo01declarationdeclaration of theobject/instanceobject/instance hw
creationcreation of hw byinstantiatinginstantiating HelloWorld_1
(calling its constructorconstructor)
method callmethod call of outputHelloWorldon receiver objectreceiver object hw
Mapping “Interfaces” to JavaMapping “Interfaces” to Java
• Interface for subsystem used in this code:Interface for subsystem used in this code:public public interfaceinterface IIHelloWorldHelloWorld{{
public voidpublic void outputHelloWorld (); outputHelloWorld ();/*/*
ensuresensures["Hello, world!" appears ["Hello, world!" appears
onon System.out]System.out]
*/*/}}
a Java “interface”
an instance methodinstance method
behavioral specificationbehavioral specificationof the method, a.k.a.
its contractcontract
Implementation of InterfaceImplementation of Interfacepublic classpublic class HelloWorld HelloWorld_1_1
implementsimplements IHelloWorld IHelloWorld{{
public voidpublic void outputHelloWorld () outputHelloWorld (){{
System.out.println ("Hello, System.out.println ("Hello, world!");world!");
}}}}
one of the componentrelationshipsrelationships of Java
UML Class DiagramUML Class Diagram
• UMLUML = “Unified Modeling Language” = “Unified Modeling Language”
• Class diagramClass diagram shows relationships shows relationships between interfaces and classesbetween interfaces and classes
<<interface>> IHelloWorld
HelloWorld_1
HelloWorld
interfaceinterface component
classclass component
implementsimplementsrelationship
usesuses relationship
Enhanced Class DiagramEnhanced Class Diagram
• Class diagram can contain more info:Class diagram can contain more info:
no abstract stateabstract state
HelloWorld
+ main (String[])
<<interface>> IHelloWorld
+ outputHelloWorld ( )
HelloWorld_1
+ outputHelloWorld ( )one public method(“++” means public)
Running a Java ProgramRunning a Java Program
• The class The class HelloWorldHelloWorld must be in a file must be in a file called HelloWorld.java (similarly for an called HelloWorld.java (similarly for an interface, e.g., IHelloWorld.java)interface, e.g., IHelloWorld.java)
• To compile the class with the main To compile the class with the main program program andand all components it depends on: all components it depends on:
javac HelloWorld.javajavac HelloWorld.java
• To run the program (“execute” it):To run the program (“execute” it):java HelloWorldjava HelloWorld
What Happens If...What Happens If...
• You change the interface You change the interface IHelloWorldIHelloWorld so: so:• the name of the interface is the name of the interface is IHWIHW??• the method is called the method is called printHelloWorldprintHelloWorld??• the specification says the output is “Hello, the specification says the output is “Hello,
George!”?George!”?
What Happens If...What Happens If...
• You change the classYou change the class HelloWorld_1HelloWorld_1 so: so:• it doesn’t claim it implements it doesn’t claim it implements IHelloWorldIHelloWorld? ? • the method is called the method is called printHelloWorldprintHelloWorld??• the output string is "Hello, Mr. Bush!"?the output string is "Hello, Mr. Bush!"?
What Happens If...What Happens If...
• You change the classYou change the class HelloWorldHelloWorld so: so:• mainmain has no arguments? has no arguments?• the parameter to the parameter to mainmain is called is called theArgstheArgs??• the type of the type of hwhw is is HelloWorld_1HelloWorld_1??• the constructor after the constructor after newnew is is IHelloWorldIHelloWorld ( )? ( )?
Another Example: Demo02Another Example: Demo02
Echo
+ main (String[])
<<interface>> IEchoer
+ userInput ( ): String
Echoer_1
+ userInput ( ): String
return typereturn type ofmethod
Code for Code for Echoer_1Echoer_1importimport java.io.BufferedReader; java.io.BufferedReader;
importimport java.io.InputStreamReader; java.io.InputStreamReader;
importimport java.io.IOException; java.io.IOException;
public classpublic class Echoer_1 Echoer_1implementsimplements IEchoer IEchoer
{{......
}}
interfaces or classes from anypackagespackages that this unit depends on,
so they can be used withoutqualified namesqualified names
Code for Code for Echoer_1Echoer_1 (cont'd) (cont'd)publicpublic StringString userInput () userInput ()
throwsthrows IOException IOException
{{
System.out.println ("What do System.out.println ("What do you say?");you say?");
BufferedReader input = BufferedReader input = newnew
BufferedReader (BufferedReader (
newnew InputStreamReader InputStreamReader (System.in));(System.in));
returnreturn input.readLine (); input.readLine ();
}}
method call might result in anexceptional conditionexceptional condition, which
client is responsible for handlinghandling
idiomidiom for doing simple input,from standard inputstandard input
returningreturning a valuefrom a method
What Happens If...What Happens If...
• You change the interface You change the interface IEchoerIEchoer so: so:• it doesn’t import it doesn’t import java.io.IOExceptionjava.io.IOException??• the method the method userInputuserInput does not say that it does not say that it
throws IOExceptionthrows IOException??
What Happens If...What Happens If...
• You change the classYou change the class Echoer_1Echoer_1 so: so:• it doesn’t import it doesn’t import java.io.BufferedReaderjava.io.BufferedReader??• it doesn’t have a it doesn’t have a returnreturn statement in the statement in the
method body?method body?
Another Example: Demo03Another Example: Demo03
publicabstractabstract statestate
((valuevalue))
Echo
+ main (String[])
<<interface>> IRecorder+ string of character+ acceptUserInput ( )+ getRememberedString ( ): String
Recorder_1– remembered: String+ acceptUserInput ( )+ getRememberedString ( ): String
private, or hidden,representationrepresentation of
abstract state, a.k.a.instance fieldsinstance fields ordata membersdata members
Javadoc CommentsJavadoc Comments
• Java has a standard way of writing Java has a standard way of writing comments, and a tool called comments, and a tool called javadocjavadoc that that processes them into easily browsable user processes them into easily browsable user documentationdocumentation
• See the See the IRecorderIRecorder interface for an example of interface for an example of how to write how to write javadocjavadoc comments comments
• Visit …/demo03/docs/index.html to check what Visit …/demo03/docs/index.html to check what javadocjavadoc produces from them produces from them
Code for Code for Recorder_1Recorder_1public classpublic class Recorder_1 Recorder_1
implementsimplements IRecorder IRecorder{{
private Stringprivate String remembered = ""; remembered = "";......public Stringpublic String getRememberedString () getRememberedString (){{
returnreturn thisthis.remembered.remembered;;}}
}}
data representationdata representation ishiddenhidden from client
thisthis is the name used herefor the receiverreceiver of the call to
getRememberedString
explicit initializationexplicit initialization ofan instance field
Interlude: Complex Numbers Interlude: Complex Numbers TutorialTutorial
5
arctan(3/4)
(4,3) or 4 + 3i
real
imaginary
4
3
a complex number hasrealreal and imaginaryimaginary parts(rectangular coordinatesrectangular coordinates)
ii stands for √–1
vector from origin to numberhas magnitude magnitude and angleangle
(polar coordinatespolar coordinates)
Complex ArithmeticComplex Arithmetic
• Examples (you just have to work them out, Examples (you just have to work them out, knowing what knowing what ii means): means):
• (a+b(a+bii)+(c+d)+(c+dii) = (a+c)+(b+d)) = (a+c)+(b+d)ii• (a+b(a+bii)–(c+d)–(c+dii) =) =• (a+b(a+bii)*(c+d)*(c+dii) = (ac–bd)+(ac+bd)) = (ac–bd)+(ac+bd)ii• (a+b(a+bii)/(c+d)/(c+dii) =) =
this one is pretty easy!
this one easier withpolar than with
rectangular!
<<interface>> IComplex+ complex+ set (re: double, im: double)+ getRe ( ): double+ getIm ( ): double+ clear ( )+ toString ( ): String+ equals (x: Object): boolean+ clone ( ): Object
Another Example: complexAnother Example: complex
<<interface>> IComplexWithASMD
+ add (z: IComplexWithASMD)+ subtract (z: IComplexWithASMD)+ multiply (z: IComplexWithASMD)+ divide (z: IComplexWithASMD)
extendsextendsrelationship
Complex_2
Complex_1 ComplexWithASMD_1
layered implementationlayered implementation:mentions a particular
class (Complex_1), but reallydepends only on its interface!
multiple inheritancemultiple inheritance:related to more than one
other interface/class
Code for Code for IComplexIComplexpackage package complex;complex;public interfacepublic interface IComplex IComplex
extendsextends Cloneable Cloneable{{
......public Stringpublic String toString (); toString ();public booleanpublic boolean equals ( equals (ObjectObject x); x);public Objectpublic Object clone (); clone ();
}}
this interface belongs to thebelongs to thepackagepackage named complex
these methods are inheritedinherited fromthe built-in class ObjectObject (whether
or not you include them here)
extending this interface saysthat the cloneclone method will
be made public below;a curious Java idiom!
<<interface>>INatural
Another Example: naturalAnother Example: natural
Natural_1
<<interface>>INaturalEnhanced
…+ decrement ( )
NaturalEnhanced_1
…+ decrement ( )
<<interface>>INaturalEnhancedWithPR
+ power (int p)+ root (int r)
NaturalEnhancedWithPR_1
+ power (int p)+ root (int r)
first layer of additionalfunctionality
second layer of additionalfunctionality
Summary of Component Summary of Component Relationships: “implements”Relationships: “implements”
• implementsimplements• class-to-interfaceclass-to-interface• class class CC implements interface implements interface II if: if:
• CC defines a representation for values of type defines a representation for values of type II• CC provides implementations for all methods provides implementations for all methods
defined in defined in II (i.e., for each method in (i.e., for each method in II, , CC provides provides code that does what the spec in code that does what the spec in II says it should says it should do)do)
Summary of Component Summary of Component Relationships: “extends”Relationships: “extends”
• extendsextends• interface-to-interfaceinterface-to-interface
• interface interface IBIB extends interface extends interface IAIA if if IBIB provides provides everything that everything that IAIA does, plus it defines zero or does, plus it defines zero or more new methodsmore new methods
• class-to-classclass-to-class• classclass D D extends class extends class CC if D provides everything if D provides everything
that C does, plus it implements zero or more new that C does, plus it implements zero or more new methodsmethods
Summary of Component Summary of Component Relationships: “uses”Relationships: “uses”
• usesuses• interface-to-interfaceinterface-to-interface
• interface interface IBIB uses interface uses interface IAIA if if IBIB mentions mentions IAIA (extends is a special case of uses)(extends is a special case of uses)
• class-to-classclass-to-class• class class DD uses class uses class CC if if DD mentions mentions CC (extends is a (extends is a
special case of uses)special case of uses)• class-to-interfaceclass-to-interface
• class class CC uses interface uses interface II if if CC mentions mentions II (implements is a special case of uses)(implements is a special case of uses)