Wednesday, January 30, 2013

Inheritance and graphics programming using JAVA programming

Inheritance - 

InheritanceDemo.java
/**
@author Sreekandan.K
*/
class Employee
{
 private String name;
 private double salary;
 Employee(String n,double s)
 {
  name=n;
  salary=s;
 }
 public String getName()
 {
  return name;
 }
 public double getSalary()
 {
  return salary;
 }
}
class Manager extends Employee
{
 private double bonus;
 Manager(String n,double s)
 {
  super(n,s);
 }
 public void setBonus(double b)
 {
  bonus=b;
 }
 public double getSalary()
 {
  return super.getSalary()+bonus;
 }
}
public class InheritanceDemo
{
 public static void main(String args[])
 {
  Manager mgr=new Manager("BOSS",50000);
  mgr.setBonus(5000);
  System.out.println("Manager:");
  System.out.println("Name:"+mgr.getName());
  System.out.println("Salary:"+mgr.getSalary());
  System.out.println("Employees:");
  Employee[] emp=new Employee[2];
  emp[0]=new Employee("Sreekandan.K",41000);
  emp[1]=new Employee("XYZ",53000);
  for(Employee e:emp)
  {
   System.out.println("Name:"+e.getName());
   System.out.println("Salary:"+e.getSalary());
  }
 }
}

OUTPUT













SingleInheritanceDemo.java
/**
@author Sreekandan.K
*/
class Marks
{
 int mrk1=70,mrk2=65,mrk3=73;
}
class Total extends Marks
{
 int getTotal()
 {
  return mrk1+mrk2+mrk3;
 }
}
public class SingleInheritanceDemo
{
 public static void main(String args[])
 {
  Total ttl=new Total();
  System.out.println("Total:"+ttl.getTotal());
 }
}

MultilevelInheritanceDemo.java
/**
@author Sreekandan.K
*/
class Marks
{
 int mrk1=70,mrk2=65,mrk3=73;
}
class Total extends Marks
{
 int getTotal()
 {
  return mrk1+mrk2+mrk3;
 }
}
class AvgTot extends Total
{
 double getAvg()
 {
  return getTotal()/3;
 }
 void print()
 {
  System.out.println("Total:"+getTotal());
  System.out.println("Average:"+getAvg());
 }
}
public class MultilevelInheritanceDemo
{
 public static void main(String args[])
 {
  AvgTot at=new AvgTot();
  at.print();
 }
}

HierarchicalInheritanceDemo.java
/**
@author Sreekandan.K
*/
class stud
{
 int mrk1=70,mrk2=65,mrk3=73;
 int getTotal()
 {
  return mrk1+mrk2+mrk3;
 }
 double getAvg()
 {
  return getTotal()/3;
 }
}
class Total extends stud
{
 void print()
 {
  System.out.println("Total:"+getTotal());
 }
}
class Average extends stud
{
 void print()
 {
  System.out.println("Average:"+getAvg());
 }
}
public class HierarchicalInheritanceDemo
{
 public static void main(String args[])
 {
  Total t=new Total();
  Average avg=new Average();
  t.print();
  avg.print();
 }
}

MultipleInheritanceDemo.java
/**
@author Sreekandan.K
*/
interface Total
{
 int mrk1=70,mrk2=65,mrk3=73;
 public int getTotal();
}
interface Average
{
 public double getAvg();
}
class stud implements Total,Average
{
 public int getTotal()
 {
  return mrk1+mrk2+mrk3;
 }
 public double getAvg()
 {
  return getTotal()/3;
 }
 void print()
 {
  System.out.println("Total:"+getTotal());
  System.out.println("Average:"+getAvg());
 }
}
public class MultipleInheritanceDemo
{
 public static void main(String args[])
 {
  stud s=new stud();
  s.print();
 }
}

