Anda di halaman 1dari 40

Java

S.Ganesh Babu

Basic Java

Exception
Thread
Serialization
AWT
Applet
Swing
Collection

Advance Java

RMI
CORBA
Java Media Framework
JMS

Java Introduction

Class
Object
Java Virtual Machine
Constructor
OOP Concepts
Eclipse IDE

Exception

Introduction
Try Catch
Multiple Try Catch
Try Catch Finally
throws
throw - User Defined Exceptions

Exception

Introduction
Try Catch
Multiple Try Catch
Try Catch Finally
throws
throw - User Defined Exceptions

Exception Try - Catch


public class ExceptionSample {
public static void main(String args[])
{
try {
} catch (Exception e) {
// Handle Exception
}finally
{
System.out.println("Final Block");
}
}
}

Exception User Defined


class MyException extends Exception
{
String name;
public MyException(String str)
{
name= str;
}
public String toString()
{
return "My Exception Demo";
}
}

Exception throws
public class ExceptionSample {
public static void main(String args[]) throws Exception
{
}
}

Thread

Introduction
Multithreading
Implementing Runnable Interface
Extending Thread Class
Suspend Resume
wait notify - notifyAll

Thread Life Cycle

Thread Runnable Interface


public class RunnableSample implements Runnable
{
Thread t;
public RunnableSample() {
t= new Thread(this,"Example Thread");
t.start();
System.out.println("Child thread");
}
public void run() {
try {
for (int j = 0; j < 10; j++) {
System.out.println(j);
Thread.sleep(100);
}
} catch (Exception e) {
System.out.println("error"+e);
}
}
}

Thread extending Thread


public class ThreadSample extends Thread
{
Thread t;
public RunnableSample() {
t= new Thread("Example Thread");
start();
System.out.println("Child thread");
}
public void run() {
try {
for (int j = 0; j < 10; j++) {
System.out.println(j);
Thread.sleep(100);
}
} catch (Exception e) {
System.out.println("error"+e);
}
}
}

Thread Control
Suspend
Resume
Wait
Notify
Notify All

Serialization

Introduction
Object Serialization
Demo

Serialization - Program
class MyClass implements Serializable {
String s;
int i;
double d;
public MyClass(String s, int i, double d) {
this.s = s;
this.i = i;
this.d = d;
}
public String toString() {
return "s=" + s + "; i=" + i + "; d=" + d;
}
}

AWT

Introduction
GUI concepts
Containers
Layout Managers
Controls
Events

AWT

Frame
Flowlayout,BorderLayout,GridLayout
TextField
Button
CheckBox
Choice
List
TextArea
Label
Listeners ActionListener, WindowListener
etc

Applet

Introduction
LifeCycle
Init,start,stop,paint,destroy
Running Applet Programs
Appletviewer

Applet - Program
import
import

java.applet.*;
java.awt.*;

public class RectangleApplet extends Applet {


public void paint(Graphics g) {
g.drawRect(0, 0, 100, 100);
}
}

Swing

Introduction
Difference Between AWT & Applet
Heavy Weight Light Weight
Content Pane
UIManager
Swing Components

Java Collection

Introduction
Lists
store objects in a specific order
Sets
Reject duplicates of any objects already in the collection

Maps
store objects in association with a key, which is later used to
look up and retrieve the object (note that if an item with a
duplicate key is put into a map, the new item will replace the
old item)

Java Collection Tree


Collection

MAP
SORTED MAP

LIST
ArrayList,
Vector

SET
Hash Set,
TreeSet

TreeMap,Hash
Map,HashTable

Java Collection Examples


import java.util.*;
public class ArrayListSample {
public static void main(String args[]) {
List list = new ArrayList();
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");
System.out.println(list);
}
}

Java Collection Examples


import java.util.Set;
import java.util.TreeSet;
public class SetExample {
public static void main(String args[]) {

}
}

Set set = new HashSet();


set.add("One");
set.add("Two");
set.add("Three");
set.add("Four");
set.add("Five");
System.out.println(set);
Set sortedSet = new TreeSet(set);
System.out.println(sortedSet);

Java Collection <k,v>


Hashtable ht = new Hashtable();
ht.put("1","one");
ht.put("2","two");
ht.put("3","three");
ht.put("4","four");
System.out.println(ht.get("4"));

Advance Java

RMI
CORBA
Java Media Framework
JMS

Remote Method Invocation

Introduction
Server Program
Client Program
Interface
Rmic compiler
rmiregistry
executing RMI Programs

RMI Server Program


import
import
import

java.rmi.Naming;
java.rmi.RemoteException;
java.rmi.server.UnicastRemoteObject;

publicclass AddServer extends UnicastRemoteObject implements AddServerInterface


{
public AddServer()throws RemoteException
{}
public double add(double d1,double d2)throws RemoteException
{
return(d1+d2);
}
public static void main(String ar[])
{
try
{
AddServer addser = new AddServer();
Naming.rebind("AddServer",addser);
}
catch(Exception e)
{
System.out.println("Exce caught"+e);
}
}
}

RMI Client Program


