1 / 26

Programming in Java

Programming in Java. Review; Topics for Further Study. Course Review (1). Introduction Basic structure of a Java application Java applets Java as a programming language data types type casting variables, operators, expressions statements (if, while, do, …) input and output

ivory
Download Presentation

Programming in Java

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. Programming in Java Review; Topics for Further Study Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  2. Course Review (1) • Introduction • Basic structure of a Java application • Java applets • Java as a programming language • data types • type casting • variables, operators, expressions • statements (if, while, do, …) • input and output • comparison to C/C++ • Arrays (single + multidimensional) • String, StringBuffer, StringTokenizer Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  3. Course Review (2) • Object-oriented programming in Java • Classes, objects • Defining custom classes and methods • Private, protected, package, and public visibility of classes and members • Instance members vs. class members (variables + methods) • Method overloading • Java's inheritance model • inheritance hierarchy • use of interfaces to simulate multiple inheritance • abstract classes • Packages Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  4. Course Review (3) • Graphics Primitives • Color • Lines, ovals, rectangles, etc. • Graphical User Interfaces • GUI components (Buttons, Text Fields, Labels, …) • Event-driven programming • Containers • Component hierarchies • Layout managers • Exception Handling • try, throw, catch, finally • Threads Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  5. There's much more to Java! • This course barely scratched the surface! • Java • 1.0: 8 packages, 211 classes • 1.1: 23 packages, 503 classes • 1.2: Expected to be released summer 1998 will be much larger • Remaining slides briefly look at a few other topics Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  6. Object Serialization • Serialization - Writing objects [to a file] • Deserialization - Reading objects [from a file] • Example Declaration • class Point implements Serializable { • public int x; • public int y; • Point (int ax, int ay) { x=ax; y=ay; } • public String toString() { return "("+x+","+y+") "; } • } • class Line implements Serializable { • public Point p1, p2; • Line (Point ap1, Point ap2) { p1=ap1; p2=ap2; } • public String toString() { return p1 + "-" + p2; } • } Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  7. Object Serialization • class LineWriter { • public static void main(String[] args) throws IOException { • /* Set up object to be serialized */ • Line l = new Line (new Point (1,2), new Point (3,4)); • System.out.println ("Line: " + l); • /* Set up the ObjectOutputStream */ • FileOutputStream fos = new FileOutputStream ("deleteme.akm"); • GZIPOutputStream gzos = new GZIPOutputStream(fos); • ObjectOutputStream out = new ObjectOutputStream(gzos); • /* Write out the object and clean up */ • out.writeObject(l); • out.flush(); • out.close(); • } • } Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  8. Object Deserialization • class LineReader { • public static void main (String[] args) • throws IOException, ClassNotFoundException • { • /* Setup the reader */ • InputStream fis = new FileInputStream ("deleteme.akm"); • GZIPInputStream gzis = new GZIPInputStream (fis); • ObjectInputStream in = new ObjectInputStream(gzis); • /* Read the object */ • Line l = (Line) in.readObject(); • System.out.println ("Line: " + l); • } • } Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  9. Java Beans • A reusable software component that can be manipulated visually in a builder tool • Examples • Spreadsheet; Graph generator module; Telephony • Java's version of Active-X (OCX, OLE) • Language independent Reusable components • Standard specifications for accessing properties and methods Application Program Java Bean Beanbox tools Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  10. Internationalization (I18N) • Internationalization • Process of making a program flexible enough to run in any locale • Localization • Process of arranging for a program to run in a specific locale • java.util.Locale • Characters • Unicode char encoding (16 bit) allows multiple alphabets • InputStreamReader, OutputStreamWriter - convert local encoding to Unicode. • Dates, times, formatting numbers, money, sorting strings • java.text package addresses these • See …/jdk1.1.5/demo/i18n/DateTimeFormat • Text messages should be displayed in the local language • ResourceBundle (and related classes in java.util) facilitates this. Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  11. Reflection • Reflection • Can be used to obtain information about a class and its members (properties, events, methods) • Can get/set values of instance variables dynamically • Can invoke methods dynamically • Technique is used by JavaBeans "introspection" mechanism • Implemented in the package java.lang.reflect • Example • JIN: examples/ch12/ShowClass.java Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  12. Database Access with SQL • JDBC • Java DataBase Connectivity • Allows Java programs to send SQL queries to database servers • Implemented by classes in the package java.sql • An API interface (not embedded SQL) • Example statements (not complete!) • import java.sql.*; • // Get database url, userid, password, establish connection • Connection conn = DriverManager.getConnection(url,usr,pwd); • Statement s = conn.createStatement(); • try { • s.executeUpdate("CREATE TABLE POINT (x INT, y INT)"); • } catch (SQLException e) { • … • } Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  13. Remote Method Invocation • Remote Method Invocation (RMI) • Powerful technology for networked applications • Server defines objects that clients can use remotely • Clients invoke methods of the objects remotely • Arguments and return values • Can be primitive values or Serializable objects • Server and client must be Java applications • Implemented using packages java.rmi and java.rmi.server Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  14. Networking (URL) • Part of package java.net (classes for networking) • URL = Uniform Resource Locator • Class: Organizes manipulation of URLs • Example: Display contents of a (text) URL • import java.io.*; • import java.net.*; • public class AkmGetURL { • public static void main(String[] args) throws IOException{ • URL url = new URL("file:c:/autoexec.bat"); • InputStream in = url.openStream(); • // Now copy bytes from the URL to the output stream • byte[] buffer = new byte[4096]; • int bytes_read; • while((bytes_read = in.read(buffer)) != -1) • System.out.write(buffer, 0, bytes_read); • in.close(); • } • } Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  15. Networking (URLConnection) • URLConnection • Allows you to get information about URL documents • import java.io.*; • import java.net.*; • public class AkmGetURL { • public static void main(String[] args) throws IOException{ • URL url = new URL(args[0]); • URLConnection con = url.openConnection(); • System.out.println ("Encoding: " + con.getContentEncoding()); • System.out.println ("Length : " + con.getContentLength()); • System.out.println ("Type : " + con.getContentType()); • System.out.println ("Expires : " + con.getExpiration()); • System.out.println ("Modified: " + con.getLastModified()); • } • } Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  16. Networking (getContent) • import java.io.*; • import java.net.*; • public class AkmGetURL { • public static void main(String[] args) throws IOException{ • URL url = new URL(args[0]); • Object urlContent = url.getContent(); • System.out.println ("** getContent returned instance of " + • "class " + urlContent.getClass()); • System.out.println ("----------Content-----------"); • if (urlContent instanceof String) { • System.out.println (urlContent); • } else if (urlContent instanceof InputStream) { • InputStream is = (InputStream) urlContent; • int b = is.read(); • while (b!=-1) { System.out.print((char) b); b=is.read(); } • System.out.print("\n"); • } else { • System.out.println ("Cannot show object of this type"); • } • } • } Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  17. Sockets • Sockets • Streams of data that can flow between networked computers • Involves at least two programs • Server: supplies data and services • Client: requests data and services • "EchoServer" • Example of Client/Server socket communication • Client sends text Strings to server • Server converts the strings to upper case and returns them • Note • To keep things simple, no error checking is done • Should have "try … catch" blocks to check for disconnect, etc. • To allow multiple clients, should use threads (each client gets its own thread Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  18. EchoServer Server out in out in Client Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  19. EchoServer • import java.net.*; • import java.io.*; • public class EchoServer { • public static void main(String args[]) throws IOException { • int thePort = 1234; • ServerSocket theServerSocket = new ServerSocket(thePort); • System.out.println( "Server ready." ); • Socket theSocket = theServerSocket.accept(); // Accept client • System.out.println( "Client connected." ); • /* Establish socket in/out streams */ • BufferedReader in = new BufferedReader • (new InputStreamReader(theSocket.getInputStream())); • PrintWriter out = new PrintWriter • (new OutputStreamWriter(theSocket.getOutputStream()), true); Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  20. EchoServer (cont) • /* Service requests… */ • while (true) { • String line = in.readLine(); • if (line == null) break; • System.out.println("Read: " + line); • line = line.toUpperCase() + "!"; • out.println(line); • System.out.println("Wrote: " + line); • } • /* Clean up and exit */ • in.close(); • out.close(); • theSocket.close(); • theServerSocket.close(); • } • } Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  21. EchoClient (1) • // A client applet (works with EchoServer.java) • import java.awt.*; • import java.awt.event.*; • import java.applet.*; • import java.net.*; • import java.io.*; • public class EchoClient extends Applet { • final static int thePort = 1234; • BufferedReader in; • PrintWriter out; • TextField theField = new TextField(20); • TextArea theArea = new TextArea(); Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  22. EchoClient (2) • public void init() { • try { • String host = getCodeBase().getHost(); • Socket theSocket = new Socket( host, thePort ); • in = new BufferedReader • (new InputStreamReader(theSocket.getInputStream())); • out = new PrintWriter • (new OutputStreamWriter(theSocket.getOutputStream()), true); • add(theField); • add(theArea); • setVisible(true); • theField.addActionListener(new ActionListener () { • public void actionPerformed(ActionEvent theEvent) { • out.println( theField.getText() ); • theField.setText( "" ); • } • }); • } • catch (IOException io) { • System.err.println("IOException:\n" + io); • } • } Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  23. EchoClient (3) • public void start() { • try { • while (true) { • String s = in.readLine(); • if ( s != null ) theArea.append( s + '\n' ); • } • } • catch (IOException io) { • System.err.println( "IOException:\n" + io ); • } • } • } Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  24. Swing • A new GUI toolkit • Will be part of JDK1.2 - Swing is already released separately • Simplifies development of windowing components • Customizable "look and feel" • Menus, tabs, controls look like those of a "native" application • Or, you can give your application a specific look and feel for a specific target platform Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  25. Heavyweight vs. Lightweight • Extends AWT • All AWT components are heavyweight • All Swing components are lightweight (except: JWindow, JFrame, JDialog, and JApplet) • Mixing Heavyweight and Lightweight components is not recommended • Lightweight components • More efficient use of resources • More consistent across platforms • Cleaner L&F integration (more control over this) • Can have transparent pixels • Can be non-rectangular • Don't respond to mouse events (they fall through to parent) • Swing has many more UI components than AWT • Tree-view, list-box, tabbed-pane • Adhere to JavaBeans specifications Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

  26. Swing is part of JFC 1.2 • JFC 1.2 contains three major APIs • Java 2D - Set of classes for advanced 2D graphics and imaging • Drag and Drop - Enables data transfer • Between Java application and native application • Across Java Applications • Within a single application • Accessibility API - Provides assistive technologies • Examples: screen magnifiers and audible text readers • Designed to help people with disabilities interact with JFC and AWT components Programming in Java; Instructor:Alok Mehta Review; Topics for Further Study

More Related