Presentation is loading. Please wait.

Presentation is loading. Please wait.

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 5 Java Fundamentals Fri. 9/15/00.

Similar presentations


Presentation on theme: "UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 5 Java Fundamentals Fri. 9/15/00."— Presentation transcript:

1 UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 5 Java Fundamentals Fri. 9/15/00

2 Homework #1, Part 1 Due today at start of lecture.

3 Homework #1, Part 2 Mon, 9/18 “Hello World” programming (application and applet) Homework is due at the start of lecture on the due date. Note: If you want to use Swing on HW#1 in UML CS UNIX environment, 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. This Java version is on the saturn CS machine.This Java version is on the saturn CS machine. If you are using a workstation (not just an X-terminal), remotely log into saturn so you can access the Java version on saturn. Otherwise, you will default to the Java version that is on that workstation. If you are using a workstation (not just an X-terminal), remotely log into saturn so you can access the Java version on saturn. Otherwise, you will default to the Java version that is on that workstation. Note: When testing an applet in the UML CS UNIX environment, be sure to set the UNIX DISPLAY environment variable appropriately.

4 Homework #1, Part 2 (continued) Homework is due at the start of lecture on the due date. To submit your files electronically, use the UNIX submit command Example for 3 files: Example for 3 files: submit kdaniels hw1 filename.java filename.class filename.html submit kdaniels hw1 filename.java filename.class filename.html You’ll submit 5 files for HW#1, part 2: 2 for application, 3 for applet You’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 September Submission deadline is 9:30 a.m. on Monday, 18 September I have now set it up to allow multiple submissions before the deadline I 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 lecture Bring a paper printout of your code to give to me at start of Monday’s lecture

5 Java Fundamentals ä 1: Java fundamentals ä applets, applications, data types, control structures, OO programming, files ä 2: Advanced Java concepts & Java support for distributed computing ä GUI/events, graphics, exceptions, advanced data structures, threads/ multithreading, RMI ä 3: Distributed computing using a sample Java- based environment: Jini

6 Syllabus for Part 1 of the Course Closed book; Worth 15% of grade

7 Why Java & Distributed Computing? ä Platform independence helps with heterogeneous hardware, software challenge ä Built-in support for: ä Multithreading: “concurrent” multiple activities ä Client-Server computing: servlets, JDBC ä Remote Method Invocation (RMI): method calls across network ä Networking: socket-based communication views networking like file I/O For insight into the original Java vision, see the original Java whitepaper by James Gosling at: http://java.sun.com/people/jag/OriginalJavaWhitepaper.pdf

8 Introduction to Java Applications ä Java application is executed using the Java interpreter (but not inside browser like an applet) ä Application begins execution in main( ) ä main( ) is static method inside class ä no convenient input from command line ä System.exit(0); // required to clean up // after GUI/graphics // after GUI/graphics // Save this in file Welcome1.java 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!" ); }} javac Welcome1.java java Welcome1 Welcome1.class Welcome1.java

9 javac Welcome1Applet.java appletviewer Welcome1Applet.html Introduction to Java Applets ä Java applet is executed inside a WWW browser (or appletviewer) using a Java interpreter that is inside the browser. Welcome1Applet.class Welcome1Applet.java Welcome1Applet.html // Save this file in Welcome1Applet.java import java.applet.*; import java.awt.*; 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> </APPLET></HTML> Save this file in Welcome1Applet.html

10 Introduction to Java Applets (continued) ä Order of (a subset of) applet method invocations: ä init( ) ä start( ) ä paint( )

