UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

26
UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 4 Lecture 4 HW#1: Electronic Submission of Part 2 HW#1: Electronic Submission of Part 2 Jini Overview (continued) Jini Overview (continued) C++ Diagnostic Results C++ Diagnostic Results Java Fundamentals Java Fundamentals Wed. 9/13/00 Wed. 9/13/00

description

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000. Lecture 4 HW#1: Electronic Submission of Part 2 Jini Overview (continued) C++ Diagnostic Results Java Fundamentals Wed. 9/13/00. Electronic Submission of Homework #1, Part 2. - PowerPoint PPT Presentation

Transcript of UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Page 1: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

UMass Lowell Computer Science 91.460

Java and Distributed Computing

Prof. Karen Daniels Fall, 2000

UMass Lowell Computer Science 91.460

Java and Distributed Computing

Prof. Karen Daniels Fall, 2000

Lecture 4Lecture 4HW#1: Electronic Submission of Part 2HW#1: Electronic Submission of Part 2

Jini Overview (continued)Jini Overview (continued)C++ Diagnostic ResultsC++ Diagnostic Results

Java FundamentalsJava FundamentalsWed. 9/13/00Wed. 9/13/00

Page 2: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Electronic Submission of Homework #1, Part 2

Electronic Submission of Homework #1, Part 2

Mon, 9/18 Mon, 9/18 “Hello World” programming“Hello World” programming (application and applet)(application and applet)

Homework is due at the start of lecture on the due date.Homework is due at the start of lecture on the due date.

NoteNote: If you want to use Swing on HW#1 in UML CS UNIX : If you want to use Swing on HW#1 in UML CS UNIX environment, use the Java version located in: /usr/opt/java122/binenvironment, use the Java version located in: /usr/opt/java122/bin

• /usr/opt/java122/bin/javac/usr/opt/java122/bin/javac

• /usr/opt/java122/bin/java/usr/opt/java122/bin/java

• Entire class will transition to this Java version starting with HW#2.Entire class will transition to this Java version starting with HW#2.

Page 3: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Electronic Submission of Homework #1, Part 2

Electronic Submission of Homework #1, Part 2

Mon, 9/18 Mon, 9/18 “Hello World” programming“Hello World” programming (application and applet)(application and applet)

Homework is due at the start of lecture on the due date.Homework is due at the start of lecture on the due date.

To submit your files electronically, use the UNIX To submit your files electronically, use the UNIX submitsubmit commandcommand

• Example for 3 files: Example for 3 files:

• submit kdaniels hw1 filename.java filename.class filename.htmlsubmit kdaniels hw1 filename.java filename.class filename.html

• You’ll submit 5 files for HW#1, part 2: 2 for application, 3 for appletYou’ll submit 5 files for HW#1, part 2: 2 for application, 3 for applet

• Submission deadline is 9:30 a.m. on Monday, 18 SeptemberSubmission deadline is 9:30 a.m. on Monday, 18 September

• I have now set it up to allow multiple submissions before the deadlineI have now set it up to allow multiple submissions before the deadline

• Bring a paper printout of your code to give to me at start of Monday’s Bring a paper printout of your code to give to me at start of Monday’s lecturelecture

Page 4: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Jini Overview (continued)Jini Overview (continued)

Page 5: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Requirements for Service, ClientRequirements for Service, Client

Either you Either you or a delegateor a delegate must be able to:must be able to: Connect to a TCP/IP Connect to a TCP/IP

network ( )network ( ) Participate in discovery Participate in discovery

to find a Lookup Serviceto find a Lookup Service Register with a Lookup Register with a Lookup

Service to provide proxyService to provide proxy Keep leases currentKeep leases current

Participate in discovery to Participate in discovery to find a Lookup Servicefind a Lookup Service

Communicate with a Communicate with a Lookup Service to receive Lookup Service to receive service proxyservice proxy

To be a Jini Service:To be a Jini Service: To be a Jini Client:To be a Jini Client:

Extra “machinery” needed:Extra “machinery” needed:- http server(s) for each machine to offer up downloadable code- http server(s) for each machine to offer up downloadable code- RMI activation daemon- RMI activation daemon

Page 6: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Hello World in a Jini Setting...Hello World in a Jini Setting...

c:c:

policy.allpolicy.allpolicypolicy

corejinicorejini chapter5chapter5

HelloWorldServiceInterface.javaHelloWorldServiceInterface.javaHelloWorldService.javaHelloWorldService.javaHelloWorldClient.javaHelloWorldClient.java

