Programming in Java - PowerPoint PPT Presentation

About This Presentation
Title:

Programming in Java

Description:

Programming in Java Review; Topics for Further Study – PowerPoint PPT presentation

Number of Views:498
Avg rating:3.0/5.0
Slides: 27
Provided by: Alo133
Learn more at: http://www.cs.rpi.edu
Category:

less

Transcript and Presenter's Notes

Title: Programming in Java


1
Programming in Java
  • 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

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

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

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

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) xax yay
  • public String toString() return
    "("x","y") "
  • class Line implements Serializable
  • public Point p1, p2
  • Line (Point ap1, Point ap2) p1ap1 p2ap2
  • public String toString() return p1 "-"
    p2

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()

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(g
    zis)
  • / Read the object /
  • Line l (Line) in.readObject()
  • System.out.println ("Line " l)

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

Java Bean
Application Program
Beanbox tools
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.

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

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)

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

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("filec/autoexec.bat")
  • InputStream in url.openStream()
  • // Now copy bytes from the URL to the output
    stream
  • byte buffer new byte4096
  • int bytes_read
  • while((bytes_read in.read(buffer)) ! -1)
  • System.out.write(buffer, 0, bytes_read)
  • in.close()

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(args0)
  • 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())

16
Networking (getContent)
  • import java.io.
  • import java.net.
  • public class AkmGetURL
  • public static void main(String args) throws
    IOException
  • URL url new URL(args0)
  • 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)
    bis.read()
  • System.out.print("\n")
  • else
  • System.out.println ("Cannot show object of
    this type")

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

18
EchoServer
Server
out
in
in
out
Client
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.getInputStr
    eam()))
  • PrintWriter out new PrintWriter
  • (new OutputStreamWriter(theSocket.getOutputS
    tream()), true)

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()

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()

22
EchoClient (2)
  • public void init()
  • try
  • String host getCodeBase().getHost()
  • Socket theSocket new Socket( host,
    thePort )
  • in new BufferedReader
  • (new InputStreamReader(theSocket.getInputSt
    ream()))
  • out new PrintWriter
  • (new OutputStreamWriter(theSocket.getOutputStre
    am()), 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)

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
    )

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

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

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
Write a Comment
User Comments (0)
About PowerShow.com