11 C to C++ to Java: At a Glance C Primitive Data Types Arrays Struct, Union, Enum Preprocessor Directives OperatorsExpressions Control Statements FunctionsPointers Dynamic Memory Mgt File I/O Exception Handling JavaC++ Primitive Data Types Arrays Struct, Union, Enum Preprocessor Directives OperatorsExpressions Control Statements FunctionsPointers Dynamic Memory Mgt File I/O Exception Handling Classes Multiple Inheritance PolymorphismTemplates uu Primitive Data Types Arrays OperatorsExpressions Control Statements Dynamic Memory Mgt File I/O Exception Handling Classes, Interfaces Inheritance InheritancePolymorphism Support for DBs, networks, GUIs, events, graphics, threads, libraries Procedural, Compiled Language Procedural/OO, Compiled Language OO, Interpreted Language

12 Java Variables ä Two types: ä primitive data type variable (a.k.a. variable) ä always passed by value ä initialized by default to 0 if numeric; false if boolean ä except for local variables ä value is constant if final keyword is used ä reference variable (a.k.a. reference) ä refers to memory location of object ä always passed by reference ä initialized by default to null

13 Java Primitive Data Types

14 Java Array Class ä Declarations: ä One-dimensional: ä int c [ ]; c = new int [8]; ä int c [ ] = new int [8]; ä final int ARRAY_SIZE = 8; int c [ ] = new int [ARRAY_SIZE]; ä int c [ ] = { 32, 44, 6, 7, -1, 25, 88, -31 }; ä Multi-dimensional (e.g. 2): ä int c[ ][ ] = { {32, 44, 6, 7 }, {-1, 25, 88, -31} }; ä int c[ ][ ] = new int[2][4]; ä int c[ ][ ] = new int[2][ ]; // allocate rows for non-rectangular array ä c[0] = new int[3]; // allocate columns for row 0 ä c[1] = new int[4]; // allocate columns for row 1 ä Knows its own length! (e.g. c.length) ä Bounds are checked for you!

15 Java String Class (basics) ä String is a series of characters treated as a single unit ä Declarations and Constructors: ä String s; // empty string for now -- its length is 0 ä s = new String(); // String() is null constructor. It yields an empty // string for now whose length is 0 ä s = new String(“hello”); // initializes s to “hello” ä String s = “hello”; // initializes s to “hello” ä s2 = new String (s1); // copy constructor ä String s = “hello” + “ world”; // String concatenation ä s1.equals (s2); // tests equality of contents ä s1 = = s2 // tests if both refer to same object in memory ä Knows its own length! (e.g. s.length) ä Array of Strings: String Pets[ ] = {“dog”, “cat”, “fish”};

16 Some Java Operators [Deitel, p. 271; complete list is in Appendix C] Precedence: Operator higher up the chart is evaluated before operator lower down the chart Associativity: Order of evaluation for operators of equal precedence

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

18 Java 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

19 Statements ä Smallest “executable” unit ä Declaration statements ä Control statements ä Assignment statements ä Method invocation ä Compound statement (block) ä Semicolon-separated list of statements ä Enclosed in “curly” brackets { } ä Deitel calls it a ‘block’ only if it has declarations of its own

20 Java Abstract Windowing Toolkit GUI Components (from java.awt package) ä java.awt.Graphics (see list on p. 530-531) ä Given an object g of Graphics class: ä g.setColor( Color.red ); // Sets current drawing color to red ä g.drawString(“hello”, 200, 25); // Draws String starting at (200,25) ä g.drawLine(20, 28, 40, 10 ); // Draws line from (20,28) to (40,10) ä g.fillRect(100, 5, 20, 15); // 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); // 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)x y hello usescurrentcolor

21 Java Swing GUI Components (from javax.swing package) ä javax.swing.JOptionPane ä Dialog box ä message ä error ä information ä warning ä question ä plain ä input ä javax.swing.JTextArea ä javax.swing.JScrollPane

22 Homework 1 Fri, 9/8 Fri, 9/15 Part 1 Mon, 9/18 Part 2 Mon, 9/18 Part 2 2Fri, 9/15 Fri, 9/22 Part 1 & Part 2 HW# Assigned Due Content Homework is due at the start of lecture on the due date.


Download ppt "UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 5 Java Fundamentals Fri. 9/15/00."

Similar presentations


Ads by Google