Tuesday, April 29, 2014
Friday, April 4, 2014
C Program for DFS(Depth first search)
/*Program to perform depth first traversal on a graph */
#include <stdio.h>
int v , adj[10][10] , visited[10] ;
/* Assuming maximum 9 vertices. */
void dfs(int k);
void main()
{
int i,j;
clrscr();
/* Creating graph of v vertices */
printf("Enter the number of vertices in the graph: ");
scanf("%d",&v);
for(i=1 ; i<=v ; i++)
for(j=1 ; j<=v ; j++)
{
printf("Is vertex %d adjacent to vertex %d ? ",j,i);
printf("Enter 1 if yes and 0 if no. ");
scanf("%d",&adj[i][j]);
}
/* Making all the vertices as unvisited */
for(i=1;i<=v;i++)
visited[i]=0;
printf("Depth first Search of the graph is as shown:\n");
for(i=1;i<=v;i++)
if(visited[i]==0)
dfs(i);
/* Initiating dfs from vertex i if it is not visited */
getch();
}
void dfs(int k)
{
int j;
visited[k]=1; /* Visiting vertex k */
printf("%d ",k);
for(j=1; j<=v ; j++)
if(adj[k][j] == 1)
if(visited[j] ==0)
dfs(j); /* Initiating dfs from vertex j */
}
/* The first if checks whether vertex j is adjacent to vertex k
The second if checks whether j is visited or not */
#include <stdio.h>
int v , adj[10][10] , visited[10] ;
/* Assuming maximum 9 vertices. */
void dfs(int k);
void main()
{
int i,j;
clrscr();
/* Creating graph of v vertices */
printf("Enter the number of vertices in the graph: ");
scanf("%d",&v);
for(i=1 ; i<=v ; i++)
for(j=1 ; j<=v ; j++)
{
printf("Is vertex %d adjacent to vertex %d ? ",j,i);
printf("Enter 1 if yes and 0 if no. ");
scanf("%d",&adj[i][j]);
}
/* Making all the vertices as unvisited */
for(i=1;i<=v;i++)
visited[i]=0;
printf("Depth first Search of the graph is as shown:\n");
for(i=1;i<=v;i++)
if(visited[i]==0)
dfs(i);
/* Initiating dfs from vertex i if it is not visited */
getch();
}
void dfs(int k)
{
int j;
visited[k]=1; /* Visiting vertex k */
printf("%d ",k);
for(j=1; j<=v ; j++)
if(adj[k][j] == 1)
if(visited[j] ==0)
dfs(j); /* Initiating dfs from vertex j */
}
/* The first if checks whether vertex j is adjacent to vertex k
The second if checks whether j is visited or not */
Advanced Java program to Display IP
/*import java.net.*;
public class ShowIP
{
public static void main(String[] args)
{
try
{
InetAddress ad = InetAddress.getLocalHost();
int h = ad.hashCode();
String c = ad.getCanonicalHostName();
String a = ad.getHostAddress();
String n = ad.getHostName();
System.out.println (ad.getHostAddress());
System.out.println (ad.getHostName());
System.out.println ("home-7077aa2ac2" + "'s IP is "+ad.getHostAddress());
System.out.println (ad +"" +h +""+c +"\n" +a +"" +n);
}
catch (UnknownHostException ex)
{
System.out.println (ex);
}
}
}
class pwd {
public static void main(String[] args) {
String curDir = System.getProperty("user.dir");
System.out.println (curDir);
}
}*/
import java.net.InetAddress;
class ShowIP
{
public static void main(String[] args)
{
try
{
InetAddress ad;
ad = InetAddress.getByName("home-7077aa2ac2");
System.out.println (ad);
ad = InetAddress.getLocalHost();
System.out.println (ad);
System.out.println ("\n\n");
System.out.println (ad.getAddress()+"\n"+ad.getCanonicalHostName()+"\n"+
ad.getHostAddress()+"\n"+ad.getHostName()+"\n"+
ad.getLocalHost()+"\n"+ad.isMulticastAddress()+"\n"+
ad.toString());
byte [] b;
b = ad.getAddress();
System.out.println (b);
String str ="";
for (int i = 0; i<b.length; i++)
{
if (i>0)
{
str += ".";
}
str += b[i];
}
System.out.println (str);
}
catch (Exception ex)
{
System.out.println (ex);
}
}
}
public class ShowIP
{
public static void main(String[] args)
{
try
{
InetAddress ad = InetAddress.getLocalHost();
int h = ad.hashCode();
String c = ad.getCanonicalHostName();
String a = ad.getHostAddress();
String n = ad.getHostName();
System.out.println (ad.getHostAddress());
System.out.println (ad.getHostName());
System.out.println ("home-7077aa2ac2" + "'s IP is "+ad.getHostAddress());
System.out.println (ad +"" +h +""+c +"\n" +a +"" +n);
}
catch (UnknownHostException ex)
{
System.out.println (ex);
}
}
}
class pwd {
public static void main(String[] args) {
String curDir = System.getProperty("user.dir");
System.out.println (curDir);
}
}*/
import java.net.InetAddress;
class ShowIP
{
public static void main(String[] args)
{
try
{
InetAddress ad;
ad = InetAddress.getByName("home-7077aa2ac2");
System.out.println (ad);
ad = InetAddress.getLocalHost();
System.out.println (ad);
System.out.println ("\n\n");
System.out.println (ad.getAddress()+"\n"+ad.getCanonicalHostName()+"\n"+
ad.getHostAddress()+"\n"+ad.getHostName()+"\n"+
ad.getLocalHost()+"\n"+ad.isMulticastAddress()+"\n"+
ad.toString());
byte [] b;
b = ad.getAddress();
System.out.println (b);
String str ="";
for (int i = 0; i<b.length; i++)
{
if (i>0)
{
str += ".";
}
str += b[i];
}
System.out.println (str);
}
catch (Exception ex)
{
System.out.println (ex);
}
}
}
Thursday, April 3, 2014
java program for calender
import java.util.*;
class MCalendar
{
public static void main(String [] args)
{
// StringCharacterIterator ad = new StringCharacterIterator();
GregorianCalendar cal = new GregorianCalendar();
System.out.println (cal.getTime());
System.out.println (cal.getActualMaximum(Calendar.YEAR));
System.out.println (cal.isLeapYear(Calendar.YEAR));
System.out.println (cal.getTime());
System.out.println (cal.get(Calendar.HOUR));
Random r = new Random();
System.out.println (r.nextInt(44));
System.out.println (r.nextInt(44));
System.out.println (r.nextInt(44));
System.out.println (r.nextInt(44));
System.out.println ("\n\n");
System.out.println (r.nextInt(4));
System.out.println (r.nextInt(4));
System.out.println (r.nextInt(4));
System.out.println (r.nextInt(4));
}
}
class MCalendar
{
public static void main(String [] args)
{
// StringCharacterIterator ad = new StringCharacterIterator();
GregorianCalendar cal = new GregorianCalendar();
System.out.println (cal.getTime());
System.out.println (cal.getActualMaximum(Calendar.YEAR));
System.out.println (cal.isLeapYear(Calendar.YEAR));
System.out.println (cal.getTime());
System.out.println (cal.get(Calendar.HOUR));
Random r = new Random();
System.out.println (r.nextInt(44));
System.out.println (r.nextInt(44));
System.out.println (r.nextInt(44));
System.out.println (r.nextInt(44));
System.out.println ("\n\n");
System.out.println (r.nextInt(4));
System.out.println (r.nextInt(4));
System.out.println (r.nextInt(4));
System.out.println (r.nextInt(4));
}
}
Java program for zero one diamond pattern
import java.*;
class ZeroOneDiamond
{
public static void main(String[] args)
{
int n,i,j,k;
if(args.length>0)
{
for(i=0;i<args.length;i++)
{
try
{
n=Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.out.print("\n\nThis is not a valid Integer,\nSo considering it as 0 \n\n");
n=0;
}
if(n<0) n=-n;
for(i=0;i<n;i++)
{
for(j=0;j<40-i;j++)
{
System.out.print(" ");
}
for(k=0;k<=i;k++)
{
if(k%2==0)
{
System.out.print(1+" ");
}
else
{
System.out.print(0+" ");
}
}
System.out.print("\n");
}
for(i=n-1;i>0;i--)
{
for(j=40-i;j>=0;j--)
{
System.out.print(" ");
}
for(k=i;k>0;k--)
{
if(k%2==0)
{
System.out.print(1+" ");
}
else
{
System.out.print(0+" ");
}
}
System.out.print("\n");
}
}
}
}
}
class ZeroOneDiamond
{
public static void main(String[] args)
{
int n,i,j,k;
if(args.length>0)
{
for(i=0;i<args.length;i++)
{
try
{
n=Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.out.print("\n\nThis is not a valid Integer,\nSo considering it as 0 \n\n");
n=0;
}
if(n<0) n=-n;
for(i=0;i<n;i++)
{
for(j=0;j<40-i;j++)
{
System.out.print(" ");
}
for(k=0;k<=i;k++)
{
if(k%2==0)
{
System.out.print(1+" ");
}
else
{
System.out.print(0+" ");
}
}
System.out.print("\n");
}
for(i=n-1;i>0;i--)
{
for(j=40-i;j>=0;j--)
{
System.out.print(" ");
}
for(k=i;k>0;k--)
{
if(k%2==0)
{
System.out.print(1+" ");
}
else
{
System.out.print(0+" ");
}
}
System.out.print("\n");
}
}
}
}
}
Java program on swing package
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MyJFrame extends JFrame
{
JLabel jl1=new JLabel("Name");
JButton jb1=new JButton("Click Now");
JTextField jt1=new JTextField("Type Here",20);
JPanel pnl=new JPanel();
JPanel pnl2=new JPanel();
Container c;
public MyJFrame(String title)
{
super(title);
c=getContentPane();
setSize(300,200);
pnl.add(jl1);
pnl.add(jt1);
pnl2.add(jb1);
c.add("North",pnl);
c.add("South",pnl2);
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb1)
{
jb1.setText("Your Name is :"+jt1.getText());
//jb1.invalidate();
jt1.setText(jb1.getText());
}
}
});
}
public static void main(String[] args)
{
JFrame j=new MyJFrame(args[0]);
j.show();
j.setVisible(true);
}
}
}
import javax.swing.*;
import java.awt.event.*;
class MyJFrame extends JFrame
{
JLabel jl1=new JLabel("Name");
JButton jb1=new JButton("Click Now");
JTextField jt1=new JTextField("Type Here",20);
JPanel pnl=new JPanel();
JPanel pnl2=new JPanel();
Container c;
public MyJFrame(String title)
{
super(title);
c=getContentPane();
setSize(300,200);
pnl.add(jl1);
pnl.add(jt1);
pnl2.add(jb1);
c.add("North",pnl);
c.add("South",pnl2);
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb1)
{
jb1.setText("Your Name is :"+jt1.getText());
//jb1.invalidate();
jt1.setText(jb1.getText());
}
}
});
}
public static void main(String[] args)
{
JFrame j=new MyJFrame(args[0]);
j.show();
j.setVisible(true);
}
}
}
Java program to demonstrate swing progress bar
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class SwingProgress extends JFrame implements ActionListener
{
JProgressBar jp;
JButton jb;
JPanel jp1,jp2;
public SwingProgress()
{
jp1=new JPanel();
jp2=new JPanel();
jp=new JProgressBar(0,-5,5);
jb=new JButton("Increment the Progress");
jp1.add(jp);
jp2.add(jb);
this.add(jp1);
this.add(jp2);
jb.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
}
public static void main(String[] args)
{
SwingProgress s=new SwingProgress();
s.setSize(300,200);
s.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
class SwingProgress extends JFrame implements ActionListener
{
JProgressBar jp;
JButton jb;
JPanel jp1,jp2;
public SwingProgress()
{
jp1=new JPanel();
jp2=new JPanel();
jp=new JProgressBar(0,-5,5);
jb=new JButton("Increment the Progress");
jp1.add(jp);
jp2.add(jb);
this.add(jp1);
this.add(jp2);
jb.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
}
public static void main(String[] args)
{
SwingProgress s=new SwingProgress();
s.setSize(300,200);
s.setVisible(true);
}
}
Wednesday, April 2, 2014
Java program for Maximum and Minimum number
import java.lang.*;
import java.io.*;
import java.util.*;
class ConsoleIO
{
private static String line="";
private static BufferedReader br= new BufferedReader (new InputStreamReader(System.in));
private static StringTokenizer toks=new StringTokenizer(line);
public static int readInt() throws NumberFormatException, IOException
{
if(!toks.hasMoreTokens())
{
line=br.readLine();
toks=new StringTokenizer(line);
}
return Integer.parseInt(toks.nextToken());
}
}
class ReadASetOfIntegers extends Object
{
public static void main(String[] args) throws IOException
{
int i,sum,n,max=-1,min=-1;
System.out.print("\r\nInput the size \r\n");
n=ConsoleIO.readInt();
int[] a=new int[n];
System.out.print("\nEnter "+n+" integers, to Calculate Average.....\r\n");
for(i=0;i<n;i++)
{
a[i]=ConsoleIO.readInt();
if (i==0)
{
max=a[i];
min=a[i];
}
else
if(a[i]>max)
{
max=a[i];
}
else
if (a[i]<min)
{
min=a[i];
}
}
System.out.print("\n\nThe Maximum of all the " +n+" Numbers is : "+ max+ "\n\nThe minimum of all the " +n+" Numbers is : "+ min+"\n\n");
}
}
import java.io.*;
import java.util.*;
class ConsoleIO
{
private static String line="";
private static BufferedReader br= new BufferedReader (new InputStreamReader(System.in));
private static StringTokenizer toks=new StringTokenizer(line);
public static int readInt() throws NumberFormatException, IOException
{
if(!toks.hasMoreTokens())
{
line=br.readLine();
toks=new StringTokenizer(line);
}
return Integer.parseInt(toks.nextToken());
}
}
class ReadASetOfIntegers extends Object
{
public static void main(String[] args) throws IOException
{
int i,sum,n,max=-1,min=-1;
System.out.print("\r\nInput the size \r\n");
n=ConsoleIO.readInt();
int[] a=new int[n];
System.out.print("\nEnter "+n+" integers, to Calculate Average.....\r\n");
for(i=0;i<n;i++)
{
a[i]=ConsoleIO.readInt();
if (i==0)
{
max=a[i];
min=a[i];
}
else
if(a[i]>max)
{
max=a[i];
}
else
if (a[i]<min)
{
min=a[i];
}
}
System.out.print("\n\nThe Maximum of all the " +n+" Numbers is : "+ max+ "\n\nThe minimum of all the " +n+" Numbers is : "+ min+"\n\n");
}
}
Java applet program for List Box
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class listbox extends Applet
{
List lst = new List();
public listbox()
{
lst.add("Mercury");
lst.add("Venus");
lst.add("Earth");
lst.add("JavaSoft");
lst.add("Mars");
lst.add("Jupiter");
lst.add("Saturn");
lst.add("Uranus");
lst.add("Neptune");
lst.add("Pluto");
lst.setSize(300,200);
add(lst);
}
}
import java.applet.*;
import java.awt.event.*;
public class listbox extends Applet
{
List lst = new List();
public listbox()
{
lst.add("Mercury");
lst.add("Venus");
lst.add("Earth");
lst.add("JavaSoft");
lst.add("Mars");
lst.add("Jupiter");
lst.add("Saturn");
lst.add("Uranus");
lst.add("Neptune");
lst.add("Pluto");
lst.setSize(300,200);
add(lst);
}
}
Java program to display current time
import mypackages.ConsoleIO.ConsoleIO;
import java.io.*;
class CurrentTime
{
public static void main(String [] srgs)
{
long time;
String s1="",s2="",s3="";
System.out.print("Enter your full name : ");
try
{
s1=ConsoleIO.readString();
s2=ConsoleIO.readString();
s3=ConsoleIO.readString();
}
catch(IOException ioe)
{
}
s1 = s1.toUpperCase();
s2.toUpperCase();
s3.toUpperCase();
System.out.println("Your first name is : "+s1);
System.out.println("Your middle name is : "+s2);
System.out.println("Your last name is : "+s3);
time=System.currentTimeMillis();
System.out.println("Current time is :- "+(int)time/3600000+":"+(int)time/60000+":"+(int)time/1000);
}
}
import java.io.*;
class CurrentTime
{
public static void main(String [] srgs)
{
long time;
String s1="",s2="",s3="";
System.out.print("Enter your full name : ");
try
{
s1=ConsoleIO.readString();
s2=ConsoleIO.readString();
s3=ConsoleIO.readString();
}
catch(IOException ioe)
{
}
s1 = s1.toUpperCase();
s2.toUpperCase();
s3.toUpperCase();
System.out.println("Your first name is : "+s1);
System.out.println("Your middle name is : "+s2);
System.out.println("Your last name is : "+s3);
time=System.currentTimeMillis();
System.out.println("Current time is :- "+(int)time/3600000+":"+(int)time/60000+":"+(int)time/1000);
}
}
Java program for queue
import java.util.*;
class Queue
{
private LinkedList l=new LinkedList();
public void insert(Object o)
{
l.add(o);
}
public int size()
{
return l.size();
}
public Object remove()
{
try{
return l.remove();
}catch(NoSuchElementException n)
{
System.out.print("\nQueue is Empty ");
}
return null;
}
public Object[] display()
{
Object[] a=new Object[l.size()];
for(int i=0;i<l.size();i++)
a[i]=l.get(i);
return a;
}
}
class test
{
public static void main(String[] args)
{
Queue q=new Queue();
Object[] a;
q.insert(new Integer(1));
q.insert(new Float(1.5));
q.insert("rajesh");
System.out.println("\nThe Size of Queue : "+q.size()+"\n\n");
a=q.display();
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
System.out.print("\n\n");
for(int j=0;j<3;j++)
System.out.println(q.remove());
System.out.println("\nThe Size of Queue : "+q.size()+"\n\n");
System.out.println(q.remove());
a=q.display();
System.out.print("\n\n");
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
class Queue
{
private LinkedList l=new LinkedList();
public void insert(Object o)
{
l.add(o);
}
public int size()
{
return l.size();
}
public Object remove()
{
try{
return l.remove();
}catch(NoSuchElementException n)
{
System.out.print("\nQueue is Empty ");
}
return null;
}
public Object[] display()
{
Object[] a=new Object[l.size()];
for(int i=0;i<l.size();i++)
a[i]=l.get(i);
return a;
}
}
class test
{
public static void main(String[] args)
{
Queue q=new Queue();
Object[] a;
q.insert(new Integer(1));
q.insert(new Float(1.5));
q.insert("rajesh");
System.out.println("\nThe Size of Queue : "+q.size()+"\n\n");
a=q.display();
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
System.out.print("\n\n");
for(int j=0;j<3;j++)
System.out.println(q.remove());
System.out.println("\nThe Size of Queue : "+q.size()+"\n\n");
System.out.println(q.remove());
a=q.display();
System.out.print("\n\n");
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
Advanced java program on swing
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MyJFrame extends JFrame
{
JLabel jl1=new JLabel("Name");
JButton jb1=new JButton("Click Now");
JTextField jt1=new JTextField("Type Here",20);
JPanel pnl=new JPanel();
JPanel pnl2=new JPanel();
Container c;
public MyJFrame(String title)
{
super(title);
c=getContentPane();
setSize(300,200);
pnl.add(jl1);
pnl.add(jt1);
pnl2.add(jb1);
c.add("North",pnl);
c.add("South",pnl2);
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb1)
{
jb1.setText("Your Name is :"+jt1.getText());
//jb1.invalidate();
jt1.setText(jb1.getText());
}
}
});
}
public static void main(String[] args)
{
JFrame j=new MyJFrame(args[0]);
j.show();
j.setVisible(true);
}
}
import javax.swing.*;
import java.awt.event.*;
class MyJFrame extends JFrame
{
JLabel jl1=new JLabel("Name");
JButton jb1=new JButton("Click Now");
JTextField jt1=new JTextField("Type Here",20);
JPanel pnl=new JPanel();
JPanel pnl2=new JPanel();
Container c;
public MyJFrame(String title)
{
super(title);
c=getContentPane();
setSize(300,200);
pnl.add(jl1);
pnl.add(jt1);
pnl2.add(jb1);
c.add("North",pnl);
c.add("South",pnl2);
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb1)
{
jb1.setText("Your Name is :"+jt1.getText());
//jb1.invalidate();
jt1.setText(jb1.getText());
}
}
});
}
public static void main(String[] args)
{
JFrame j=new MyJFrame(args[0]);
j.show();
j.setVisible(true);
}
}
Java program array using ArrayList class and using size(), add(), remove() method insert an elements in array and remove elements get the size of array and finally print the array to the screen
/*create an array using ArrayList class and using size(), add(), remove() method
insert an elements in array and remove elements get the size of array
and finally print the array to the screen.*/
import java.util.*;
class Aray
{
public static void main(String[] args)
{
String s="This";
int c=1,d=1;
ArrayList a=new ArrayList();
System.out.println("THe Current SiZe : "+a.size());
System.out.println("\nAll 10 elements : \n");
for(int i=0;i<10;i++)
{
d=d*2;
a.add(i,d);
}
System.out.println("THe Current SiZe : "+a.size());
for(int i=0;i<10;i++)
System.out.println(a.get(i)+"\n");
System.out.println("\nNow Removing Botton 5 elements : \n");
try{
for(int i=5;i<10;i++)
a.remove(i);
}
catch(IndexOutOfBoundsException ab)
{
}
a.trimToSize();
System.out.println("THe Current SiZe : "+a.size());
for(int i=0;i<5;i++)
System.out.println(a.get(i)+"\n");
for(int i=5;i<10;i++)
{
a.add(i,s);
s=s+" is";
}
for(int i=0;i<10;i++)
System.out.println(a.get(i)+"\n");
System.out.println("The Index of 32 : "+a.indexOf(32));
System.out.println("THe Current SiZe : "+a.size());
}
}
insert an elements in array and remove elements get the size of array
and finally print the array to the screen.*/
import java.util.*;
class Aray
{
public static void main(String[] args)
{
String s="This";
int c=1,d=1;
ArrayList a=new ArrayList();
System.out.println("THe Current SiZe : "+a.size());
System.out.println("\nAll 10 elements : \n");
for(int i=0;i<10;i++)
{
d=d*2;
a.add(i,d);
}
System.out.println("THe Current SiZe : "+a.size());
for(int i=0;i<10;i++)
System.out.println(a.get(i)+"\n");
System.out.println("\nNow Removing Botton 5 elements : \n");
try{
for(int i=5;i<10;i++)
a.remove(i);
}
catch(IndexOutOfBoundsException ab)
{
}
a.trimToSize();
System.out.println("THe Current SiZe : "+a.size());
for(int i=0;i<5;i++)
System.out.println(a.get(i)+"\n");
for(int i=5;i<10;i++)
{
a.add(i,s);
s=s+" is";
}
for(int i=0;i<10;i++)
System.out.println(a.get(i)+"\n");
System.out.println("The Index of 32 : "+a.indexOf(32));
System.out.println("THe Current SiZe : "+a.size());
}
}
Java program on armstrong number
import java.*;
class Armstrong
{
public static void main(String[] args)
{
int n=0,m=0,sum=0,i,r;
if(args.length>0)
{
for(i=0;i<args.length;i++)
{
try
{
n=Integer.parseInt(args[i]);
m=Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.out.print("\n\nThis "+args[i]+" is not a valid Integer, so considering it 0\n");
n=0;
m=0;
}
while(n!=0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
if(sum==m)
{
System.out.print("\n\nYes, This "+m+" is Armstrong no.\n\n");
}
else
{
System.out.print("\n\nNo, This "+m+" is not an Armstrong no.\n\n");
}
}
}
}
}
class Armstrong
{
public static void main(String[] args)
{
int n=0,m=0,sum=0,i,r;
if(args.length>0)
{
for(i=0;i<args.length;i++)
{
try
{
n=Integer.parseInt(args[i]);
m=Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.out.print("\n\nThis "+args[i]+" is not a valid Integer, so considering it 0\n");
n=0;
m=0;
}
while(n!=0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
if(sum==m)
{
System.out.print("\n\nYes, This "+m+" is Armstrong no.\n\n");
}
else
{
System.out.print("\n\nNo, This "+m+" is not an Armstrong no.\n\n");
}
}
}
}
}
Java program to demonstrate multiple try catch block
import java.io.*;
import allpackage.stack.ConsoleIO;
class Eligibility
{
public static void main(String[] args)
{
int maths,physics,chemistry,sum,total;
System.out.print("\nEnter Marks in Maths : ");
try{
maths=ConsoleIO.readInt();
}
catch(IOException ioe)
{
System.out.println("Due to mal functioning Exception");
maths=0;
}
catch(NumberFormatException nfe)
{
maths=0;
}
System.out.print("\nEnter Marks in physics : ");
try{
physics=ConsoleIO.readInt();
}
catch(IOException ioe)
{
System.out.println("Due to mal functioning Exception");
physics=0;
}
catch(NumberFormatException nfe)
{
physics=0;
}
System.out.print("\nEnter Marks in chemistry : ");
try{
chemistry=ConsoleIO.readInt();
}
catch(IOException ioe)
{
System.out.println("Due to mal functioning Exception");
chemistry=0;
}
catch(NumberFormatException nfe)
{
chemistry=0;
}
sum=maths+physics;
total=sum+chemistry;
if(maths>=60 && physics>=50 && chemistry>=40 && (total>=200 || sum>=150))
System.out.println("~ ELIGIBLE ~");
else
System.out.println("~ NOT ELIGIBLE ~");
}
}
import allpackage.stack.ConsoleIO;
class Eligibility
{
public static void main(String[] args)
{
int maths,physics,chemistry,sum,total;
System.out.print("\nEnter Marks in Maths : ");
try{
maths=ConsoleIO.readInt();
}
catch(IOException ioe)
{
System.out.println("Due to mal functioning Exception");
maths=0;
}
catch(NumberFormatException nfe)
{
maths=0;
}
System.out.print("\nEnter Marks in physics : ");
try{
physics=ConsoleIO.readInt();
}
catch(IOException ioe)
{
System.out.println("Due to mal functioning Exception");
physics=0;
}
catch(NumberFormatException nfe)
{
physics=0;
}
System.out.print("\nEnter Marks in chemistry : ");
try{
chemistry=ConsoleIO.readInt();
}
catch(IOException ioe)
{
System.out.println("Due to mal functioning Exception");
chemistry=0;
}
catch(NumberFormatException nfe)
{
chemistry=0;
}
sum=maths+physics;
total=sum+chemistry;
if(maths>=60 && physics>=50 && chemistry>=40 && (total>=200 || sum>=150))
System.out.println("~ ELIGIBLE ~");
else
System.out.println("~ NOT ELIGIBLE ~");
}
}
Java program for even odd number using try catch block
import java.*;
class EvenOdd
{
public static void main(String[] args)
{
int a,i;
if(args.length>0)
{
for(i=0;i<args.length;i++)
{
try
{
a=Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.out.print("\n\nThis is not a valid Integer,\nSo considering it as 0 \n\n");
a=0;
}
if(a%2==0)
{
System.out.print("\n"+a+" -------> Even\n");
}
else
{
System.out.print("\n"+a+" -------> Odd\n");
}
}
}
}
}
class EvenOdd
{
public static void main(String[] args)
{
int a,i;
if(args.length>0)
{
for(i=0;i<args.length;i++)
{
try
{
a=Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.out.print("\n\nThis is not a valid Integer,\nSo considering it as 0 \n\n");
a=0;
}
if(a%2==0)
{
System.out.print("\n"+a+" -------> Even\n");
}
else
{
System.out.print("\n"+a+" -------> Odd\n");
}
}
}
}
}
Java program to count number of words
import java.lang.*;
import java.io.*;
import java.util.*;
class WordCount
{
private static String line="";
private static BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
private static StringTokenizer toks=new StringTokenizer(line);
public static void main(String[] args) throws Exception
{
int cnt=0;
line=br.readLine();
toks=new StringTokenizer(line);
while(toks.hasMoreTokens())
{
toks.nextToken();
cnt++;
}
System.out.println("\n\nThe Number of Words in the line was = "+cnt+"\n\n");
}
}
import java.io.*;
import java.util.*;
class WordCount
{
private static String line="";
private static BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
private static StringTokenizer toks=new StringTokenizer(line);
public static void main(String[] args) throws Exception
{
int cnt=0;
line=br.readLine();
toks=new StringTokenizer(line);
while(toks.hasMoreTokens())
{
toks.nextToken();
cnt++;
}
System.out.println("\n\nThe Number of Words in the line was = "+cnt+"\n\n");
}
}
Java program of student using "this" keyword
package mypackages.student;
import java.io.*;
public class Student implements Serializable
{
private int rn=0;
private String name="";
private String dnm="";
private String loc="";
public Student()
{
}
public Student(int rn,String name)
{
this.name=name;
this.rn=rn;
}
public void setRn(int rn)
{
this.rn = rn;
}
public void setDnm(String dnm)
{
this.dnm = dnm;
}
public void setName(String name)
{
this.name = name;
}
public int getRn()
{
return rn;
}
public String getDnm()
{
return dnm;
}
public String getName()
{
return name;
}
public String getLoc()
{
return loc;
}
public void setLoc(String loc)
{
this.loc = loc;
}
public String toString()
{
String str = "";
str += "\nRoll No : " +this.rn ;
str += "\nName : " +this.name;
return str;
}
}
import java.io.*;
public class Student implements Serializable
{
private int rn=0;
private String name="";
private String dnm="";
private String loc="";
public Student()
{
}
public Student(int rn,String name)
{
this.name=name;
this.rn=rn;
}
public void setRn(int rn)
{
this.rn = rn;
}
public void setDnm(String dnm)
{
this.dnm = dnm;
}
public void setName(String name)
{
this.name = name;
}
public int getRn()
{
return rn;
}
public String getDnm()
{
return dnm;
}
public String getName()
{
return name;
}
public String getLoc()
{
return loc;
}
public void setLoc(String loc)
{
this.loc = loc;
}
public String toString()
{
String str = "";
str += "\nRoll No : " +this.rn ;
str += "\nName : " +this.name;
return str;
}
}
Java program for Stop Clock
class StopClock extends Thread
{
private int hh=0;
private int mm=0;
private int ss=0;
public int getHr()
{
return hh;
}
public int getMn()
{
return mm;
}
public int getSs()
{
return ss;
}
public void run()
{
for(;;)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
}
ss++;
mm+=ss/60;
ss%=60;
hh+=mm/60;
mm%=60;
hh%=24;
}
}
}
{
private int hh=0;
private int mm=0;
private int ss=0;
public int getHr()
{
return hh;
}
public int getMn()
{
return mm;
}
public int getSs()
{
return ss;
}
public void run()
{
for(;;)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
}
ss++;
mm+=ss/60;
ss%=60;
hh+=mm/60;
mm%=60;
hh%=24;
}
}
}
Java program for string reverse
import java.*;
class Reverse
{
public static void main(String[] args)
{
for(int i=0;i<args.length;i++)
{
System.out.print("\n\nThe Reversed String of * "+args[i]+" * is : ");
for(int j=args[i].length()-1;j>=0;j--)
{
System.out.print(args[i].charAt(j));
}
System.out.print("\n\n");
}
}
}
class Reverse
{
public static void main(String[] args)
{
for(int i=0;i<args.length;i++)
{
System.out.print("\n\nThe Reversed String of * "+args[i]+" * is : ");
for(int j=args[i].length()-1;j>=0;j--)
{
System.out.print(args[i].charAt(j));
}
System.out.print("\n\n");
}
}
}
Java program for pyramid pattern in java
import java.*;
class ZeroOneDiamond
{
public static void main(String[] args)
{
int n,i,j,k;
if(args.length>0)
{
for(i=0;i<args.length;i++)
{
try
{
n=Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.out.print("\n\nThis is not a valid Integer,\nSo considering it as 0 \n\n");
n=0;
}
if(n<0) n=-n;
for(i=0;i<n;i++)
{
for(j=0;j<40-i;j++)
{
System.out.print(" ");
}
for(k=0;k<=i;k++)
{
if(k%2==0)
{
System.out.print(1+" ");
}
else
{
System.out.print(0+" ");
}
}
System.out.print("\n");
}
for(i=n-1;i>0;i--)
{
for(j=40-i;j>=0;j--)
{
System.out.print(" ");
}
for(k=i;k>0;k--)
{
if(k%2==0)
{
System.out.print(1+" ");
}
else
{
System.out.print(0+" ");
}
}
System.out.print("\n");
}
}
}
}
}
class ZeroOneDiamond
{
public static void main(String[] args)
{
int n,i,j,k;
if(args.length>0)
{
for(i=0;i<args.length;i++)
{
try
{
n=Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.out.print("\n\nThis is not a valid Integer,\nSo considering it as 0 \n\n");
n=0;
}
if(n<0) n=-n;
for(i=0;i<n;i++)
{
for(j=0;j<40-i;j++)
{
System.out.print(" ");
}
for(k=0;k<=i;k++)
{
if(k%2==0)
{
System.out.print(1+" ");
}
else
{
System.out.print(0+" ");
}
}
System.out.print("\n");
}
for(i=n-1;i>0;i--)
{
for(j=40-i;j>=0;j--)
{
System.out.print(" ");
}
for(k=i;k>0;k--)
{
if(k%2==0)
{
System.out.print(1+" ");
}
else
{
System.out.print(0+" ");
}
}
System.out.print("\n");
}
}
}
}
}
Java Applet program for encryption and Decryption
package mypackages.encryption;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Encryption extends JFrame implements ActionListener
{
FileInputStream fis=null;
FileOutputStream fos=null;
File f=null;
int n = 0,x = 0;
private JPanel pnl = null;
private JTextField tf=null;
private JFileChooser jf=null;
private JButton openbtn=null;
private JButton enc=null;
private JButton denc=null;
public Encryption()
{
super("Do What You Want !!!!!!!");
pnl = new JPanel();
tf = new JTextField(15);
jf = new JFileChooser("F:/BCA/Semester-III/Java/Exercise-1");
openbtn = new JButton(".......");
enc = new JButton("Encrypt");
denc = new JButton("Dencrypt");
tf.setEnabled(false);
tf.setToolTipText("Selected File");
openbtn.setToolTipText("Browse");
enc.setToolTipText("Click to encrypt");
denc.setToolTipText("Click to dencrypt");
openbtn.addActionListener(this);
enc.addActionListener(this);
denc.addActionListener(this);
pnl.add(tf);
pnl.add(openbtn);
pnl.add(enc);
pnl.add(denc);
this.add(pnl);
this.setSize(300,100);
this.setVisible(true);
this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==openbtn)
{
jf.showOpenDialog(this);
tf.setText(jf.getSelectedFile().getAbsolutePath());
}
if (ae.getSource()==enc)
{
if(tf.getText().length()!=0)
this.encrpt(tf.getText());
else
JOptionPane.showMessageDialog(this,"First select the file.");
}
if (ae.getSource()==denc)
{
if(tf.getText().length()!=0)
this.decrpt(tf.getText());
else
JOptionPane.showMessageDialog(this,"First select the file.");
}
}
private boolean requery(int digit,String filename)
{
boolean done = false;
try
{
f = new File("mypackages/encryption/tempfile.html");
fis = new FileInputStream(filename);
fos = new FileOutputStream(f);
n = 0;
x = fis.read();
if(digit == 1)
digit = x;
else
if(digit == 0)
digit = -x;
fos.write(x);
n++;
x = fis.read();
while (x!=-1)
{
n++;
fos.write(x+digit);
x = fis.read();
}
fis = new FileInputStream(f);
fos = new FileOutputStream(filename);
x = fis.read();
while (x!=-1)
{
fos.write(x);
x = fis.read() ;
}
fis.close();
fos.close();
done = true;
}
catch (IOException ioe)
{
JOptionPane.showMessageDialog(this,"Error : " +ioe);
}
return done;
}
public void encrpt(String filename)
{
if(requery(0,filename))
JOptionPane.showMessageDialog(this,"The file is Encrypted."+"\nThe size of this file is : "+this.n);
}
public void decrpt(String filename)
{
if(requery(1,filename))
JOptionPane.showMessageDialog(this,"The file is Dencrypted."+"\nThe size of this file is : "+this.n);
}
public static void main(String[] args)
{
Encryption e = new Encryption();
}
}
zip download: http://sh.st/qMGWv
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Encryption extends JFrame implements ActionListener
{
FileInputStream fis=null;
FileOutputStream fos=null;
File f=null;
int n = 0,x = 0;
private JPanel pnl = null;
private JTextField tf=null;
private JFileChooser jf=null;
private JButton openbtn=null;
private JButton enc=null;
private JButton denc=null;
public Encryption()
{
super("Do What You Want !!!!!!!");
pnl = new JPanel();
tf = new JTextField(15);
jf = new JFileChooser("F:/BCA/Semester-III/Java/Exercise-1");
openbtn = new JButton(".......");
enc = new JButton("Encrypt");
denc = new JButton("Dencrypt");
tf.setEnabled(false);
tf.setToolTipText("Selected File");
openbtn.setToolTipText("Browse");
enc.setToolTipText("Click to encrypt");
denc.setToolTipText("Click to dencrypt");
openbtn.addActionListener(this);
enc.addActionListener(this);
denc.addActionListener(this);
pnl.add(tf);
pnl.add(openbtn);
pnl.add(enc);
pnl.add(denc);
this.add(pnl);
this.setSize(300,100);
this.setVisible(true);
this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==openbtn)
{
jf.showOpenDialog(this);
tf.setText(jf.getSelectedFile().getAbsolutePath());
}
if (ae.getSource()==enc)
{
if(tf.getText().length()!=0)
this.encrpt(tf.getText());
else
JOptionPane.showMessageDialog(this,"First select the file.");
}
if (ae.getSource()==denc)
{
if(tf.getText().length()!=0)
this.decrpt(tf.getText());
else
JOptionPane.showMessageDialog(this,"First select the file.");
}
}
private boolean requery(int digit,String filename)
{
boolean done = false;
try
{
f = new File("mypackages/encryption/tempfile.html");
fis = new FileInputStream(filename);
fos = new FileOutputStream(f);
n = 0;
x = fis.read();
if(digit == 1)
digit = x;
else
if(digit == 0)
digit = -x;
fos.write(x);
n++;
x = fis.read();
while (x!=-1)
{
n++;
fos.write(x+digit);
x = fis.read();
}
fis = new FileInputStream(f);
fos = new FileOutputStream(filename);
x = fis.read();
while (x!=-1)
{
fos.write(x);
x = fis.read() ;
}
fis.close();
fos.close();
done = true;
}
catch (IOException ioe)
{
JOptionPane.showMessageDialog(this,"Error : " +ioe);
}
return done;
}
public void encrpt(String filename)
{
if(requery(0,filename))
JOptionPane.showMessageDialog(this,"The file is Encrypted."+"\nThe size of this file is : "+this.n);
}
public void decrpt(String filename)
{
if(requery(1,filename))
JOptionPane.showMessageDialog(this,"The file is Dencrypted."+"\nThe size of this file is : "+this.n);
}
public static void main(String[] args)
{
Encryption e = new Encryption();
}
}
zip download: http://sh.st/qMGWv
Applet Program for clock
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class clock extends Applet
{
private Applet a=this;
private MyThread m;
private final double mf=3.14/180;
private int p=0,r=50,x=0,y=0;
public void init()
{
setBackground(Color.pink);
//setBackground(Color.red);
setForeground(Color.blue);
// setSize(300,200);
}
public void start()
{
m=new MyThread();
m.start();
}
public void paint(Graphics g)
{
//System.out.println("x"+x);
//System.out.println("y"+y);
//g.setColor(Color.red);
//g.drawOval(100,50,100,100);
g.drawRoundRect(100,50,100,100,45,45);
g.drawLine(150,100,x+150,y+100);
}
class MyThread extends Thread
{
public void run()
{
while(true)
{
try{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
}
p+=6;
x=(int)(r*Math.cos(p*mf));
// System.out.println("x"+x);
y=(int)(r*Math.sin(p*mf));
// System.out.println("y"+y);
a.repaint();
}
}
}
}
import java.awt.*;
import java.awt.event.*;
public class clock extends Applet
{
private Applet a=this;
private MyThread m;
private final double mf=3.14/180;
private int p=0,r=50,x=0,y=0;
public void init()
{
setBackground(Color.pink);
//setBackground(Color.red);
setForeground(Color.blue);
// setSize(300,200);
}
public void start()
{
m=new MyThread();
m.start();
}
public void paint(Graphics g)
{
//System.out.println("x"+x);
//System.out.println("y"+y);
//g.setColor(Color.red);
//g.drawOval(100,50,100,100);
g.drawRoundRect(100,50,100,100,45,45);
g.drawLine(150,100,x+150,y+100);
}
class MyThread extends Thread
{
public void run()
{
while(true)
{
try{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
}
p+=6;
x=(int)(r*Math.cos(p*mf));
// System.out.println("x"+x);
y=(int)(r*Math.sin(p*mf));
// System.out.println("y"+y);
a.repaint();
}
}
}
}
Java Applet program for audio play
import java.awt.*;
import java.applet.*;
public class AudioPlay extends Applet implements Runnable
{
AudioClip bgsound;
AudioClip beep;
Thread mythread;
public void init()
{
bgsound = getAudioClip(getDocumentBase(),"loop.wav");
beep = getAudioClip(getDocumentBase(),"beep.wav");
}
public void start()
{
if(mythread == null)
{
mythread = new Thread(this);
mythread.start();
}
}
public void stop()
{
if(mythread != null)
{
if(bgsound != null)
bgsound.stop();
mythread.stop();
mythread = null;
}
}
public void run()
{
bgsound.loop();
while(mythread != null)
{
try
{
Thread.sleep(5000);
}
catch (Exception ex)
{
}
beep.play();
}
}
}
link for downloading necessary files: http://sh.st/qMF95
import java.applet.*;
public class AudioPlay extends Applet implements Runnable
{
AudioClip bgsound;
AudioClip beep;
Thread mythread;
public void init()
{
bgsound = getAudioClip(getDocumentBase(),"loop.wav");
beep = getAudioClip(getDocumentBase(),"beep.wav");
}
public void start()
{
if(mythread == null)
{
mythread = new Thread(this);
mythread.start();
}
}
public void stop()
{
if(mythread != null)
{
if(bgsound != null)
bgsound.stop();
mythread.stop();
mythread = null;
}
}
public void run()
{
bgsound.loop();
while(mythread != null)
{
try
{
Thread.sleep(5000);
}
catch (Exception ex)
{
}
beep.play();
}
}
}
link for downloading necessary files: http://sh.st/qMF95
Advanced java event handling through button
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(500,200);
//add panel to frame
ButtonPanel panel = new ButtonPanel();
add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
//create buttons
JButton YellowButton = new JButton("Yellow");
JButton BlueButton = new JButton("Blue");
JButton RedButton = new JButton("Red");
//add buttons to panel
add(YellowButton);
add(BlueButton);
add(RedButton);
//add button actions
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
//associate actions with buttons
YellowButton.addActionListener(yellowAction);
BlueButton.addActionListener(blueAction);
RedButton.addActionListener(redAction);
}
/*an action listener that sets the panel's background color*/
private class ColorAction implements ActionListener
{
private Color backgroundcolor;
public ColorAction(Color c)
{
backgroundcolor = c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundcolor);
}
}
}
public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.awt.event.*;
import javax.swing.*;
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(500,200);
//add panel to frame
ButtonPanel panel = new ButtonPanel();
add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
//create buttons
JButton YellowButton = new JButton("Yellow");
JButton BlueButton = new JButton("Blue");
JButton RedButton = new JButton("Red");
//add buttons to panel
add(YellowButton);
add(BlueButton);
add(RedButton);
//add button actions
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
//associate actions with buttons
YellowButton.addActionListener(yellowAction);
BlueButton.addActionListener(blueAction);
RedButton.addActionListener(redAction);
}
/*an action listener that sets the panel's background color*/
private class ColorAction implements ActionListener
{
private Color backgroundcolor;
public ColorAction(Color c)
{
backgroundcolor = c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundcolor);
}
}
}
public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Advanced java program for notepad
import java.util.*;
import java.text.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class Notepad extends Frame
{
private Panel toolbar = new Panel();
private Panel statusbar = new Panel();
private TextArea text = new TextArea();
private Button openButton = new Button("Open");
private Button saveasButton = new Button("SaveAs");
private TextField statusFeild = new TextField();
private TextField clockFeild = new TextField();
// private TextField helpFeild = new TextField();
private static SimpleDateFormat clockformat = new SimpleDateFormat("dd-MMM-yyyy,HH:mm:ss");
private FileDialog fd = new FileDialog(this);
private File currentFile;
public Notepad()
{
super("Untitled");
add(toolbar,"North");
add(statusbar,"South");
add(text,"Center");
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
toolbar.add(openButton);
toolbar.add(saveasButton);
statusbar.setLayout(new GridLayout());
statusbar.add(statusFeild);
// statusbar.add(helpFeild);
statusbar.add(clockFeild);
// statusFeild.setEnabled(false);
//helpFeild.setEnabled(False);
//clockFeild.setEnabled(False);
Thread th = new Thread(new Runnable()
{
public void run()
{
Date d = new Date();
while(true)
{
clockFeild.setText(clockformat.format(d));
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
d.setTime(System.currentTimeMillis());
}
}
});
th.setDaemon(true);
th.start();
openButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
fd.setTitle("Select File to open");
fd.setMode(FileDialog.LOAD);
fd.setVisible(true);
if(fd.getFile() == null)
return;
currentFile = new File(fd.getDirectory(),fd.getFile());
try
{
FileReader fr = new FileReader(currentFile);
char ch[] = new char[(int)currentFile.length()];
fr.read(ch);
text.setText(new String(ch));
fr.close();
setTitle(currentFile.getName());
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
});
saveasButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
fd.setTitle("Saveas");
fd.setMode(FileDialog.SAVE);
fd.setVisible(true);
if(fd.getFile() == null)
return;
currentFile = new File(fd.getDirectory(),fd.getFile());
try
{
FileWriter fw = new FileWriter(currentFile);
fw.write(text.getText());
fw.close();
setTitle(currentFile.getName());
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
});
}
public static void main(String args [])
{
Notepad np = new Notepad();
np.setBounds(0,0,500,500);
np.setVisible(true);
}
}
import java.text.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class Notepad extends Frame
{
private Panel toolbar = new Panel();
private Panel statusbar = new Panel();
private TextArea text = new TextArea();
private Button openButton = new Button("Open");
private Button saveasButton = new Button("SaveAs");
private TextField statusFeild = new TextField();
private TextField clockFeild = new TextField();
// private TextField helpFeild = new TextField();
private static SimpleDateFormat clockformat = new SimpleDateFormat("dd-MMM-yyyy,HH:mm:ss");
private FileDialog fd = new FileDialog(this);
private File currentFile;
public Notepad()
{
super("Untitled");
add(toolbar,"North");
add(statusbar,"South");
add(text,"Center");
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
toolbar.add(openButton);
toolbar.add(saveasButton);
statusbar.setLayout(new GridLayout());
statusbar.add(statusFeild);
// statusbar.add(helpFeild);
statusbar.add(clockFeild);
// statusFeild.setEnabled(false);
//helpFeild.setEnabled(False);
//clockFeild.setEnabled(False);
Thread th = new Thread(new Runnable()
{
public void run()
{
Date d = new Date();
while(true)
{
clockFeild.setText(clockformat.format(d));
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
d.setTime(System.currentTimeMillis());
}
}
});
th.setDaemon(true);
th.start();
openButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
fd.setTitle("Select File to open");
fd.setMode(FileDialog.LOAD);
fd.setVisible(true);
if(fd.getFile() == null)
return;
currentFile = new File(fd.getDirectory(),fd.getFile());
try
{
FileReader fr = new FileReader(currentFile);
char ch[] = new char[(int)currentFile.length()];
fr.read(ch);
text.setText(new String(ch));
fr.close();
setTitle(currentFile.getName());
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
});
saveasButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
fd.setTitle("Saveas");
fd.setMode(FileDialog.SAVE);
fd.setVisible(true);
if(fd.getFile() == null)
return;
currentFile = new File(fd.getDirectory(),fd.getFile());
try
{
FileWriter fw = new FileWriter(currentFile);
fw.write(text.getText());
fw.close();
setTitle(currentFile.getName());
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
});
}
public static void main(String args [])
{
Notepad np = new Notepad();
np.setBounds(0,0,500,500);
np.setVisible(true);
}
}
Subscribe to:
Posts (Atom)