1 / 32

Nelson Baloian Universidad de Chile

Internetworking with JAVA. 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 ?.

Download Presentation

Nelson Baloian Universidad de Chile

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Internetworking with JAVA Nelson Baloian Universidad de Chile

  2. 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 ?

  3. 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

  4. 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

  5. Examples of Objects Set the pointers’ value • A clock: the variables are the position of the pointers • A telephone book: the variables are a list of names and telephone numbers 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

  6. 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

  7. 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

  8. An Example about use of Objects in Java (2) • PhoneBook B = new PhoneBook( ); • B.addEntry(“Eduardo Vera”,422596160); • B.getNumber(“Eduardo Vera” ); Creates a new Phone Book Adds the name and the phone number given Eduardo Vera 422596160 Eduardo Vera Gets the number of the person 422596160

  9. Why Object Orientation ? • A strategy to develop bigger programs with few errors and at lower cost (time) by building over existing components

  10. 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 }

  11. 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

  12. 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

  13. JAVA is an interpreted Language Java compiler (specific for each platform) javac P1.java Program’s output P1.java P1.class Java interpreter (specific for each platform) also called Java Virtual Machine java P1 (class)

  14. 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

  15. Java Applets Applets are java programs which are downloaded with the HTML page. Html Animator.class Animator.class Java program running on the client <applet code=Animator.class > <parameters> </applet>

  16. 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

  17. 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

  18. 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 ?

  19. 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.

  20. 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 calledmain(String args[ ]) The instructions should be written inside the brackets{}

  21. 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 instructionSystem.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

  22. 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)); } }

  23. 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

  24. Reading a number from keyboard 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 !”); } } • 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

  25. 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”Not a Primitive

  26. Declarations int i; //declaration int i = 1; //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.

  27. 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;

  28. 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 { }

  29. 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

  30. 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;

  31. 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));

  32. 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); } }

More Related