clientclient

HelloWorldServiceInterface.classHelloWorldServiceInterface.classHelloWorldClient$Listener.classHelloWorldClient$Listener.classHelloWorldClient.classHelloWorldClient.classcorejinicorejini chapter5chapter5

HelloWorldServiceInterface.classHelloWorldServiceInterface.classHelloWorldServiceProxy.classHelloWorldServiceProxy.classHelloWorldService$Listener.classHelloWorldService$Listener.classHelloWorldService.classHelloWorldService.class

corejinicorejini chapter5chapter5serviceservice

service-dlservice-dl HelloWorldServiceProxy.classHelloWorldServiceProxy.classcorejinicorejini chapter5chapter5

Page 7: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Who Else is in the Game?

(other providers of coordination frameworks/connection technologies/service architectures for distributed computing)

Who Else is in the Game?

(other providers of coordination frameworks/connection technologies/service architectures for distributed computing)

HAViHAVi Sony, Sharp, Toshiba, Philips, RCA, Matsushita, Hitachi, GrundigSony, Sharp, Toshiba, Philips, RCA, Matsushita, Hitachi, Grundig

Microsoft & HPMicrosoft & HP ChaiAppliance Plug and PlayChaiAppliance Plug and Play eSpeakeSpeak Universal Plug and PlayUniversal Plug and Play

Network protocol levelNetwork protocol level XML device descriptionXML device description

Salutation FrameworkSalutation Framework

Page 8: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Further Information – JiniFurther Information – Jini

Sun web sitesSun web sites http://www.javasoft.com/jinihttp://www.javasoft.com/jini http://www.jini.orghttp://www.jini.org

Development – BooksDevelopment – Books ““Core Jini” by W. Keith EdwardsCore Jini” by W. Keith Edwards ““The Jini Specification” by Arnold, O’Sullivan, Scheifler, The Jini Specification” by Arnold, O’Sullivan, Scheifler,

Waldo, and WollrathWaldo, and Wollrath

Gartner Advisory: Gartner Advisory: Java Jini: A ‘Killer-App Enabler’ for Network Computing? Java Jini: A ‘Killer-App Enabler’ for Network Computing?

http://gartner.jmu.edu/research/ras/83000/83044/83044.htmlhttp://gartner.jmu.edu/research/ras/83000/83044/83044.html

portions of this slide contributed by Ayal Spitz

Page 9: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

C++ Diagnostic ResultsC++ Diagnostic Results

Number of StudentsNumber of Students17171616151514141313121211111010998877665544332211

Question NumberQuestion Number1 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 9

Number Number

of of

Correct Correct

ResponsesResponses

Most students ran out of time…Most students ran out of time…

Page 10: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Java FundamentalsJava Fundamentals

1: Java fundamentals1: Java fundamentals applets, applications, data types, control structures, applets, applications, data types, control structures,

OO programming, filesOO programming, files

2: Advanced Java concepts & Java support for 2: Advanced Java concepts & Java support for distributed computingdistributed computing GUI/events, graphics, exceptions, advanced data GUI/events, graphics, exceptions, advanced data

structures, threads/ multithreading, RMIstructures, threads/ multithreading, RMI

3: Distributed computing using a sample Java-3: Distributed computing using a sample Java-based environment: Jinibased environment: Jini

Page 11: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Syllabus for Part 1 of the CourseSyllabus for Part 1 of the Course

Lecture Date Topic ReadingWed 9/6,Fri 9/8

Introduction/Overview Deitel: Chapter 1

Mon 9/11 Jini Overview Edwards: Chapters 1, 2Java Fundamentals Deitel

Wed 9/13 Java Overview/C++ Comparison Chapters 2, 3Fri 9/15 Java Applets & Applications Chapters 2, 3

Mon 9/18,Wed 9/20

Data types, Operators, Expressions From chapter:- 4: 4.11, 4.12, 4.13- 5: 5.9Chapters 7, 10

Fri 9/22 Control Structures Rest of chapters 4, 5Mon 9/25 Exception Handling Chapter 14Wed 9/27 Methods, Object-Based Programming Chapters 6, 8Fri 9/29,Mon 10/2

Object-Oriented Programming Chapter 9

Wed 10/4 Files & Streams Chapter 17Fri 10/6 Exam 1 Closed book; Worth 15% of gradeClosed book; Worth 15% of grade

Page 12: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Why Java & Distributed Computing?Why Java & Distributed Computing?

