Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Nelson Baloian Universidad de Chile 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 ? 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 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 Examples of Objects • A clock: the variables are the position of the pointers Set the pointers’ value What time is it ? • A telephone book: the variables are a list of names and telephone numbers • Set a name with a telephone number • change the telephone number • delete a pair name-telephone • Given a name ask the telephone number 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 Object A of class clock Object B of class clock Class Clock Object C of class clock 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 An Example about use of Objects in Java (2) • PhoneBook B = new PhoneBook( ); Creates a new Phone Book • B.addEntry(“Eduardo Vera”,422596160); Eduardo Vera 422596160 Adds the name and the phone number given • B.getNumber(“Eduardo Vera” ); Eduardo Vera 422596160 Gets the number of the person Why Object Orientation ? • A strategy to develop bigger programs with few errors and at lower cost (time) by building over existing components 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 block try { instructions which may cause an error } catch (Error e) { instructions to react to the error } 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 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 JAVA is an interpreted Language P1.java P1.class Program’s output Java compiler (specific for each platform) javac P1.java Java interpreter (specific for each platform) also called Java Virtual Machine java P1 (class) 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 Java Applets Applets are java programs which are downloaded with the HTML page. Html Animator.class Animator.class <applet code=Animator.class > <parameters> </applet> Java program running on the client Java Script The code of the java program is written directly in the HTML page Html & Script <script language = “JavaScript”> the code </script> Java program running on the client Java Servlets The code of the java program which runs on the server and can dynamically produce HTML content according to the particular situation (the client of a bank) HTML from page HTML from servlet MyServlet.class HTML-page with a reference to a servlet 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 ? 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. The “Hello World” example 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 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 More about programming... 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)); } } Reading a line of characters from keyboard 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); } } • 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 Reading a number from keyboard import java.io.*; public class Program4 { public static void main(String args[ ]) IOException { throws 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 !”); } } • 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 Primitive types in Java • integer: int, long, short, byte Const. 1, -1, 1024, 1L • real: float, double Const. 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” Primitive Not a Declarations int i; int i = 1; //declaration //declaration and //initialization double 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. 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; 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 { } 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:3561 Enter the value given by the customer :10000 give 1 of 5000 give 1 of 1000 give 4 of 100 give 3 of 10 give 1 of 5 give 4 of 1 Doing Loops • The basic instruction for the loop is the while while (condition) instruction; • There are others, but they are variations do instruction; while (condition); for (instr1; condition; instr2) instruction; 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)); Another Example: the MCD public class MCD { //computing of the maximum common //divider of 15 & 24 public 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); } }