JDC_Lecture6 - Computer Science

Download Report

Transcript JDC_Lecture6 - Computer Science

UMass Lowell Computer Science 91.460
Java and Distributed Computing
Prof. Karen Daniels
Fall, 2000
Lecture 6
Java Fundamentals
Mon. 9/18/00
Homework #1, Part 2
Due at the start of lecture today:
• electronic code submission
• paper printout
Note: When testing an applet in the UML CS UNIX environment, be
sure to set the UNIX DISPLAY environment variable appropriately.
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/java
• Entire class will transition to this Java version starting with HW#2.
•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.
Homework #2
HW# Assigned Due
Content
1
Part 1
Part 2
Part 1 & Part 2
2
Fri, 9/8
Fri, 9/15
Mon, 9/18
Fri, 9/15 Fri, 9/22
Homework is due at the start of lecture on the due date.
C to C++ to Java: At a Glance
C
C++
Procedural, Compiled
Language
Primitive Data Types
Arrays
Struct, Union, Enum
Preprocessor Directives
Operators
Expressions
Control Statements
Functions
Pointers
Dynamic Memory Mgt
File I/O
Exception Handling
Classes
Multiple Inheritance
Polymorphism
Templates
Primitive Data Types
Arrays
Operators
u
u
Primitive Data Types
Arrays
Struct, Union, Enum
Preprocessor Directives
Operators
Expressions
Control Statements
Functions
Pointers
Dynamic Memory Mgt
File I/O
Exception Handling
Java
Procedural/OO,
Compiled Language
Expressions
Control Statements
Dynamic Memory Mgt
File I/O
Exception Handling
Classes, Interfaces
Single Inheritance
Polymorphism
Support for DBs, networks,
GUIs, events, graphics,
threads, libraries
OO, Interpreted Language
Java Variables

Two types:

primitive data type variable (a.k.a. variable)

always “passed-by-value” (i.e. “call-by-value”)


initialized by default to 0 if numeric; false if boolean



value of argument is copied
except for local variables
value is constant if final keyword is used
reference variable (a.k.a. reference)




refers to memory location of object
reference variable itself is always “passed-by-value”
method can use reference to manipulate object directly
initialized by default to null
Java Primitive Data Types
Type
Size in Bits
Value Range
boolean
char
8
16
true or false
‘\u0000’ to ‘\uFFFF’
byte
8
-27 to + 27- 1
Value
Default
false
Standard
‘\u0000’
ISO Unicode character
set (see Appendix D)
0
(-128 to +127)
short
int
long
float
16
32
64
32
double
64
-215 to + 215- 1
-231 to + 231- 1
-263 to + 263- 1
-3.40292347E+38 to
+3.40292347E+38
-1.79769313486231570E+308
to
+1.79769313486231570E+308
0
0
0
0.0
0.0
IEEE 754 floating
point
IEEE 754 floating
point
Java Array Class

Declarations:

One-dimensional:





Multi-dimensional (e.g. 2):





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 };
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!
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”};
Some Java Operators
[Deitel, p. 271; complete list is in Appendix C]
Operators
() [] .
++
-++ -- + !
*
/
%
+ < <= > >=
==
!=
&
^
|
&&
||
?:
=
+=
-=
*=
(type)
/=
%=
Associativity
left-to-right
right-to-left
right-to-left
left-to-right
left-to-right
left-to-right
left-to-right
left-to-right
left-to-right
left-to-right
left-to-right
left-to-right
right-to-left
right-to-left
Type
highest
unary postfix
unary
multiplicative
additive
relational
equality
boolean logical AND
boolean logical exclusive OR
boolean logical inclusive OR
logical AND
logical OR
conditional
assignment
Precedence: Operator higher up the chart is evaluated before operator lower down the chart
Associativity: Order of evaluation for operators of equal precedence
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)
Java Expression BNF
expression
::=
numeric_expression
| testing_expression
| logical_expression
| string_expression
| bit_expression
| casting_expression
From http://cuiwww.unige.ch/db-research/
| creating_expression
Enseignement/analyseinfo/BNFweb.html
| literal_expression
| "null"
| "super"
| "this"
| identifier
| ( "(" expression ")" )
| ( expression
( ( "(" [ arglist ] ")" )
| ( "[" expression "]" )
| ( "." expression )
| ( "," expression )
| ( "instanceof" ( class_name | interface_name ) )
))
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
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:


uses
current 
color

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

g.drawOval(60, 9, 20, 13); // Draws oval whose bounding box upper left
(0,0)
y
x
// corner is at (60, 9). Width = 20. Height = 13
hello
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