Java Outline • Intro and Applications • Applets and GUI

Download Report

Transcript Java Outline • Intro and Applications • Applets and GUI

Java Outline
•
•
•
•
Intro and Applications
Applets and GUI
Client / Server
RMI, JDBC
CSTP FS99
CS423 (cotter)
1
Background
•
•
•
•
Objective: Platform independent C++
1991 - 1994 Oak
1994 - JAVA
Virtual Machine / Interpreter
CSTP FS99
CS423 (cotter)
2
Java “Buzzwords”
Simple
Network-Savy
Secure
Portable
High Performance
Dynamic
CSTP FS99
Object-Oriented
Robust
Architecture neutral
Interpreted
Multi-threaded
CS423 (cotter)
3
JAVA Implementation
Java program
Java program
Java program
Java program
Java Cmds
Java Cmds
Java Cmds
VM
SPARC
CSTP FS99
VM
VM
PowerPC
Intel
CS423 (cotter)
4
Extension to Internet
•
•
•
•
Timing with Internet growth
Same functionality needed
Just implement a new tag (applet)
Browser loads web page & applet
CSTP FS99
CS423 (cotter)
5
Operations Environments
• Applications program (~DOS window)
– supports file access
• Applet (browser)
– supports Internet linkages.
CSTP FS99
CS423 (cotter)
6
Object Oriented Concepts
• Intent is to model the environment
• Combine attributes with functions
CSTP FS99
CS423 (cotter)
7
OO Structure
(audio clip)
Play
Stop
Characteristics
Current track
whatever...
Wave Data
Record
CSTP FS99
CS423 (cotter)
8
OO vs. Procedures
function
function
function
function
Global Data
Play
Play clip
Stop
stop
Audio Player
Current track
Record
record clip
CSTP FS99
Characteristics
whatever...
Wave Data
CS423 (cotter)
9
OO Concepts
• Inheritance
– Integer
– Myinteger (with min and max fields)
• Method Overloading
– public String(char value[]);
– public String(char value[], int offset, int count);
• Polymorphism
– public int hashCode();
CSTP FS99
CS423 (cotter)
10
OO Class Relationships
• use (call a method, etc.)
• containment (“has-a”)
• inheritance (is-a”)
CSTP FS99
CS423 (cotter)
11
OO Structure
• class
– data
• public
• private
– methods
• constructor
• destructor
• others
CSTP FS99
CS423 (cotter)
12
Java Structure
class HelloApplication
{
public static void main (String argv[])
{
String message[] = new String[2];
message[0] = "Welcome to CS490D";
message[1] = "The subject is JAVA!";
System.out.println (message[0]);
System.out.println (message[1]);
}
}
CSTP FS99
CS423 (cotter)
13
Java Support Environments
• JDK 1.0.2, JDK 1.1.4, JDK 1.2.2
• Borland JBuilder
• Semantic Cafe
– just-in-time compiler
• Visual J++
• etc.
CSTP FS99
CS423 (cotter)
14
Java Info Sources
• http:/java.sun.com
– The home for Java
• http://www.gamelan.com/
– central repository for info
• MANY others.
CSTP FS99
CS423 (cotter)
15
Java Language Structure Packages (1.0.2)
•
•
•
•
•
•
•
•
java.lang - basic classes (objects, strings, threads, etc.)
java.io - stream mgmt for files, strings, other sources
java.util - time, rand, generic data struct..
java.net - URLs, TCP, UDP, IP, binary to text
java.awt - manages user interface components
java.awt.image - manage image data
java.awt.peer - platform specific implementation links
java.applet - Applet class, interfaces to resources
CSTP FS99
CS423 (cotter)
16
java.lang
•
•
•
•
•
•
•
•
Object
Process
Runtime
Thread
Math
String
Integer
etc……
CSTP FS99
CS423 (cotter)
17
java.lang.Object
public
{
class
java.lang.Object
// Constructors
public Object();
§1.12.1
// Methods
protected Object clone();
§1.12.2
public boolean equals(Object obj); §1.12.3
protected void finalize(); §1.12.4
public final Class getClass();
§1.12.5
public int hashCode();
§1.12.6
public final void notify(); §1.12.7
public final void notifyAll();
§1.12.8
public String toString();
§1.12.9
public final void wait();
§1.12.10
public final void wait(long timeout);
§1.12.11
public final void wait(long timeout, int nanos); §1.12.12
}
CSTP FS99
CS423 (cotter)
18
java.lang.integer
public final class java.lang.Integer
extends java.lang.Number (I-§1.11)
{
// Fields
public final static int MAX_VALUE; §1.8.1
public final static int MIN_VALUE; §1.8.2
// Constructors
public Integer(int value); §1.8.3
public Integer(String s); §1.8.4
CSTP FS99
CS423 (cotter)
19
java.lang.integer
// Methods
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
}
CSTP FS99
double doubleValue();
§1.8.5
boolean equals(Object obj); §1.8.6
float floatValue(); §1.8.7
static Integer getInteger(String nm);
§1.8.8
static Integer getInteger(String nm, int val);
§1.8.9
static Integer getInteger(String nm, Integer val); §1.8.10
int hashCode();
§1.8.11
int intValue();
§1.8.12
long longValue();
§1.8.13
static int parseInt(String s);
§1.8.14
static int parseInt(String s, int radix); §1.8.15
static String toBinaryString(int i);
§1.8.16
static String toHexString(int i);
§1.8.17
static String toOctalString(int i); §1.8.18
String toString();
§1.8.19
static String toString(int i);
§1.8.20
static String toString(int i, int radix); §1.8.21
static Integer valueOf(String s);
§1.8.22
static Integer valueOf(String s, int radix);
§1.8.23
CS423 (cotter)
20
Building Java Applications
Using Java Development Kit
• Get a copy of the JDK (java.sun.com, etc.)
• Install the development kit (tools and classes)
• Create MyApp.java source file
– text editor (notepad, etc.)
– winedit, Café, etc.
• Compile .java file to create .class file
– javac MyApp.java
• Run application (.class file) through java VM
– java MyApp
CSTP FS99
CS423 (cotter)
21
Sample Java App
G:\Data\Java\MyHelloApp>type MyHelloApp.java
class MyHelloApp
{
public static void main (String argv[])
{
String message[] = new String[2];
message[0] = "Welcome to CS423";
message[1] = "The subject is JAVA!";
System.out.println (message[0]);
System.out.println (message[1]);
}
}
CSTP FS99
CS423 (cotter)
22
Sample Java App
G:\Data\Java\MyHelloApp>javac MyHelloApp.java
G:\Data\Java\MyHelloApp>java MyHelloApp
Welcome to CS423
The subject is JAVA!
G:\Data\Java\MyHelloApp>
CSTP FS99
CS423 (cotter)
23
Add Interaction with User
class ParmApplication
{
public static void main (String argv[])
{
int i;
if (argv.length == 0)
{
System.out.println ("");
System.out.println (" Put parameters on the "
+ "command line. Here is the syntax:");
CSTP FS99
CS423 (cotter)
24
Add Interaction with User
System.out.println ("");
System.out.println ("java ParmApplication Parm0 "
+ "Parm1 Parm2 [...]");
System.out.println ("");
System.exit (1);
}
System.out.println ("");
System.out.println (" " + argv.length
+ " string parameter(s) entered:");
CSTP FS99
CS423 (cotter)
25
Add Interaction with User
for (i = 0; i < argv.length; i++)
{
System.out.println
( "
Parm #"
+ i
+ ": --->"
+ argv[i]
+ "<---" );
}
System.out.println ("");
}
}
CSTP FS99
CS423 (cotter)
26
Add interaction with user
G:\ParmApplication>javac ParmApplication.java
G:\ParmApplication>java ParmApplication
Put parameters on the command line.
Here is the syntax:
java ParmApplication Parm0 Parm1 Parm2 [...]
G:\ParmApplication>java ParmApplication first 2nd 35
3 string
Parm
Parm
Parm
parameter(s) entered:
#0: --->first<--#1: --->2nd<--#2: --->35<---
G:\ParmApplication>
CSTP FS99
CS423 (cotter)
27
Get Input from User
import java.io.*;
class InputApp
{
InputApp ()
{
DataInputStream dis = new DataInputStream (System.in);
String sIn = " ";
String sOut = " ";
File fIn;
File fOut;
FileInputStream fis;
FileOutputStream fos;
int iNumBytesRead;
byte[] ab;
CSTP FS99
CS423 (cotter)
28
Get Input from User
System.out.println
("*-------------------------------------------*");
System.out.print ("Enter Source File Name Here: ");
System.out.flush ();
try {sIn = dis.readLine ();}
catch (Exception e)
{System.out.println ("Exception on input file name"
+ e);}
System.out.print("Enter Destination File Name Here: ");
System.out.flush ();
try {sOut = dis.readLine ();}
catch (Exception e)
{System.out.println ("Exception on output file name"
+ e);}
CSTP FS99
CS423 (cotter)
29
Get Input from User
try
{
fIn = new File (sIn);
fis = new FileInputStream (fIn);
fOut = new File (sOut);
fos = new FileOutputStream (fOut);
ab = new byte[2048];
while ((iNumBytesRead = fis.read (ab)) != -1)
{
fos.write (ab, 0, iNumBytesRead);
}
fis.close ();
fos.close ();
}
catch (Exception e)
{System.out.println ("Exception during copy: " + e); }
CSTP FS99
CS423 (cotter)
30
Get Input from User
System.out.println ("Data copied from " + sIn + " to "
+ sOut);
System.out.println
("*-------------------------------------------*");
}
public static void main (String args[])
{
new InputApp ();
}
}
CSTP FS99
CS423 (cotter)
31
Get Input from User
G:\Data\Java\InputApp>java InputApp
*-------------------------------------------*
Enter Source File Name Here: books_db.txt
Enter Destination File Name Here: My_list.txt
Data copied from books_db.txt to My_list.txt
*-------------------------------------------*
G:\Data\Java\InputApp>
CSTP FS99
CS423 (cotter)
32
Java Inheritance (extension)- awt
•
•
•
•
•
•
Object - “cosmic base class”
Component - button, listBox
Container - dialog box
Panel - “sub-window”, group of buttons, etc.
Applet - small application, web enabled
MyApplet - specific instance of an applet
CSTP FS99
CS423 (cotter)
33