Upload
clemence-allison
View
218
Download
0
Embed Size (px)
Citation preview
Java ProgrammingThe Language of Now & the Future*
Lecture 0An Introduction to Java Syntax for Non-C
Programmers
John MorrisDepartment of Electrical and Electronic Engineering,
University of Western Australia
*The presenters of this course wish to disclaim responsibility this corny part of the title!
JohnMorris
PeterJones
Program
• Object Oriented Design• Objects and Classes• Inheritance• Interfaces
• Java Programs and Applets• Graphical User Interfaces• Swing• Windows, Frames, Menus, ...
• Practical Session
Week 2
Week 1
Week 3
Program
• Object Oriented Design• Objects and Classes• Inheritance• Interfaces
• Java Programs and Applets• Graphical User Interfaces• Swing• Windows, Frames, Menus, ...
• Practical Session
Week 5
Week 4
Week 6
Class Example - Java code
• Rectangle.java
class Rectangle { private double width, height; // attributes
public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}
double Height( ) { // projectorreturn height; // simply returns value} // of an attribute
double Width( ) {return width;}
double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates
double getPerimeter( ) {
return 2.0*(width + height);}
void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute
void setWidth( double w ) {width = w;}
}
Primitive Types
• Boolean • boolean• Values• true• false
• Not in C!
Primitive Types
• Character• char• Unicode• Uses two bytes• Designed for “internationalisation”• More later!
• Literals• Single quotes• ‘Ordinary’ (printing) characters
• ‘a’, ‘A’, ‘!’, ‘1’, ...
• Special• Hexadecimal form
• ‘\u0008’ (Unicode backspace)
Primitive Types
• Character• char• Literals• Non-printing characters
• Use \ as escape character
• ‘\b’ backspace• ‘\t’ tab• ‘\n’ linefeed• ‘\r’ return• ‘\”’ double quote• ‘\’’ single quote• ‘\\’ backslash itself!
Primitive Types
• Integers• Many flavours (just like C )
• int 4 bytes• short 2 bytes• long 8 bytes• byte 1 bytes (not in C)
(Use in low level file handling
char is Unicode!) • Literals
• [ sign ] { decimal digits }: 1, +1, -1, 9999, ....• L suffix for long: 123456789122L• 0x prefix for hexadecimal: 0x234ab
Primitive Types
• Floating point• Two flavours (as C)
• float 4 bytes• double 8 bytes • Literals
• [+|-] { dec digit }.[ { dec digit } ] [e|E] [+|-] { dec digit } • 1. 1.0 1.E+6 1.2e-6• -1.3e-06 1.896
Primitive Types
• Arrays• Rather different from C!• int [] intArray;...intArray = new int[n];
• double [] dlist = new double[n];• Initialised arraysint[] primes = { 2, 3, 5, 7, 11 };
• Array attributes int n = primes.length;
• More later!
Primitive Types
• Constants• No preprocessor (as C)!
• Use final keyword
• static final int n = 100;int [] intArray;...intArray = new int[n];double [] dlist = new double[n];
Class Example - Java code
• Rectangle.java
• Block delimited by{ }
class Rectangle { private double width, height; // attributes
public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}
double Height( ) { // projectorreturn height; // simply returns value} // of an attribute
double Width( ) {return width;}
double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates
double getPerimeter( ) {
return 2.0*(width + height);}
void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute
void setWidth( double w ) {width = w;}
}
1 2
Declarations
• type_name variable_name_list ;• boolean ok, finished;• char tab, separator;• int k, j;• byte c, d;• float x, y;• double a, b;• May occur anywhere in a block
• C: must be at head of block
• Scope• Valid within the block
Class Example - Java code
• Rectangle.java
• Compilable programunit
• class• one or more classes
per file
• package• several classes• used for making libraries
class Rectangle { private double width, height; // attributes
public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}
double Height( ) { // projectorreturn height; // simply returns value} // of an attribute
double Width( ) {return width;}
double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates
double getPerimeter( ) {
return 2.0*(width + height);}
void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute
void setWidth( double w ) {width = w;}
}
Methods (functions)
• [ public | private ] return_type name ( parameter_list ) block
• public int count( double a ) { some_statements; }
• public - accessible outside the class
• private - accessible inside the class only
• Some other qualifiers (later!)• static, protected, synchronized
Methods (functions)
• [ public | private ] return_type name ( parameter_list ) block
• public int count( double a ) { some_statements; }
• return_type - any Java type or class
• void - method doesn’t return a value but may update an object
Methods (functions)
• [ public | private ] return_type name ( parameter_list ) block
• public int count( double a ) { some_statements; }
• name - any legal name• must begin with a character• letters, numbers, _
• Convention• setValue, raiseSalary, printTable• common, but not mandatory!
Methods (functions)
• [ public | private ] return_type name ( parameter_list ) block• public int count( double a ) { some_statements; }
• parameter_list - • comma separated list
• type name pairs• void setPoint( double from, double to );• void update( Graphics g );• void setNewValues( int[] a );
Methods (functions)
• [ public | private ] return_type name ( parameter_list ) block• public int count( double a ) { some_statements; }
• block - • delimited by { }
• contains sequences of declarations & statements
• ; is terminator• not separator (as some languages)
Statements
• Assignment• l_value = expression
• a = b;x = y = 0.0;z = f( x, a ) * g( y, b );
• Statements are expressions
Expressions
• Operators• = assignment operator+, -, *, / arithmetic operators% modulus (remainder)! complement<<, >> left, right shift&, |, ^ arithmetic and, or, xor<, <=, >, >= relational== relational eq&&, || boolean and, or++, -- increment, decrement?: if(x) a else b
Expressions
• Operators•<<, >> left, right shifta << 1 multiplies a by 2b >> 2 divide b by 4x << n x * 2n
y >> k y / 2k
Expressions
• Operators• &, |, ^ arithmetic and, or, xora & b bitwise and of a and ba | b bitwise orx ^ y x xor y
• Care• a && b• a & b
• Both legal, very different meaning!
Expressions
• Operators•== relational equalif ( a == b ) .. notif ( a = b ) assignment operator!
Expressions
• Operators• &&, || boolean and, orif ( (a == b) && (x != y) ) ... again notif ( (a=b) & (x=y) ) ..
arithmetic operators!
Expressions
• Operators• ++, -- increment, decrement
x++ use x, then increment y-- use y, then decrement --x decrement x, then use it ++y increment y, then use it
• Postfix and prefix operators are quite different!
Expressions
• Operators•?: if(x) a else b
(a==b)?x:y if( a== b) x else y(a>b)?a:b max of a and b
Statements
• IF•if( exp ) statement1;else statement2;
if (a>b) x = a; else x = b;
else is optional if ( x[i] > max ) max = x[i];
Statements
• Loops• whilewhile( exp ) statement1;while( exp ) { statements; }
while (a>b) a = x[i++]; while ( x < 0 ) { x = getX( ... ); y = y + x; }
• while is zero-trip
Statements
• Loops• dodo statement; while( exp ); do { statements; } while( exp );
do a = x[i++]; while( a>z );• do { x = getX( ... ); y = y + x; } while ( x > 0 );
• do is one-trip
Statements - Loops
• forfor( exp1; exp2; exp3 ) { s; }
• equivalent to: exp1; while ( exp2 ) { s; exp3; }
• for( k=0; k<n; k++ ) { s; }
k=0;while( k<n ) { s; k++; }
• Standard pattern for n iterations!
Statements - switch
• switch( exp1 ) { case x1: s1; break; case x2: s2; break; default: s3; }
• switch( x ) { case 1: y = a; break; case 2: y = b; break; default: y = c; }
Strings
• Standard class• Package: java.lang
• Literals• “abcd”• “123”
Objects for primitive data types
• Wrappers• Make objects from standard data types• java.lang
•Boolean•Integer•Long•Character•Float•Double
Class Example - Java code
• Rectangle.java
class Rectangle { private double width, height; // attributes
public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}
double Height( ) { // projectorreturn height; // simply returns value} // of an attribute
double Width( ) {return width;}
double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates
double getPerimeter( ) {
return 2.0*(width + height);}
void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute
void setWidth( double w ) {width = w;}
}
class Rectangle {
private double width, height; // attributes
....... }
Class name
Class Example - Java code
• Rectangle.java
class Rectangle { private double width, height; // attributes
public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}
double Height( ) { // projectorreturn height; // simply returns value} // of an attribute
double Width( ) {return width;}
double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates
double getPerimeter( ) {
return 2.0*(width + height);}
void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute
void setWidth( double w ) {width = w;}
}
class Rectangle {
private double width, height; // attributes
....... }
Class name
Block delimiters
Class Example - Java code
• Rectangle.java
class Rectangle { private double width, height; // attributes
public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}
double Height( ) { // projectorreturn height; // simply returns value} // of an attribute
double Width( ) {return width;}
double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates
double getPerimeter( ) {
return 2.0*(width + height);}
void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute
void setWidth( double w ) {width = w;}
}
class Rectangle {
private double width, height; // attributes
....... }
Class name
AttributesBlock delimiters
Class Example - Java code
• Rectangle.java
class Rectangle { private double width, height; // attributes
public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}
double Height( ) { // projectorreturn height; // simply returns value} // of an attribute
double Width( ) {return width;}
double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates
double getPerimeter( ) {
return 2.0*(width + height);}
void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute
void setWidth( double w ) {width = w;}
}
class Rectangle {
private double width, height; // attributes
....... }
Class name
Attributes
Note that in all our examples,we mark the attributes private!
In good designs, classes areblack boxes!
Class Example - Java code
• Rectangle.java
class Rectangle { private double width, height; // attributes
public Rectangle( double w, double h ) { // constructorwidth = w; // set attributes fromheight = h; // parameters}
double Height( ) { // projectorreturn height; // simply returns value} // of an attribute
double Width( ) {return width;}
double getArea( ) { // projectorreturn width*height; // returns the value of an attribute} // which it calculates
double getPerimeter( ) {
return 2.0*(width + height);}
void setHeight( double h ) { // update methodheight = h; // changes the value of an} // attribute
void setWidth( double w ) {width = w;}
}
class Rectangle {
private double width, height; // attributes
....... }
Class name
Attributes
Note that in all our examples,we mark the attributes private!
In good designs, classes areblack boxes!