import java.rmi.Naming;
public class AddClient
{
public static void main(String ar[])
{
try
{
String url="rmi://localhost/AddServer";
AddServerInterface ob=(AddServerInterface)Naming.lookup(url);
System.out.println("RMI started working..");
System.out.println("Sum of two numbers"+ob.add(12,90));
}
catch(Exception e)
{ System.out.println("This is "+e); }
}
}

RMI Interface
import java.rmi.Remote;
import java.rmi.RemoteException;
interface AddServerInterface extends Remote
{
double add(double d1,double d2)throws
RemoteException;
}

CORBA

Introduction
Architecture
CORBA - Demo

CORBA - IDL Program


module HelloApp
{
interface Hello
{
string sayHello();
oneway void shutdown();
};
};

CORBA Server Program

class HelloImpl extends HelloPOA


{
private ORB orb;
public void setORB(ORB orb_val) { orb=orb_val; }
public String sayHello() { return "\nHello world!!\n"; }
public void shutdown()
{ orb.shutdown(false); }
}
public class HelloServer {
public static void main(String args[]) {
try {
ORB orb=ORB.init(args,null);
POA rootpoa=POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
HelloImpl helloImpl=new HelloImpl();
helloImpl.setORB(orb);
org.omg.CORBA.Object ref=rootpoa. servant_to_reference(helloImpl);
Hello href=HelloHelper.narrow(ref);
org.omg.CORBA.Object objRef=orb.resolve_initial_references("NameService");
NamingContextExt ncRef=NamingContextExtHelper.narrow(objRef);
String name="hello";
NameComponent path[]=ncRef.to_name(name);
ncRef.rebind(path,href);
System.out.println("server ready and waiting");
orb.run();
}
catch(Exception e) { System.err.println("ERRROR:"+e); e.printStackTrace(System. out); }
System.out.println("server exiting");
}}

CORBA Client Program

public class HelloClient


{
static Hello helloImpl;
public static void main(String args[]) {
try
{
ORB orb=ORB.init(args,null);
org.omg.CORBA.Object
objRef=orb.resolve_initial_references("NameService");
NamingContextExt ncRef=NamingContextExtHelper.narrow(objRef);
String name="hello";
helloImpl=HelloHelper.narrow(ncRef.resolve_str(name));
System.out.println("Obtained a handle on server
object:"+helloImpl);
System.out.println(helloImpl.sayHello());
helloImpl.shutdown();
}catch(Exception e)
{
System.out.println("ERROR:"+e);
e.printStackTrace(System.out);
}
}

Java Media Framework

Introduction
Demo

JMF - Program
public class MediaPanel extends JPanel
{
public MediaPanel( URL mediaURL )
{
setLayout( new BorderLayout() ); // use a BorderLayout
// Use lightweight components for Swing compatibility
Manager.setHint
Manager.setHint(( Manager.LIGHTWEIGHT_RENDERER
Manager.LIGHTWEIGHT_RENDERER,, true );
try
{
// create a player to play the media specified in the URL
Player mediaPlayer = Manager.createRealizedPlayer
Manager.createRealizedPlayer(( mediaURL );
// get the components for the video and the playback controls
Component video = mediaPlayer.getVisualComponent();
Component controls = mediaPlayer.getControlPanelComponent();
if ( video != null )
add( video, BorderLayout.CENTER
BorderLayout.CENTER ); // add video component
if ( controls != null )
add( controls, BorderLayout.NORTH
BorderLayout.NORTH ); // add controls

mediaPlayer.start(); // start playing the media clip


} // end try
catch ( NoPlayerException noPlayerException )
{
System.err
.println( "No media player found" );
System.err.println(
} // end catch
catch ( CannotRealizeException cannotRealizeException )
System.err
.println( "Could not realize media player" );
System.err.println(
} // end catch
catch ( IOException iOException )

System.err
.println( "Error reading from the source" );
System.err.println(
} // end catch
} // end MediaPanel constructor
} // end class MediaPanel

JMF - Program

public class MediaTest


{
// launch the application
public static void main( String args[] )
{
// create a file chooser
JFileChooser fileChooser = new JFileChooser();

// show open file dialog


int result = fileChooser.showOpenDialog( null );
if ( result == JFileChooser.APPROVE_OPTION
JFileChooser.APPROVE_OPTION ) // user chose a file
{
URL mediaURL = null;
null;
try
{
// get the file as URL
mediaURL = fileChooser.getSelectedFile().toURL()
;
fileChooser.getSelectedFile().toURL();
} // end try
catch ( MalformedURLException malformedURLException )
{
System.err
.println( "Could not create URL for the file" );
System.err.println(
} // end catch
if ( mediaURL != null ) // only display if there is a valid URL
{
JFrame mediaTest = new JFrame( "Media Tester" );
mediaTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE
JFrame.EXIT_ON_CLOSE );
MediaPanel mediaPanel = new MediaPanel( mediaURL );
mediaTest.add( mediaPanel );
mediaTest.setSize( 300, 300 );
mediaTest.setVisible( true );
} // end inner if
} // end outer if
} // end main
} // end class MediaTest

Java Messaging Service

Introduction
point to-point JMS
Queue JMS

Thank You

Anda mungkin juga menyukai