Download - Nelson Baloian Universidad de Chile
![Page 1: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/1.jpg)
Nelson Baloian
Universidad de Chile
![Page 2: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/2.jpg)
Goals of intensive lecture
• To learn:– What is JAVA ?– What is JAVA good for ?– What is JAVA bad for ?– Which programming resources offers JAVA to
support internetworking programming ?– How does JAVA programming looks like ?
![Page 3: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/3.jpg)
Why is JAVA so popular ?• Object oriented: the modern programming
paradigm • Robust: avoids programming features which can
frequently cause errors, permits recovering from errors.
• Aware of the Network: easy for programming in a TCP/IP network programming
• Muti-platform• It is free and has very good documentation
![Page 4: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/4.jpg)
What is Object Orientation ?• Data used by the program are objects
• Objects are a collection of variables and methods
Method that modifies the values of the object’s variables
Method that returns the value of the object’s variables
![Page 5: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/5.jpg)
Examples of Objects • A clock: the variables are the position of the
pointers
• A telephone book: the variables are a list of names and telephone numbers
Set the pointers’ value
What time is it ?
• Set a name with a telephone number• change the telephone number• delete a pair name-telephone
• Given a name ask the telephone number
![Page 6: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/6.jpg)
Classes and Objects • Every object is of a certain class• The class define the type of the object, this means
– what variables contains an object of this class
– which methods can be applied to an object of this class
Class Clock
Object A of class clock
Object B of class clock
Object C of class clock
![Page 7: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/7.jpg)
An Example about use of Objects in Java (1)
• Clock A = new Clock( );
• A.setTime(3,35);
• int i;
• i = A.getHour( );
Put pointers in 3 and 35
Get the value of the hour-pointer
![Page 8: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/8.jpg)
An Example about use of Objects in Java (2)
• PhoneBook B = new PhoneBook( );
• B.addEntry(“Eduardo Vera”,422596160);
• B.getNumber(“Eduardo Vera” );
Adds the name and thephone number given
Gets the number of the person
Creates a new Phone Book
Eduardo Vera422596160
Eduardo Vera
422596160
![Page 9: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/9.jpg)
Why Object Orientation ?• A strategy to develop bigger programs with
few errors and at lower cost (time) by building over existing components
![Page 10: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/10.jpg)
Robust (compared with C)• The basic instructions in Java are similar to the basic
instructions in C BUT it has not the instructions and characteristics which make C less stable like:– Pointer arithmetic– assignation in conditions like in if (a = 9)– garbage collection (liberating memory which is not used any
more)
• It permits to program a set of instructions within a try and catch blocktry {
instructions which may cause an error} catch (Error e) {
instructions to react to the error}
![Page 11: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/11.jpg)
Network savvy • Java appears when the internet and the WWW are
already popular and in rapid expansion (1995)• It has a library with classes which make possible to
easily communicate 2 programs running in a TCP/IP network (internet)
INTERNET
![Page 12: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/12.jpg)
Examples
• Provides a library with classes which make possible to easily communicate 2 programs running in a TCP/IP network (internet)
– Find IP Address given the host’s name– Establish URL connections with www servers– Build TCP & UDP sockets– Implement RMI by implementing remote objects
![Page 13: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/13.jpg)
JAVA is an interpreted Language
Java compiler (specific for each platform) javac P1.java
P1.java P1.class
Java interpreter (specific for each platform) also called Java Virtual Machine java P1 (class)
Program’soutput
![Page 14: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/14.jpg)
What kinds of programs can I develop with JAVA ?
• Stand alone programs: written with a text editor, compiled and interpreted
• Applets: programs that run inside www page.
• Servlets: programs invoked by a www page running on the server side
• Java script
• JSP
![Page 15: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/15.jpg)
Java Applets
<applet code=Animator.class ><parameters></applet>
Java programrunning on the client
Html
Animator.class
Applets are java programs which are downloaded with the HTML page.
Animator.class
![Page 16: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/16.jpg)
Java Script
Java programrunning on the client
Html & Script
The code of the java program is written directly in the HTML page
<script language = “JavaScript”>the code</script>
![Page 17: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/17.jpg)
Java ServletsThe code of the java program which runs on the serverand can dynamically produce HTML content accordingto the particular situation (the client of a bank)
MyServlet.class
HTML-page with a reference to a servlet
HTML from page
HTML from servlet
![Page 18: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/18.jpg)
In JAVA Programming means to write classes
• What components (attributes) should have an object of this class ?
• How can I create a new Object (constructor) ?
• What methods can I perform on an object of this class ?
• There may be a static method which can be called from “outside”.
• Is my new class the extension of an existing one ?
![Page 19: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/19.jpg)
How do I write a simple stand alone program ?
public class Classname {public static void main(String args[ ])
{HERE JAVA INSTRUCTIONS
} // end of the main method } // end of the class
• Write a file with the program (the name should be Classname.java)
• Compile it with the command javac Classname.java
• This will create a new file Classname.class• Execute the command java Classname and the
instructions written in the main method will be executed.
![Page 20: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/20.jpg)
public class HelloWorld {public static void main(String args[ ])
{System.out.println(“Hello World”);
} // end of the main method} // end of the class
• Every program should start with a class declaration:
public class ClassName (class name is given by the programmer)
• The instruction which will be executed should be written inside a method called main(String args[ ]) The instructions should be written inside the brackets {}
The “Hello World” example
![Page 21: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/21.jpg)
public class HelloWorld {public static void main(String args[ ])
{System.out.println(“Hello World”);
} // end of the main method} // end of the class
• After a double slash // the rest of the line is considered comments and are not executable instructions
• The instruction System.out.println(“Hello World”); writes a line on the screen with everything that is inside the brackets
• This program should be written in a file named HelloWorld.java
The “Hello World” example
![Page 22: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/22.jpg)
public class Program2 {public static void main(String args[ ]) { int myNumber1 = 5; double myNumber2 = 9.0;
System.out.println(“number is ” +myNumber1);
System.out.println(”number plus 1 is ”+(myNumber1+1));
System.out.println(”number minus 3 is ”+(myNumber1-3));
System.out.println(”double of number is ”+(myNumber1*2));
System.out.println(”half of number is ”+(myNumber1/2));
System.out.println(”My other number is ”+myNumber2);
System.out.println(”The half is ” + (myNumber2/2));
System.out.println(“The square root is ” + Math.sqrt(myNumber2));
}}
More about programming...
![Page 23: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/23.jpg)
import java.io.*; public class Program3 {public static void main(String args[ ]) throws IOException { BufferedReader inKbd = new BufferedReader(
new InputStreamReader(System.in)); String inputLine; System.out.print(“ please enter your name: ”); inputLine = inKbd.readLine(); System.out.println(“Hajimemashite,”+ inputLine); }
}
Reading a line of characters from keyboard
• It is only possible to read a line of characters with this method• It is necessary to write throws IOException at the beginning of the main method. Otherwise it will not even pass compilation • For using input/output classes it is necessary to import the classes from the java.io.* library
![Page 24: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/24.jpg)
import java.io.*;public class Program4 {public static void main(String args[ ]) throws IOException { BufferedReader inKbd = new BufferedReader(
new InputStreamReader(System.in));
String inputLine; System.out.print(“I am Jalisco, enter your number ! ”); inputLine = inKbd.readLine(); int yourNumber = Integer.parseInt(inputLine); System.out.println(“My number is ”+
(yourNumber+1)+” I win !”); }
}
Reading a number from keyboard
• In Java2 there is also Double.parseDouble(aString)• If the content of the String does not correspond to a and error will occur during the program execution
![Page 25: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/25.jpg)
Primitive types in Java• integer: int, long, short, byte Const. 1, -1, 1024, 1L
• real: float, doubleConst. 1.0, -3.14159, 1.5e4, 1.0f
• character: char Const. ‘a’, ‘X’, ‘@’
• logic value: boolean Const. true, false
String: “Hola“,“21 of Jannuary 2000” Not a Primitive
![Page 26: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/26.jpg)
Declarationsint i; //declarationint i = 1; //declaration and
//initializationdouble pi = 3.14159;char c = ‘a’;boolean genki_desu = true;• Declarations are necessary to reserve memory for
storing a value in a certain part of the memory and referencing it with a name (not address)
• A declaration of variable can appear in any part of the program but always before the variable is used.
![Page 27: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/27.jpg)
Expressions & Assignation
• Aritmethics : sum + 20 * c / (mod % 3)• Boolean: a > b, b >= c, c != 4, a == 0• String: “Hello “+ name + “ today is the “+
day + “of”+month• Casts: (int) pi (pi = 3.1)
(int) (Math.random()*100)+1)• other: a == 1 ? a+1 : a-1• Assignation: a = 1;• Assignation as operator: a = b = c = d = 0;
![Page 28: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/28.jpg)
Controlling the sequence of instructions
• Conditional execution of instructions: if (condition)
instruction;
if(condition) instruction;
else instruction;
• It is always possible to write more than one instruction after the if and the else by grouping them inside curly brackets { }
![Page 29: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/29.jpg)
Problem • Write a program that reads 2 numbers from the keyboard. The first one is the amount
a customer has to pay at a bookstore. The second is the sum the customer gives to pay for the books. The computer should respond if the sum is not enough, if it exact the amount to pay or if then customer should receive change. In case the customer should receive change, the computer must give the exact amount of 5000 and 1000 bills and 500, 100, 50, 10, 5 and 5 coins.
Enter the value to pay:3561Enter the value given by the customer :10000give 1 of 5000give 1 of 1000give 4 of 100give 3 of 10give 1 of 5give 4 of 1
![Page 30: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/30.jpg)
Doing Loops
• The basic instruction for the loop is the whilewhile (condition) instruction;
• There are others, but they are variations
do instruction; while (condition);
for (instr1; condition; instr2)instruction;
![Page 31: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/31.jpg)
Example Loops• The while loop
int i = 1;while (i <= 10) { System.out.println(”5 X ” + i + ” = ”+(i*5))
i = i + 1; }
• The do while loop int i = 1; do {
System.out.println(”5 X ” + i + ” = ”+(i*5)); i = i + 1;
} while (i <= 10);
• The for loop for (i = 1; i <= 10; i = i +1)
System.out.println(”5 X ” + i + ” = ”+(i*5));
![Page 32: Nelson Baloian Universidad de Chile](https://reader036.vdocuments.mx/reader036/viewer/2022062517/568130e3550346895d96f819/html5/thumbnails/32.jpg)
Another Example: the MCDpublic class MCD {//computing of the maximum common
//divider of 15 & 24public static void main(String args[ ]){
int x = 15, y = 24; while (x != y) { if (x < y)
y = y - x; else
x = x - y; }
System.out.println (“The MCD of 15 & 24 is “ + x);
}}