PolymorphismDemo.java
/**
@author Sreekandan.K
*/
class A
{
 int r=5,b=10,h=5;
 public void area()
 {
  System.out.println("Area of Different Objects");
 }
}
class Circle extends A
{
 public void area()
 {
  double a=Math.PI*r*r;
  System.out.println("Area of Circle:"+a);
 }
}
class Triangle extends A
{
 public void area()
 {
  double a=0.5*b*h;
  System.out.println("Area of Triangle:"+a);
 }
}
public class PolymorphismDemo
{
 public static void main(String args[])
 {
  A obj;
  obj=new A();
  obj.area();
  obj=new Circle();
  obj.area();
  obj=new Triangle();
  obj.area();
 }
}

AbstractClassDemo.java
/**
@author Sreekandan.K
*/
abstract class MyAbstractClass
{
 int val1=10,val2=10;
 abstract double area();
}
class Rect extends MyAbstractClass
{
 double area()
 {
  return val1*val2;
 }
}
class Triangle extends MyAbstractClass
{
 double area()
 {
  return 0.5*val1*val2;
 }
}
class AbstractClassDemo
{
 public static void main(String args[])
 {
  MyAbstractClass obj;
  obj=new Rect();
  System.out.println("Area of Rectangle:"+obj.area());
  obj=new Triangle();
  System.out.println("Area of Triangle:"+obj.area());
 }
}

ReflectionDemo.java
import java.lang.reflect.*;
/**
@author Sreekandan.K
*/
class ReflectionDemo
{
 public static void main(String args[])
 {
  try
  {
   Class c=Class.forName("java.util.Date");
   System.out.println("\nCONSTRUCTORS");
   Constructor cs[]=c.getConstructors();
   for(int i=0;i<cs.length;i++)
   {
    System.out.println(cs[i]);
   }
   System.out.println("\nMETHODS");
   Method ms[]=c.getMethods();
   for(int i=0;i<ms.length;i++)
   {
    System.out.println(ms[i]);
   }
   System.out.println("\nFIELDS");
   Field fs[]=c.getFields();
   for(int i=0;i<fs.length;i++)
   {
    System.out.println(fs[i]);
   }
  }
  catch(Exception e)
  {
   System.out.println("Exception:"+e);
  }
 }
}

OUTPUT