Platform independence helps with Platform independence helps with heterogeneous hardware, software challengeheterogeneous hardware, software challenge

Built-in support for:Built-in support for: Multithreading: “concurrent” multiple activitiesMultithreading: “concurrent” multiple activities Client-Server computing: servlets, JDBCClient-Server computing: servlets, JDBC Remote Method Invocation (RMI): method calls Remote Method Invocation (RMI): method calls

across networkacross network Networking: socket-based communication views Networking: socket-based communication views

networking like file I/Onetworking like file I/OFor insight into the original Java vision, see the original Java For insight into the original Java vision, see the original Java whitepaper by James Gosling at: whitepaper by James Gosling at: http://java.sun.com/people/jag/OriginalJavaWhitepaper.pdfhttp://java.sun.com/people/jag/OriginalJavaWhitepaper.pdf

Page 13: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

C to C++ to Java: At a GlanceC to C++ to Java: At a GlanceCC

Primitive Data TypesPrimitive Data TypesArrays Arrays Struct, Union, EnumStruct, Union, EnumPreprocessor DirectivesPreprocessor DirectivesOperatorsOperatorsExpressionsExpressionsControl StatementsControl StatementsFunctionsFunctionsPointersPointersDynamic Memory MgtDynamic Memory MgtFile I/OFile I/OException Handling Exception Handling

JavaJavaC++C++Primitive Data TypesPrimitive Data TypesArrays Arrays StructStruct, Union, Enum, Union, EnumPreprocessor DirectivesPreprocessor DirectivesOperatorsOperatorsExpressionsExpressionsControl StatementsControl StatementsFunctionsFunctionsPointersPointersDynamic Memory MgtDynamic Memory MgtFile I/OFile I/OException HandlingException HandlingClassesClassesMultiple InheritanceMultiple InheritancePolymorphismPolymorphismTemplatesTemplates uu uu

Primitive Data TypesPrimitive Data TypesArrays Arrays OperatorsOperators

ExpressionsExpressionsControl StatementsControl Statements Dynamic Memory MgtDynamic Memory MgtFile I/OFile I/OException HandlingException HandlingClasses, InterfacesClasses, Interfaces InheritanceInheritancePolymorphismPolymorphismSupport for DBs, networks, Support for DBs, networks,

GUIs, events, graphics, GUIs, events, graphics, threads, librariesthreads, libraries

Procedural, Compiled Procedural, Compiled LanguageLanguage

Procedural/OO, Procedural/OO, Compiled LanguageCompiled Language OO, Interpreted LanguageOO, Interpreted Language

Page 14: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Introduction to Java ApplicationsIntroduction to Java Applications

Java application is executed using the Java Java application is executed using the Java interpreter (but not inside browser like an applet)interpreter (but not inside browser like an applet)

Application begins execution in main( )Application begins execution in main( ) main( ) is main( ) is staticstatic method inside class method inside class no convenient input from command lineno convenient input from command line System.exit(0); // required to clean up System.exit(0); // required to clean up // after GUI/graphics// after GUI/graphics

// Save this in file Welcome1.java// Save this in file Welcome1.java

public class Welcome1{public class Welcome1{ public static void main( String args[] ) public static void main( String args[] ) {{ System.out.println( "Java is fun!" );System.out.println( "Java is fun!" ); }}}}

javacjavac Welcome1.java Welcome1.java

javajava Welcome1 Welcome1

Welcome1.classWelcome1.class

Welcome1.javaWelcome1.java

Page 15: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

javacjavac Welcome1Applet.java Welcome1Applet.java

appletviewerappletviewer Welcome1Applet.html Welcome1Applet.html

Introduction to Java AppletsIntroduction to Java Applets

Java applet is executed inside a WWW browser (or Java applet is executed inside a WWW browser (or appletviewer) using a Java interpreter that is inside the browser.appletviewer) using a Java interpreter that is inside the browser.

Welcome1Applet.classWelcome1Applet.class

Welcome1Applet.javaWelcome1Applet.java

Welcome1Applet.htmlWelcome1Applet.html

// Save this file in Welcome1Applet.java// Save this file in Welcome1Applet.javaimport java.applet.*;import java.applet.*;import java.awt.*;import java.awt.*;

public class Welcome1Applet extends Applet{public class Welcome1Applet extends Applet{ public void paint(Graphics g) public void paint(Graphics g) {{ g.drawString( "Java is fun!”, 25, 50 );g.drawString( "Java is fun!”, 25, 50 ); }}}}

