1
|
- Drie maal twee uur uitleg en voorbeelden
|
2
|
- 1994: James Gosling - SUN Microsystems
- Designed to program intelligent appliances
- Launched to send programs over the Web
- Object oriented programming style
- Allows multi-threaded execution
- Syntax borrowed from C and C++
- “Cleaner” semantics than C and C++
- Many object libraries widely available
|
3
|
- 1972 : Brian Kernighan & Dennis Richie
- Need to rewrite the UNIX operating system for computer with different assembly
language.
- Other future rewrites likely
- Design of a new High (?) Level Language
- Minimizing coding work
- Giving access to all machine details
- Block structured for detail hiding
- Easy to compile
|
4
|
- 1987 : Maintenance and debugging of "quick & dirty" C
programs is a nightmare !
- Bell Labs design new, object oriented, language, adopting syntax of C
for facilitating its adoption by "hard core" programmers.
- Presently, the most used language in professional software development,
when reliability is not of prime importance.
|
5
|
- Douglas Bell, Mike Parr
- Java for students (third edition)
- Pearson Education, 1998, 2002
- ISBN 0 130 32377 2
|
6
|
- Rogers Cadenhead
- Teach yourself Java 1.1 programming in 24 hours
- Sams.net Publishing, 1997
- ISBN : 1-57521-270-6
- H.M. Deitel, P.J. Deitel
- Java - How to program
- Prentice Hall, 1997
- ISBN : 0-13-263401-5
|
7
|
- Mark Grand
- Java Language Reference
- ISBN : 1-56592-204-2
- Scott Oaks, Henry Wong
- Java Threads
- ISBN : 1-56592-216-6
- Elliotte Rusty Harold
- Java Network Programming
- ISBN : 1-56592-227-1
|
8
|
- (* Sample program to write "Hello World" *)
- MODULE HelloWorld ;
- FROM IO IMPORT WrStr; WrLn;
- (* No data declarations in this module *)
- BEGIN
- WrStr("Hello World !"); WrLn;
- END HelloWorld.
|
9
|
- /* Sample program to write "Hello World" */
- class HelloWorld {
- // No data declarations in this class
- public static void main(String[] arguments) {
- System.out.println("Hello World !");
- }
- }
|
10
|
- /* Sample program to write "Hello World" */
- class HelloWorld {
- // No data declarations in this class
- public static void main(String[] arguments) {
- System.out.println("Hello World !");
- }
- }
|
11
|
- /* Sample program to write "Hello World" */
- class HelloWorld {
- // No data declarations in
this class
- public static void main(String[] arguments) {
- System.out.println("Hello World !");
- }
- }
|
12
|
- Fundamental idea : Abstract Data Type
- What is an ADT ?
- Data declarations
- Procedures acting upon the data
- Only the procedures can access the data.
- Why ADTs ?
- When data formats have to change, only procedures in ADT need to be
adapted, no changes outside the ADT.
- Implementation of ADTs ?
- In modula 2, library modules are ADTs.
|
13
|
|
14
|
- /* Sample program to write "Hello World" */
- class HelloWorld {
- // No data declarations in this class
- public static void main(String[] arguments) {
- System.out.println("Hello World !");
- }
- }
|
15
|
- Java program = set of classes
- class =
- Data declarations
- Methods (= procedures)
- class = template to create similar objects
- extension of the notion of type:
- Set of values the variable can take
- implicit list of operations defined on that set
- Type of variables in object
- explicit definition of operations defined on variables
- initialization of these variables
|
16
|
- /* Sample program to write "Hello World" */
- class HelloWorld {
- // No data declarations in this class
- public static void main(String[] arguments) {
- System.out.println("Hello World !");
- }
- }
|
17
|
- [protection][linkage][resulttype] name ([parameter declarations]) {[declarations]instructions}
|
18
|
- [protection][linkage][resulttype] name]) ([parameter declarations]) {[declarations]instructions}
- protection :
- public : method can be called from anywhere
- linkage :
- static : method does not apply to a specific object instantiation
- resulttype :
- void : no value is returned by this method
- (in Java, C and C++ all procedures are functions ! )
- name :
- main : this method will always be executed first
|
19
|
|
20
|
|
21
|
- Mixing of types in expressions is generally allowed
- Comparison operators : <, <=, >, >=, ==, !=
- Arithmetic operators : +, -, *, /, %
- Increment, decrement operators : ++, --
- Compound operators : +=, -=, *=, /=, ...
- Boolean operators : && (AND) , || (OR) , ! (NOT)
- Bitwise logical operators : & (AND) , | (OR) , ^ (XOR)
- ...
|
22
|
- In Modula :
- a := a + 1; b := b - 1;
- c := c + 5; d := d / 2;
- In Java :
- a++; b --;
- c += 5; d /= 2;
|
23
|
- In Modula, all variables have to be declared
- In Java, all variables have to be declared
- Variables can be initialized when declared.
- Examples:
- int a = 3;
- int b = 8 * 5;
- float pi = 3.0;
- char key = 'c';
- String Title = "Data declarations";
- boolean EndOfList = true;
|
24
|
- In Modula, named constants can be declared
- In Java, a variable can be initialized to its final value and can not be
modified further in the program.
- Example:
- final float pi = 3.1415926;
- final String Author = "J.Tiberghien";
|
25
|
- MODULA integer types : Cardinal and Integer
- Modula integer operators : relational, +, -,
*, DIV, MOD
- Java integer types :
- byte : -128 .. +127; ( 8 bit )
- short : -32768 .. +32767; ( 16 bit )
- int : -2147483648 .. +2147483647; ( 32 bit )
- long : - 2 63 .. + 2 63 - 1; ( 64 bit )
|
26
|
- Java integer operators :
- + , - , * : same as in Modula.
- / : same as DIV in Modula when both factors integer.
- % : same as MOD in Modula.
- ++ , -- : increment and decrement unary operators.
- Bitwise logical operators
- Comparison operators
|
27
|
- MODULA REAL type,
- Modula real operators : relational , +,
-, *, /
- Java real types :
- float : 1.4 10-45 .. 3.4 10+38 32 bit, IEEE
754.
- double : 4.9 10-324 .. 1.8 10+308 64 bit, IEEE
754.
- Java real operators :
- +, -, *, / : same as in Modula
- ++ , -- : increment and decrement unary operators.
- Comparison operators
|
28
|
- MODULA BOOLEAN type: is an ordinal type
- ORD(FALSE) = 0 ; ORD(TRUE) = 1
- Modula Boolean operators:
- NOT, AND, OR
- = #
> < >=
>= IN
- Java boolean type: can not be cast into integer type
- Java Boolean operators
|
29
|
- In MODULA:
- CHAR type,
- string: ARRAY[0..20] OF CHAR
- In Java:
- char:
- uses the 16 bit Unicode,
- is considered as an always positive integer type
- all operations allowed on integers are allowed
- String :
- is a specific class to represent strings.
- a String is not an ARRAY OF CHAR.
- String's can be concatenated by the + operator
|
30
|
- In MODULA:
- ARRAY
- A : ARRAY[1..3,-5..+5] OF REAL;
- Matrix : ARRAY[‘A’..’J’,1..10] OF REAL;
- RECORD, SET
- In Java: Only arrays, Records are
made by classes
- Array Declarations:
- int [] a; or int a[]
- int [][] a2;
- int [][][] a3;
- Arrays are created by the new operator.
- Dimensions are defined when array is created
|
31
|
|
32
|
- class NewRoot { // to compute the square root of the first argument
- public static void main
(String[] arguments) {
- if
(arguments.length > 0)
- { int number = 0;
-
number = Integer.parseInt(arguments[0]);
System.out.println("The Square root of "
- +
number
- +
" is "
- +
Math.sqrt(number));
- }
- else System.out.println("use: 'java
NewRoot [argument]'");
- }
- }
|
33
|
- class NewRoot { // to compute the square root of the first argument
- public static void main (String[]
arguments) {
- if
(arguments.length > 0)
- { int number = 0;
-
number = Integer.parseInt(arguments[0]);
-
System.out.println("The Square root of "
- +
number
- +
" is "
- +
Math.sqrt(number));
- }
- else System.out.println("use: 'java
NewRoot [argument]'");
- }
- }
|
34
|
- class NewRoot { // to compute the square root of the first argument
- public static void main
(String[] arguments) {
- if
(arguments.length > 0)
- { int number = 0;
-
number = Integer.parseInt(arguments[0]);
-
System.out.println("The Square root of "
- +
number
- +
" is "
- +
Math.sqrt(number));
- }
- else System.out.println("use: 'java
NewRoot [argument]'");
- }
- }
|
35
|
- class NewRoot { // to compute the square root of the first argument
- public static void main
(String[] arguments) {
- if (arguments.length > 0)
- { int number = 0;
-
number = Integer.parseInt(arguments[0]);
-
System.out.println("The Square root of "
- +
number
- +
" is "
- +
Math.sqrt(number));
- }
- else System.out.println("use: 'java
NewRoot [argument]'");
- }
- }
|
36
|
- class NewRoot { // to compute the square root of the first argument
- public static void main
(String[] arguments) {
- if
(arguments.length > 0)
- { int number = 0;
-
number = Integer.parseInt(arguments[0]);
-
System.out.println("The Square root of "
- +
number
- +
" is "
- +
Math.sqrt(number));
- }
- else System.out.println("use: 'java
NewRoot [argument]'");
- }
- }
|
37
|
- class NewRoot { // to compute the square root of the first argument
- public static void main
(String[] arguments) {
- if
(arguments.length > 0)
- { int number = 0;
- number
= Integer.parseInt(arguments[0]);
-
System.out.println("The Square root of "
- +
number
- +
" is "
- +
Math.sqrt(number));
- }
- else System.out.println("use: 'java
NewRoot [argument]'");
- }
- }
|
38
|
- class NewRoot { // to compute the square root of the first argument
- public static void main
(String[] arguments) {
- if
(arguments.length > 0)
- { int number = 0;
-
number = Integer.parseInt(arguments[0]);
-
System.out.println("The Square root of "
- +
number
- +
" is "
- +
Math.sqrt(number));
- }
- else System.out.println("use: 'java
NewRoot [argument]'");
- }
- }
|
39
|
- class NewRoot { // to compute the square root of the first argument
- public static void main
(String[] arguments) {
- if
(arguments.length > 0)
- { int number = 0;
-
number = Integer.parseInt(arguments[0]);
-
System.out.println("The Square root of "
- +
number
- +
" is "
- +
Math.sqrt(number));
- }
- else System.out.println("use: 'java
NewRoot [argument]'");
- }
- }
|
40
|
- While statement ~ Modula 2 WHILE loop
- while (Lives>0) { /* play game */ }
- Do statement ~ Modula 2 REPEAT loop
- do { /* play game */ } while (Lives>0)
- For statement ~ Modula 2 FOR loop
- for (int number =0; number < 100; number ++) {
- if ( number % 3 == 0 )
- System.out.println ("# " + number);
- }
|
41
|
- In Modula 2 : EXIT
- The Java break statement
- while (Lives>0) {
- /* play game */
- if (Cookiemonster.strikes) break;
- /* play game */
- }
|
42
|
- No specific instruction in Modula 2
- The Java continue statement
- for ( int Year = 1600; Year < 2600; Year += 4 ) {
- if (( Year % 100 == 0 ) && ( Year % 400 != 0 ))
- continue;
- System.out.printl ( "The year " + Year
- + " is a
leapyear");
- }
|
43
|
- class Strings {
- public static void main
(String[] arguments) {
- int length =
arguments.length;
- if (length > 0) {
-
System.out.println(" you entered following words :");
- int max = 0;
- for(int l =
0; l < length; l++) {
-
System.out.println(arguments[l].toUpperCase());
-
if(arguments[l].length() > max) max = arguments[l].length();
-
if(arguments[l].equals("stop")) break;
- }
-
System.out.println("Longest word : "+ max + "
characters ");
- }
- else System.out.println("use: 'java
Strings [string]'");
- }
- }
|
44
|
- class Strings {
- public static void main
(String[] arguments) {
- int length =
arguments.length;
- if (length > 0) {
-
System.out.println(" you entered following words :");
- int max = 0;
- for(int l =
0; l < length; l++) {
-
System.out.println(arguments[l].toUpperCase());
-
if(arguments[l].length() > max) max = arguments[l].length();
-
if(arguments[l].equals("stop")) break;
- }
-
System.out.println("Longest word : "+ max + "
characters ");
- }
- else System.out.println("use: 'java
Strings [string]'");
- }
- }
|
45
|
- class Strings {
- public static void main
(String[] arguments) {
- int length =
arguments.length;
- if (length > 0) {
-
System.out.println(" you entered following words :");
- int max = 0;
- for(int l =
0; l < length; l++) {
- System.out.println(arguments[l].toUpperCase());
-
if(arguments[l].length() > max) max = arguments[l].length();
-
if(arguments[l].equals("stop")) break;
- }
-
System.out.println("Longest word : "+ max + "
characters ");
- }
- else System.out.println("use: 'java
Strings [string]'");
- }
- }
|
46
|
- class Strings {
- public static void main
(String[] arguments) {
- int length =
arguments.length;
- if (length > 0) {
-
System.out.println(" you entered following words :");
- int max = 0;
- for(int l =
0; l < length; l++) {
-
System.out.println(arguments[l].toUpperCase());
- if(arguments[l].length()
> max) max = arguments[l].length();
-
if(arguments[l].equals("stop")) break;
- }
-
System.out.println("Longest word : "+ max + "
characters ");
- }
- else System.out.println("use: 'java
Strings [string]'");
- }
- }
|
47
|
- class Strings {
- public static void main
(String[] arguments) {
- int length =
arguments.length;
- if (length > 0) {
-
System.out.println(" you entered following words :");
- int max = 0;
- for(int l =
0; l < length; l++) {
-
System.out.println(arguments[l].toUpperCase());
-
if(arguments[l].length() > max) max = arguments[l].length();
- if(arguments[l].equals("stop"))
break;
- }
-
System.out.println("Longest word : "+ max + "
characters ");
- }
- else System.out.println("use: 'java
Strings [string]'");
- }
- }
|
48
|
- class Strings {
- public static void main
(String[] arguments) {
- int length =
arguments.length;
- if (length > 0) {
-
System.out.println(" you entered following words :");
- int max = 0;
- for(int l =
0; l < length; l++) {
-
System.out.println(arguments[l].toUpperCase());
-
if(arguments[l].length() > max) max = arguments[l].length();
- if(arguments[l].equals("stop"))
break;
- }
-
System.out.println("Longest word : "+ max + "
characters ");
- }
- else System.out.println("use: 'java
Strings [string]'");
- }
- }
|
49
|
- Similar to the Modula 2 CASE statement, but with fundamentally different
semantics
- switch(selector) {
- case value1 : statement1;
- case value2 : statement2;
- ...
- default : defaultstatement;
- }
|
50
|
- To make it equivalent to the Modula 2 CASE statement, break statements
are required
- switch(selector) {
- case value1 : {statement1;break}
- case value2 : {statement2;break}
- ...
- default : defaultstatement;
- }
|
51
|
- One object = One module
- Objects have
- Attributes
- thinghs that describe the object
- things that show how it is different from others
- The variables
- Behaviour
- what an object does
- The PROCEDURES
- Objects are created at compile time
- Objects tend to be large, complex and few
|
52
|
- Derived from a class (=template for a certain class of objects)
- Objects have
- Attributes
- thinghs that describe the object
- things that show how it is different from others
- The variables
- Behaviour
- what an object does
- The methods
- Objects are created by the new constructor
- Objects tend to be small, simple and many
|
53
|
- Modula 2
- Static variables
- All ordinal types
- Reals
- Arrays
- Records
- Sets
- Procedures
- Pointers
- Dynamic variables
- Defined by TYPE definition
- Created by NEW
- Accessed via pointers
- Deleted by DISPOSE
- Java
- Static Variables
- byte, short, int, long
- float, double
- boolean
- char
- Built-in types
- Dynamic objects
- Defined by Class definition
- Created by new
- Accessed via methods
- Deleted by garbage collector
|
54
|
- Public class Dog {
- String Color; //This is an attribute
- public void speak() { //This is behaviour
- System.out.println(" Wouw!,Wouw! ");
- }
- public void sleep() { //This is more behaviour
- System.out.println(" zzzzzzzzzzzzzzzzz");
- }
- }
- // Constructing two Dogs :
- Dog Bobby = new Dog();
- Dog Pluto = new Dog();
|
55
|
- Public class Cat {
- String Color; //This is an attribute
- public void speak() { //This is behavoiur
- System.out.println(" Miauw!,Miauw! ");
- }
- public void sleep() { //This is more behaviour
- System.out.println(" zzzzzzzzzzzzzzzzz");
- }
- }
- // Constructing two Cats :
- Cat Tom = new Cat();
- Cat Garfield = new Cat();
|
56
|
- Public class Pet {
- String Color; //This is a common attribute
- public void sleep() { //This is a common behaviour
- System.out.println(" zzzzzzzzzzzzzzzzz");
- }
- }
- Public class Dog extends Pet{
- public void speak() { //This is a Dog's specific behaviour
- System.out.println(" Wouw!,Wouw! ");
- }
- }
- Public class Cat extends Pet{
- public void speak() { //This is a Cat's specific behaviour
- System.out.println(" Miauw!,Miauw! ");
- }
- }
|
57
|
- Garfield.Color = "Brown tiger";
- Tom.Color = "Grey";
- Bobby.Color = "White";
- Pluto.Color = "Orange";
- …
- Bobby.sleep(); Tom.speak(); Pluto.speak(); Garfield.sleep();
- zzzzzzzzzzzzzzzzz
- Miauw!,Miauw!
- Wouw!,Wouw!
- zzzzzzzzzzzzzzzzz
|
58
|
- Avoids redundant work when creating related classes
- Subclass: Cat and Dog are subclasses of the class Pet
- Superclass: Pet is the superclass of classes Cat and Dog
- The whole Java language is a tree of classes
- Multiple inheritance is NOT allowed in Java
|
59
|
- public class Die{
- public int value;
- // constructor of class Die
- public Die(){ value = 0; }
- // executed when an object of
class Die is constructed
- public void RollDie() {
- double tempvalue = Math.random() * 6;
- value = (int) Math.floor(tempvalue) + 1;
- }
- }
|
60
|
- MODULE Die;
- FROM Lib IMPORT RANDOM;
- EXPORT Value, RollDie;
- VAR value : CARDINAL;
- PROCEDURE RollDie;
- BEGIN
- Value = RANDOM(5)+1
- END RollDie;
- BEGIN
- Value := 0
- END Die;
|
61
|
- MODULE Die1;
- FROM Lib IMPORT RANDOM;
- EXPORT Value1, RollDie;
- VAR value1 : CARDINAL;
- PROCEDURE RollDie1;
- BEGIN
- Value1 = RANDOM(5)+1
- END RollDie1;
- BEGIN
- Value1 := 0
- END Die1;
|
62
|
- MODULE Die2;
- FROM Lib IMPORT RANDOM;
- EXPORT Value2, RollDie;
- VAR value2 : CARDINAL;
- PROCEDURE RollDie2;
- BEGIN
- Value2 = RANDOM(5)+1
- END RollDie2;
- BEGIN
- Value2 := 0
- END Die2;
|
63
|
- class TestDie {
- public static void main (String[] arguments) {
- Die Die1 = new Die();
- Die Die2 = new Die();
-
System.out.println("Initially, first die: " + Die1.Value +", Second die:
" + Die2.Value);
- Die1.RollDie();
- Die2.RollDie();
- System.out.println("After rolling, first die: " + Die1.Value +", Second die:
" + Die2.Value);
- }
- }
|
64
|
|
65
|
|
66
|
|
67
|
|
68
|
|
69
|
|
70
|
|
71
|
|
72
|
|
73
|
|