ObjectAnalyzer.java
import java.lang.reflect.*;
/**
@author Sreekandan.K
*/
class Person
{
 public String name;
 public int age;
 public Person(String n,int a)
 {
  name=n;
  age=a;
 }
}
public class ObjectAnalyzer
{
 public static void main(String args[])
 {
  try
  {
   Person p=new Person("AAS",23);
   Class c=Class.forName("Person");
   Field f1=c.getField("name");
   Field f2=c.getField("age");
   System.out.println("Original Values:");
   System.out.println("Name:"+f1.get(p));
   System.out.println("Age:"+f2.get(p));
   f1.set(p,"SREE");
   f2.set(p,25);
   System.out.println("Modified Values:");
   System.out.println("Name:"+f1.get(p));
   System.out.println("Age:"+f2.get(p));
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
 }
}

ArrayReflection.java
import java.lang.reflect.*;
import java.util.*;
/**
@author Sreekandan.K
*/
class ArrayReflection
{
 public static void main(String args[])
 {
  int a[]={10,20,30,40,50};
  Class c=a.getClass();
  Class compT=c.getComponentType();
  int len=Array.getLength(a);
  Scanner in=new Scanner(System.in);
  System.out.println("Enter the value to expand the existing array:");
  int n=in.nextInt();
  int newLen=len+n;
  Object b=Array.newInstance(compT,newLen);
  System.arraycopy(a,0,b,0,len);
  System.out.println("Array Length:"+Array.getLength(b));
  //Store a new value into the array b
  for(int i=len;i<Array.getLength(b);i++)
  {
   Array.set(b,i,(i+1)*10);
  }
  //Print all elements from the array b
  for(int i=0;i<Array.getLength(b);i++)
  {
   System.out.println(Array.get(b,i));
  }
 }
}

CloneDemo.java
/**
@author Sreekandan.K
*/
class Employee implements Cloneable
{
 private String name;
 private double salary;
 public Employee(String n,double s)
 {
  name=n;
  salary=s;
 }
 public String getName()
 {
  return name;
 }
 public double getSalary()
 {
  return salary;
 }
 public void setSalary(double b)
 {
  salary=salary+b;
 }
 public Employee clone()throws CloneNotSupportedException
 {
  Employee cloned=(Employee)super.clone();
  return cloned;
 }
}
public class CloneDemo
{
 public static void main(String args[])
 {
  try
  {
   Employee e1=new Employee("SK",10000);
   Employee e2=e1;
   e2.setSalary(5000);//affect the original original objects
   System.out.println("Original Values without cloning:");
   System.out.println("Name:"+e1.getName());
   System.out.println("Salary:"+e1.getSalary());
   Employee e3=new Employee("SK",10000);
   Employee e4=e3.clone();
   e4.setSalary(5000);//does not affect the original objects
   System.out.println("Original Values using cloning:");
   System.out.println("Name:"+e3.getName());
   System.out.println("Salary:"+e3.getSalary());
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
 }
}

InnerClassDemo.java
import java.awt.event.*;
import javax.swing.*;
class OutClass
{
 private class InClass implements ActionListener
 {
  public void actionPerformed(ActionEvent ae)
  {
   System.out.println("Action Performed...");
  }
 }
 public void setInterval(int interval)
 {
  ActionListener ic=new InClass();
  Timer t=new Timer(interval,ic);
  t.start();
 }
}
/**
@author Sreekandan.K
*/
public class InnerClassDemo
{
 public static void main(String args[])
 {
  OutClass oc=new OutClass();
  oc.setInterval(1000);
  JOptionPane.showMessageDialog(null,"Do u want to stop...");
  System.exit(0);
 }
}

LocalInnerClassDemo.java
import java.awt.event.*;
import javax.swing.*;
class OutClass
{
 public void setInterval(int interval)
 {
  class InClass implements ActionListener
  {
   public void actionPerformed(ActionEvent ae)
   {
    System.out.println("Action Performed...");
   }
  }
  ActionListener ic=new InClass();
  Timer t=new Timer(interval,ic);
  t.start();
 }
}
/**
@author Sreekandan.K
*/
public class LocalInnerClassDemo
{
 public static void main(String args[])
 {
  OutClass oc=new OutClass();
  oc.setInterval(1000);
  JOptionPane.showMessageDialog(null,"Do u want to stop...");
  System.exit(0);
 }
}

AnonymousInnerClassDemo.java
import java.awt.event.*;
import javax.swing.*;
class OutClass
{
 public void setInterval(int interval)
 {
  ActionListener ic=new ActionListener()
  {
   public void actionPerformed(ActionEvent ae)
   {
    System.out.println("Action Performed...");
   }
  };
  Timer t=new Timer(interval,ic);
  t.start();
 }
}
/**
@author Sreekandan.K
*/
public class AnonymousInnerClassDemo
{
 public static void main(String args[])
 {
  OutClass oc=new OutClass();
  oc.setInterval(1000);
  JOptionPane.showMessageDialog(null,"Do u want to stop...");
  System.exit(0);
 }
}

StaticInnerClassDemo.java
import java.awt.event.*;
import javax.swing.*;
class OutClass
{
 static class InClass
 {
  public void print()
  {
   System.out.println("Inner Class Method...");
  }
 }
}
/**
@author Sreekandan.K
*/
public class StaticInnerClassDemo
{
 public static void main(String args[])
 {
  OutClass.InClass ic=new OutClass.InClass();
  ic.print();
 }
}

LineDemo.java
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
class MyPanel extends JPanel
{
 public void paintComponent(Graphics g)
 {
  Graphics2D twoD=(Graphics2D)g;
  Line2D line=new Line2D.Double(100,200,300,300);
  twoD.draw(line);
 }
}
class MyFrame extends JFrame
{
 public MyFrame()
 {
  MyPanel p=new MyPanel();
  add(p);
  setSize(500,500);
  setVisible(true);
  setTitle("Line Drawing");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
/**
@author Sreekandan.K
*/
public class LineDemo
{
 public static void main(String args[])
 {
  MyFrame f=new MyFrame();
 }
}

RectangleDemo.java
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
class MyPanel extends JPanel
{
 public void paintComponent(Graphics g)
 {
  Graphics2D twoD=(Graphics2D)g;
  Rectangle2D rect=new Rectangle2D.Double(150,50,200,150);
  twoD.draw(rect);
 }
}
class MyFrame extends JFrame
{
 public MyFrame()
 {
  MyPanel p=new MyPanel();
  add(p);
  setSize(500,500);
  setVisible(true);
  setTitle("Rectangle Drawing");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
/**
@author Sreekandan.K
*/
 public class RectangleDemo
{
 public static void main(String args[])
 {
  MyFrame f=new MyFrame();
 }
}

EllipseDemo.java
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
class MyPanel extends JPanel
{
 public void paintComponent(Graphics g)
 {
  Graphics2D twoD=(Graphics2D)g;
  Ellipse2D ellipse=new Ellipse2D.Double(100,200,300,150);
  twoD.draw(ellipse);
 }
}
class MyFrame extends JFrame
{
 public MyFrame()
 {
  MyPanel p=new MyPanel();
  add(p);
  setSize(500,500);
  setVisible(true);
  setTitle("Ellipse Drawing");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
public class EllipseDemo
{
 public static void main(String args[])
 {
  MyFrame f=new MyFrame();
 }
}

CircleDemo.java
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
class MyPanel extends JPanel
{
 public void paintComponent(Graphics g)
 {
  Graphics2D twoD=(Graphics2D)g;
  Ellipse2D circle=new Ellipse2D.Double(70,100,300,300);
  twoD.draw(circle);
 }
}
class MyFrame extends JFrame
{
 public MyFrame()
 {
  MyPanel p=new MyPanel();
  add(p);
  setSize(500,500);
  setVisible(true);
  setTitle("Circle Drawing");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
/**
@author Sreekandan.K
*/
public class CircleDemo
{
 public static void main(String args[])
 {
  MyFrame f=new MyFrame();
 }
}

DrawShapes.java
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
class MyPanel extends JPanel
{
 public void paintComponent(Graphics g)
 {
  super.paintComponent(g);
  Graphics2D twoD=(Graphics2D)g;
  Rectangle2D rect=new Rectangle2D.Double(100,100,200,150);
  Ellipse2D ellipse=new Ellipse2D.Double(100,100,200,150);
  Ellipse2D circle=new Ellipse2D.Double(75,50,250,250);
  twoD.setPaint(Color.RED);
  twoD.drawString("Fill Color Demonstration",50,25);
  twoD.draw(rect);
  twoD.draw(ellipse);
  twoD.draw(circle); 
 }
}
class MyFrame extends JFrame
{
 public MyFrame()
 {
  MyPanel panel=new MyPanel();
  add(panel);
  panel.setBackground(Color.black);
  setTitle("MY FRAME");
  setSize(400,400);
  setVisible(true);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
/**
@author Sreekandan.K
*/
 public class DrawShapes
{
 public static void main(String args[])
 {
  MyFrame f=new MyFrame();
 }
}

FlowerDemo.java
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
class MyPanel extends JPanel
{
 public void paintComponent(Graphics g)
 {
  super.paintComponent(g);
  Graphics2D twoD=(Graphics2D)g;
  Ellipse2D c1=new Ellipse2D.Double(100,100,50,50);
  Ellipse2D c2=new Ellipse2D.Double(125,100,50,50);
  Ellipse2D c3=new Ellipse2D.Double(140,120,50,50);
  Ellipse2D c4=new Ellipse2D.Double(125,135,50,50);
  Ellipse2D c5=new Ellipse2D.Double(90,115,50,50);
  Ellipse2D c6=new Ellipse2D.Double(100,130,50,50);
  Ellipse2D c7=new Ellipse2D.Double(130,130,25,25);
  twoD.setPaint(Color.ORANGE);
  twoD.fill(c1);
  twoD.fill(c2);
  twoD.fill(c3);
  twoD.fill(c4);
  twoD.fill(c5);
  twoD.fill(c6);
  twoD.setPaint(Color.PINK);
  twoD.fill(c7);
  twoD.setPaint(Color.GREEN);
  Line2D l=new Line2D.Double(136,180,130,300);
  twoD.draw(l);
 }
}
class MyFrame extends JFrame
{
 public MyFrame()
 {
  MyPanel p=new MyPanel();
  p.setBackground(Color.BLACK);
  add(p);
  setSize(500,500);
  setVisible(true);
  setTitle("Image Demonstration - Author: Sreekandan.K");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
/**
@author Sreekandan.K
*/
public class FlowerDemo
{
 public static void main(String args[])
 {
  MyFrame f=new MyFrame();
 }

OUTPUT


ImageDemo.java
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;
class MyPanel extends JPanel
{
 public void paintComponent(Graphics g)
 {
  try
  {
   super.paintComponent(g);
   String fName1="C:/Program Files/Java/jdk1.5.0/bin/comp.jpg";
   File f1=new File(fName1);
   Image img1=ImageIO.read(f1);
   g.drawImage(img1,50,50,200,200,null);
  }
  catch(IOException e)
  {
   System.out.println(e);
  }
 }
}
class MyFrame extends JFrame
{
 public MyFrame()
 {
  MyPanel p=new MyPanel();
  add(p);
  p.setBackground(Color.WHITE);
  setSize(400,400);
  setVisible(true);
  setTitle("Image Demonstration - Author: Sreekandan.K");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
/**
@author Sreekandan.K
*/
public class ImageDemo
{
 public static void main(String args[])
 {
  MyFrame f=new MyFrame();
 }
}

OUTPUT























MouseEventDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyPanel extends JPanel
{
 JLabel la;
 public MyPanel()
 {
  la=new JLabel();
  add(la);
  MouseListener listener1=new MyListener1();
  addMouseListener(listener1);
  MouseMotionListener listener2=new MyListener2();
  addMouseMotionListener(listener2);
 }
 private class MyListener1 extends MouseAdapter
 {
  public void mouseClicked(MouseEvent e)
  {
   la.setText("Mouse Clicked on:("+e.getX()+","+e.getY()+"),Mouse Click Count="+e.getClickCount());
  }
  public void mousePressed(MouseEvent e)
  {
   la.setText("Mouse Pressed on:("+e.getX()+","+e.getY()+"),Mouse Click Count="+e.getClickCount());
  }
  public void mouseReleased(MouseEvent e)
  {
   la.setText("Mouse Released............ on:("+e.getX()+","+e.getY()+"),Mouse Click Count="+e.getClickCount());
  }
 }
 private class MyListener2 extends MouseMotionAdapter
 {
  public void mouseMoved(MouseEvent e)
  {
   la.setText("Mouse Moved on:("+e.getX()+","+e.getY()+")");
  }
 }
}
class MyFrame extends JFrame
{
 public MyFrame()
 {
  MyPanel panel=new MyPanel();
  add(panel);
  setSize(300,500);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setVisible(true);
 }
}
/**
@author Sreekandan.K
*/
public class MouseEventDemo
{
 public static void main(String args[])
 {
  MyFrame frame=new MyFrame();
 }
}

BorderLayoutDemo.java
import java.awt.*;
import javax.swing.*;
class MyPanel extends JPanel
{
 public MyPanel()
 {
  //setLayout(new BorderLayout());
  BorderLayout border=new BorderLayout();
  setLayout(border);
  JButton b1=new JButton("TOP");
  add(b1,BorderLayout.NORTH);
  JButton b2=new JButton("LEFT");
  add(b2,BorderLayout.WEST);
  JButton b3=new JButton("RIGHT");
  add(b3,BorderLayout.EAST);
  JButton b4=new JButton("BOTTOM");
  add(b4,BorderLayout.SOUTH);
  JButton b5=new JButton("CENTER");
  add(b5,BorderLayout.CENTER);
 }
}
class MyFrame extends JFrame
{
 public MyFrame()
 {
  MyPanel panel=new MyPanel();
  add(panel);
  setSize(300,300);
  setVisible(true);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
/**
@author Sreekandan.K
*/
public class BorderLayoutDemo
{
 public static void main(String args[])
 {
  MyFrame frame=new MyFrame();
 }
}

FlowLayoutDemo.java
import java.awt.*;
import javax.swing.*;
class MyPanel extends JPanel
{
 public MyPanel()
 {
  //setLayout(new FlowLayout());
  FlowLayout flow=new FlowLayout();
  setLayout(flow);
  JButton b1=new JButton("Button1");
  add(b1);
  JButton b2=new JButton("Button2");
  add(b2);
  JButton b3=new JButton("Button3");
  add(b3);
  JButton b4=new JButton("Button4");
  add(b4);
  JButton b5=new JButton("Button5");
  add(b5);
 }
}
class MyFrame extends JFrame
{
 public MyFrame()
 { 
  MyPanel panel=new MyPanel();
  add(panel);
  setSize(300,300);
  setVisible(true);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
/**
@author Sreekandan.K
*/
public class FlowLayoutDemo
{
 public static void main(String args[])
 {
  MyFrame frame=new MyFrame();
 }
}

GridBagLayoutDemo.java
import java.awt.*;
import javax.swing.*;
class MyPanel extends JPanel
{
 public MyPanel()
 {
  //setLayout(new GridBagLayout());
  GridBagLayout gb=new GridBagLayout();
  setLayout(gb);
  JButton b1=new JButton("Button1");
  add(b1);
  JButton b2=new JButton("Button2");
  add(b2);
  JButton b3=new JButton("Button3");
  add(b3);
  JButton b4=new JButton("Button4");
  add(b4);
  JButton b5=new JButton("Button5");
  add(b5);
 }
}
class MyFrame extends JFrame
{
 public MyFrame()
 {
  MyPanel panel=new MyPanel();
  add(panel);
  setSize(300,300);
  setVisible(true);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
/**
@author Sreekandan.K
*/
public class GridBagLayoutDemo
{
 public static void main(String args[])
 {
  MyFrame frame=new MyFrame();
 }
}

GridLayoutDemo.java
import java.awt.*;
import javax.swing.*;
class MyPanel extends JPanel
{
 public MyPanel()
 {
  //setLayout(new GridLayout(3,3));
  GridLayout grid=new GridLayout(3,3);
  setLayout(grid);
  JButton b1=new JButton("Button1");
  add(b1);
  JButton b2=new JButton("Button2");
  add(b2);
  JButton b3=new JButton("Button3");
  add(b3);
  JButton b4=new JButton("Button4");
  add(b4);
  JButton b5=new JButton("Button5");
  add(b5);
  JButton b6=new JButton("Button6");
  add(b6);
  JButton b7=new JButton("Button7");
  add(b7);
  JButton b8=new JButton("Button8");
  add(b8);
  JButton b9=new JButton("Button9");
  add(b9);
 }
}
class MyFrame extends JFrame
{
 public MyFrame()
 {
  MyPanel panel=new MyPanel();
  add(panel);
  setSize(300,300);
  setVisible(true);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
/**
@author Sreekandan.K
*/
public class GridLayoutDemo
{
 public static void main(String args[])
 {
  MyFrame frame=new MyFrame();
 }
}

ThreadDemo1.java
class ChildThread implements Runnable
{
 public void run()
 {
  try
  {
   for(int i=5;i>0;i--)
   {
    System.out.println("Child Thread:"+i);
    Thread.sleep(1000);
   }
  }
  catch(InterruptedException e)
  {
   System.out.println("Child Thread Interrupted...");
  }
  System.out.println("Child Thread Exiting...");
 }
}
/**
@author Sreekandan.K
*/
class ThreadDemo1
{
 public static void main(String args[])
 {
  //Runnable ct=new ChildThread();
  ChildThread ct=new ChildThread();
  Thread t=new Thread(ct);
  t.start();
  try
  {
   for(int i=5;i>0;i--)
   {
    System.out.println("Main Thread:"+i);
    Thread.sleep(2000);
   }
  }
  catch(InterruptedException e)
  {
   System.out.println("Main Thread Interrupted...");
  }
  System.out.println("Main Thread Exiting...");
 }
}

ThreadDemo2.java
class ChildThread implements Runnable
{
 public void run()
 {
  try
  {
   for(int i=5;i>0;i--)
   {
    System.out.println("Child Thread:"+i);
    Thread.sleep(1000);
   }
  }
  catch(InterruptedException e)
  {
   System.out.println("Child Thread Interrupted...");
  }
  System.out.println("Child Thread Exiting...");
 }
}
/**
@author Sreekandan.K
*/
class ThreadDemo2
{
 public static void main(String args[])
 {
  Runnable ct=new ChildThread();
  Thread t=new Thread(ct);
  t.start();
  try
  {
   for(int i=5;i>0;i--)
   {
    System.out.println("Main Thread:"+i);
    Thread.sleep(1000);
   }
  }
  catch(InterruptedException e)
  {
   System.out.println("Main Thread Interrupted...");
  }
  System.out.println("Main Thread Exiting...");
 }
}

ThreadDemo3.java
class ChildThread1 implements Runnable
{
 public void run()
 {
  try
  {
   for(int i=5;i>0;i--)
   {
    System.out.println("Child Thread1:"+i);
    Thread.sleep(1000);
   }
  }
  catch(InterruptedException e)
  {
   System.out.println("Child Thread1 Interrupted...");
  }
  System.out.println("Child Thread1 Exiting...");
 }
}
class ChildThread2 implements Runnable
{
 public void run()
 {
  try
  {
   for(int i=5;i>0;i--)
   {
    System.out.println("Child Thread2:"+i);
    Thread.sleep(2000);
   }
  }
  catch(InterruptedException e)
  {
   System.out.println("Child Thread2 Interrupted...");
  }
  System.out.println("Child Thread2 Exiting...");
 }
}
/**
@author Sreekandan.K
*/
class ThreadDemo3
{
 public static void main(String args[])
 {
  ChildThread1 ct1=new ChildThread1();
  Thread t1=new Thread(ct1);
  t1.start();
  ChildThread2 ct2=new ChildThread2();
  Thread t2=new Thread(ct2);
  t2.start();
  try
  {
   for(int i=5;i>0;i--)
   {
    System.out.println("Main Thread:"+i);
    Thread.sleep(3000);
   }
  }
  catch(InterruptedException e)
  {
   System.out.println("Main Thread Interrupted...");
  }
  System.out.println("Main Thread Exiting...");
 }
}

ThreadDemo4.java
class ChildThread extends Thread
{
 public void run()
 {
  try
  {
   for(int i=5;i>0;i--)
   {
    System.out.println("Child Thread:"+i);
    Thread.sleep(1000);
   }
  }
  catch(InterruptedException e)
  {
   System.out.println("Child Thread Interrupted...");
  }
  System.out.println("Child Thread Exiting...");
 }
}
/**
@author Sreekandan.K
*/
class ThreadDemo4
{
 public static void main(String args[])
 {
  ChildThread ct=new ChildThread();
  Thread t=new Thread(ct);
  t.start();
  try
  {
   for(int i=5;i>0;i--)
   {
    System.out.println("Main Thread:"+i);
    Thread.sleep(2000);
   }
  }
  catch(InterruptedException e)
  {
   System.out.println("Main Thread Interrupted...");
  }
  System.out.println("Main Thread Exiting...");
 }

No comments:

Post a Comment

Leave the comments