<HTML><HTML><APPLET code=“Welcome1Applet.class” width=150 height=100><APPLET code=“Welcome1Applet.class” width=150 height=100></APPLET></APPLET></HTML></HTML>

Save this file in Welcome1Applet.htmlSave this file in Welcome1Applet.html

Page 16: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Introduction to Java Applets (continued)

Introduction to Java Applets (continued)

Order of (a subset of) applet method Order of (a subset of) applet method invocations:invocations: init( )init( ) start( )start( ) paint( )paint( )

Page 17: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Java VariablesJava Variables

Two types:Two types: primitive data type variable (a.k.a. variable)primitive data type variable (a.k.a. variable)

always passed by valuealways passed by value initialized by default to initialized by default to 0 0 if numeric; if numeric; falsefalse if if booleanboolean

except for local variablesexcept for local variables value is constant if value is constant if finalfinal keyword is used keyword is used

reference variable (a.k.a. reference)reference variable (a.k.a. reference) refers to memory location of objectrefers to memory location of object always passed by referencealways passed by reference initialized by default to initialized by default to nullnull

Page 18: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Java Primitive Data TypesJava Primitive Data Types

Type Size in Bits Value Range ValueDefault

Standard

boolean 8 true or false falsechar 16 ‘\u0000’ to ‘\uFFFF’ ‘\u0000’ ISO Unicode character

set (see Appendix D)byte 8 -27 to + 27- 1

(-128 to +127)0

short 16 -215 to + 215- 1 0int 32 -231 to + 231- 1 0long 64 -263 to + 263- 1 0float 32 -3.40292347E+38 to

+3.40292347E+380.0 IEEE 754 floating

pointdouble 64 -1.79769313486231570E+308

to+1.79769313486231570E+308

0.0 IEEE 754 floatingpoint

Page 19: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Java Array ClassJava Array Class

Declarations:Declarations: One-dimensional:One-dimensional:

int c [ ]; c = new int [8];int c [ ]; c = new int [8]; int c [ ] = new int [8];int c [ ] = new int [8]; final int ARRAY_SIZE = 8; int c [ ] = new int [ARRAY_SIZE];final int ARRAY_SIZE = 8; int c [ ] = new int [ARRAY_SIZE]; int c [ ] = { 32, 44, 6, 7, -1, 25, 88, -31 };int c [ ] = { 32, 44, 6, 7, -1, 25, 88, -31 };

Multi-dimensional (e.g. 2):Multi-dimensional (e.g. 2): int c[ ][ ] = { {32, 44, 6, 7 } , {-1, 25, 88, -31} };int c[ ][ ] = { {32, 44, 6, 7 } , {-1, 25, 88, -31} }; int c[ ][ ] = new int[2][4];int c[ ][ ] = new int[2][4]; int c[ ][ ] = new int[2][ ]; // allocate rows for non-rectangular arrayint c[ ][ ] = new int[2][ ]; // allocate rows for non-rectangular array

c[0] = new int[3]; // allocate columns for row 0c[0] = new int[3]; // allocate columns for row 0 c[1] = new int[4]; // allocate columns for row 1c[1] = new int[4]; // allocate columns for row 1

Knows its own length! (e.g. c.length)Knows its own length! (e.g. c.length) Bounds are checked for you!Bounds are checked for you!

Page 20: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Java String Class (basics)Java String Class (basics)

String is a series of characters treated as a single unitString is a series of characters treated as a single unit Declarations and Constructors:Declarations and Constructors:

String s; // empty string for now -- its length is 0String s; // empty string for now -- its length is 0 s = new String(); // String() is null constructor. It yields an empty s = new String(); // String() is null constructor. It yields an empty

// string for now whose length is 0 // string for now whose length is 0 s = new String(“hello”); // initializes s to “hello”s = new String(“hello”); // initializes s to “hello” String s = “hello”; String s = “hello”; // initializes s to “hello” // initializes s to “hello” s2 = new String (s1); // copy constructors2 = new String (s1); // copy constructor String s = “hello” + “ world”; // String concatenationString s = “hello” + “ world”; // String concatenation s1.equals (s2); // tests equality of contentss1.equals (s2); // tests equality of contents s1 = = s2 // tests if both refer to same object in memorys1 = = s2 // tests if both refer to same object in memory

Knows its own length! (e.g. s.length)Knows its own length! (e.g. s.length) Array of Strings: Array of Strings: String Pets[ ] = {“dog”, “cat”, “fish”};String Pets[ ] = {“dog”, “cat”, “fish”};

Page 21: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Some Java Operators [Deitel, p. 271; complete list is in Appendix C]Some Java Operators [Deitel, p. 271; complete list is in Appendix C]

PrecedencePrecedence: : Operator higher up the chart is evaluated before operator lower down the chartOperator higher up the chart is evaluated before operator lower down the chart AssociativityAssociativity: : Order of evaluation for operators of equal precedenceOrder of evaluation for operators of equal precedence

Operators Associativity Type( ) [ ] . left-to-right highest++ -- right-to-left unary postfix++ -- + - ! (type) right-to-left unary* / % left-to-right multiplicative+ - left-to-right additive< <= > >= left-to-right relational== != left-to-right equality& left-to-right boolean logical AND^ left-to-right boolean logical exclusive OR| left-to-right boolean logical inclusive OR&& left-to-right logical AND|| left-to-right logical OR?: right-to-left conditional= += -= *= /= %= right-to-left assignment

Page 22: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

ExpressionsExpressions

Sequence of operators and operands that Sequence of operators and operands that specifies a computationspecifies a computation May result in a value (e.g., 123, true)May result in a value (e.g., 123, true) May cause side-effects (e.g., change a value)May cause side-effects (e.g., change a value) Compound expressions (e.g., (a*b)+c))Compound expressions (e.g., (a*b)+c)) Includes literals (numbers or strings)Includes literals (numbers or strings)

Page 23: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Java Expression BNFJava Expression BNF expression ::= numeric_expression | testing_expression | logical_expression | string_expression | bit_expression | casting_expression | creating_expression | literal_expression | "null" | "super" | "this" | identifier | ( "(" expression ")" ) | ( expression ( ( "(" [ arglist ] ")" ) | ( "[" expression "]" ) | ( "." expression ) | ( "," expression ) | ( "instanceof" ( class_name | interface_name ) )

) )

From http://cuiwww.unige.ch/db-research/Enseignement/analyseinfo/BNFweb.html

Page 24: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

StatementsStatements

Smallest “executable” unitSmallest “executable” unit Declaration statementsDeclaration statements Control statementsControl statements Assignment statementsAssignment statements Method invocationMethod invocation Compound statement (block)Compound statement (block)

Semicolon-separated list of statementsSemicolon-separated list of statements Enclosed in “curly” brackets { }Enclosed in “curly” brackets { } Deitel calls it a ‘block’ only if it has declarations of Deitel calls it a ‘block’ only if it has declarations of

its ownits own

Page 25: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Java Abstract Windowing Toolkit GUI Components (from java.awt package)

Java Abstract Windowing Toolkit GUI Components (from java.awt package)

java.awt.Graphics java.awt.Graphics (see list on p. 530-531)(see list on p. 530-531) Given an object g of Graphics class:Given an object g of Graphics class:

g.setColor( Color.red ); g.setColor( Color.red ); // Sets current drawing color to red// Sets current drawing color to red

g.drawString(“hello”, 200, 25); g.drawString(“hello”, 200, 25); // Draws String starting at (200,25)// Draws String starting at (200,25)

g.drawLine(20, 28, 40, 10 ); g.drawLine(20, 28, 40, 10 ); // Draws line from (20,28) to (40,10)// Draws line from (20,28) to (40,10)

g.fillRect(100, 5, 20, 15); g.fillRect(100, 5, 20, 15); // Draws filled rectangle whose upper left// Draws filled rectangle whose upper left

// corner is at (100, 5). Width = 20. Height = 15// corner is at (100, 5). Width = 20. Height = 15

g.drawOval(60, 9, 20, 13); g.drawOval(60, 9, 20, 13); // Draws oval whose bounding box upper left // Draws oval whose bounding box upper left

// corner is at (60, 9). Width = 20. Height = 13// corner is at (60, 9). Width = 20. Height = 13(0,0)(0,0) xx

yy hellohello

usesusescurrentcurrentcolorcolor

Page 26: UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

Java Swing GUI Components(from javax.swing package)Java Swing GUI Components(from javax.swing package)

javax.swing.JOptionPanejavax.swing.JOptionPane Dialog boxDialog box

messagemessage errorerror informationinformation warningwarning questionquestion plainplain

inputinput

javax.swing.JTextAreajavax.swing.JTextArea javax.swing.JScrollPanejavax.swing.JScrollPane