/* Write a program in java and print a massage. */ class Msg { public static void main(String args[]) { [Link].
println("Java is better than C++"); } }
Output :
Java is better then C++
/* Write a program input a number and find no. is even or odd with the help of conditional operater */ import [Link].*; class EvenOdd { public static void main(String arg[])throws IOException { int n; DataInputStream in= new DataInputStream([Link]); [Link]("Enter a no :"); n=[Link]([Link]()); [Link]((n%2==0)?"Even":"Odd");
Output : Enter a no :4 Even
/* write a program to create a class and then call its methods by its object */ class Circle { private double radius; public void setRadius(double x) { if(x <= 0) throw new IllegalArgumentException("Radius can not be zero or negative"); radius=x; } public double getRadius() { return radius; }
} class CircleTest2 { public static void main(String args[]) { Circle c1; c1 = new Circle(); [Link](-2.6); double p; p = [Link]* 2 *[Link](); [Link]("Peri = "+p);
} Output Peri = 16.336281798666924
/* Write a program of an applet use of font properties */
import [Link].*; import [Link].*; public class color extends Applet { Color textcolor; Font f=new Font("Courier",[Link] +[Link],30); String s="Colorful Applet"; String Param; public void init() { setBackground([Link]); Param=getParameter("Mycolor"); if(Param !=null) { if([Link]("red"))textcolor=[Link]; if([Link]("green"))textcolor=[Link]; if([Link]("yellow"))textcolor=[Link]; } else textcolor=[Link]; } public void paint(Graphics g) { [Link](textcolor); [Link](f); [Link](s,40,40); }
} /*<applet code="[Link]" width=400 height=300> <Param name=mycolor value="blue"> </applet> */
/* write a program to create a canvas on applet
*/
import [Link].*; import [Link].*; public class Frame10 extends Applet { public void init() { setLayout(new FlowLayout([Link])); setBackground([Link]); Canvas c=new Canvas(); [Link]([Link]); [Link](100,50); add(c); } } /* <applet code=[Link] width=150 height=160 > </applet> */
/* write a program in applet and use the method of applet init(), start(), stop(), destroy() */ import [Link].*; import [Link].*; public class MethodeTxt extends Applet { String out; public void init() { setBackground([Link]); setForeground([Link]); [Link]("Init method"); out="init"; } public void start() { [Link]("Start method"); out="start->"; } public void stop() { [Link]("Stop method"); out="start->"; } public void destroy() { [Link]("Destroy method"); out="Destroy->"; } public void paint(Graphics g) { [Link]("Paint method"); out="paint->"; [Link](out,100,10); }
/*<applet code=[Link] width=200 height=200 > </applet> */
Output
Output on DOS prompt
Init method Start method Paint method Stop method
/* Write a program of applet to print a massage on applet */ import [Link].*; import [Link].*; public class Msgs extends Applet { public void paint(Graphics g) { [Link]("Welcome to applet",30,30); } } /*<applet code=[Link] width=50 height=80 > </applet> */
Output
/* write a program and set the priority of a thread
*/
class A extends Thread { public void run() { [Link]("Thread first is started"); for(int i=1;i<=10;i++) { [Link]("i :"+i); } [Link]("Exit from A"); } } class B extends Thread { public void run() { [Link]("Second thread is starting"); for(int j=1;j<11;j++) { [Link]("j :"+j); } [Link]("Exit from B"); } } class DaemonPriority { public static void main(String arg[]) { [Link]("Main begin"); A f=new A(); [Link](10); B f1=new B(); [Link](1); [Link](); [Link]();
Output
Main begin Thread first is started i :1 Second thread is starting i :2 j :1 i :3 j :2 i :4 j :3 j :4 j :5 j :6 j :7 j :8 j :9 j :10 i :5 Exit from B i :6 i :7 i :8 i :9 i :10 Exit from A
/* To create a program call the SetDaemon() method after the thread creation and before execution is started */ class DaemonSet1 { public static void main(String { First f=new First(); if([Link]()) [Link]("This else [Link]("This [Link](true); if([Link]()) [Link]("This else [Link]("This [Link](false); [Link](); } } class First extends Thread { public void run() { [Link]("This for(int i=1;i<=10;i++)
args[])
is Daemon thread"); is not daemon thread");
is daemon thread"); is not daeomon thread");
is the begin of thread");
[Link]("i :"+i); [Link]("Over first thread"); }
Output This is not daemon thread This is daemon thread This is the begin of thread i :1 i :2 i :3 i :4 i :5 i :6 i :7 i :8 i :9 i :10 Over first thread
/* Write a program of array in java in which we create a matrix and print diagonal elements from left */ class Array4 { public static void main(String args[]) { int a[][]={ {1,2,3}, {4,5,6}, {7,8,9} }; [Link]("The actual matrix is:"); for(int i=0;i<[Link];i++) { for(int j=0;j<[Link];j++) { [Link]("\t"+a[i][j]); } [Link]("\t"); } [Link]("The diagonal series is of numbers from left:"); for(int i=0;i<[Link];i++) { for(int j=0;j<[Link];j++) { if(i==j) [Link](a[i][j]); } } [Link]("The diagonal series is of numbers from left:"); for(int i=0;i<[Link];i++) { for(int j=0;j<[Link];j++)
if(i<j) [Link](+a[i][j]);
[Link]("The diagonal series is of numbers from left:"); for(int i=0;i<[Link];i++) { for(int j=0;j<[Link];j++) { if(i>j) [Link]("\t"+a[i][j]); } [Link]("\t"); } [Link]("The diagonal series is of numbers from right:"); for(int i=0;i<[Link];i++) { for(int j=0;j<[Link];j++) { if(i+j==2) [Link](a[i][j]); } } }
Output The actual matrix is: 1 2 3 4 5 6 7 8 9 The diagonal series is of numbers from left:
The diagonal series is of numbers from left: 2 3 6 The diagonal series is of numbers from left: 4 7 8 The diagonal series is of numbers from right: 3 5 7
/* Write a program of Exception handling */ class ExTest3 { public static void main(String args[]) { int x,y,r; try {
} catch(ArithmeticException ex) { [Link]("Divisor can not be zero"); } catch(RuntimeException ex) { [Link]("I/O Error"); } [Link]("End of main....");
x = [Link](args[0]); y = [Link](args[1]); r = x/y; [Link]("Result : "+r);
Output
G:\Pro\coll\File>java ExTest3 Result : 6 End of main.... G:\Pro\coll\File>java ExTest3 Divisor can not be zero End of main.... G:\Pro\coll\File>java ExTest3 I/O Error End of main.... G:\Pro\coll\File>java ExTest3 I/O Error End of main....
12 2
12 0
12
vfhdg dfj
/* A program of String */ import static [Link].*; class StrTest1 { public static void main(String args[]) { String s1 = new String("Unisoft Technologies"); String s2 = ",Dehradun"; //[Link](s1[3]); //String s3 = [Link](s2); String s3 = s1+s2; [Link](s1); [Link](s2); [Link](s3); }
Output
Unisoft Technologies ,Dehradun Unisoft Technologies,Dehradun
/* A program of StringBuffer and reverse the String */
import static [Link].*; class BufferTest1 { public static void main(String args[]) { StringBuffer s1 = new StringBuffer("Amit Kumar Sharma"); StringBuffer s2 = s1; [Link](",Dehradun"); [Link](s1); [Link](s2); [Link](); [Link](s1); [Link](s2); }
Output Amit Kumar Sharma,Dehradun Amit Kumar Sharma,Dehradun nudarheD,amrahS ramuK timA nudarheD,amrahS ramuK timA
/* Write a program in thread use join() and Sleep method and also call second class of start() method from First class */
class Alpha extends Thread { public void run() { [Link]("Alpha Started..."); Beta b1 = new Beta(); [Link](); for(int i=1;i<=5;i++) { [Link]("Alpha : "+i); try {
if(i==3) [Link](); [Link](1000);
} catch(Exception e){} } [Link]("Alpha Ended..."); } class Beta extends Thread { public void run() { [Link]("Beta Started..."); for(int i=5;i>0;i--) { [Link]("Beta : "+i); try }
{ } catch(Exception e){} } [Link]("Beta Ended..."); } } [Link](1000);
class ThreadTest5 { public static void main(String args[])throws Exception { [Link]("Main Started..."); Alpha a1 = new Alpha(); [Link](); for(int i=1;i<=5;i++) { [Link]("Main : "+i); if(i==5) [Link](); [Link](1000); } [Link]("Main Ended..."); } }
Output Main Started... Main : 1 Alpha Started... Alpha : 1 Beta Started... Beta : 5 Beta : 4 Main : 2 Alpha : 2 Alpha : 3 Main : 3 Beta : 3 Main : 4 Beta : 2 Beta : 1 Main : 5 Beta Ended... Alpha : 4 Alpha : 5 Alpha Ended... Main Ended...
/* Write a program use all data types */ class DataType { public static void main(String args[]) { /*boolean b=true; char ch = 'a'; ch = '\u10FA'; ch=65; [Link](ch); long l = 12345678324324324L; [Link](l); float f = 2.5F; [Link](f);*/ byte b=(byte)128; [Link](b);
Output : true A 12345678324324324 2.5 -128
/* Write a program and print host name and IP address */ import [Link].*; import [Link].*; import static [Link].*; class Networking1 { public static void main(String args[])//throws Exception { try { InetAddress ia=[Link](); [Link]("Host and address"+ia); [Link]("Host name:"+[Link]()); String s=[Link](); [Link]("IP address:"+[Link]([Link]("/"+1))); } catch(Exception e) { } } }
Output: Host and addressabc/[Link] Host name:abc IP address:/[Link]
/* Write a program enter a host name and print its IP address */ import [Link].*; import [Link].*; import static [Link].*; class Networking2 { public static void main(String args[])//throws Exception { try { [Link]("Enter the host name:"); String s=new DataInputStream([Link]).readLine(); InetAddress i=[Link](s); [Link]("IP address :"+i); } catch(Exception e) { [Link]("Please input correct host name"); } } }
Output : Enter the host name:abc IP address :abc/[Link]
\/* Write a program of swing and print a massage on applet */ import [Link].*; import [Link].*; public class Swing1 extends JApplet { public void init() { Container c=getContentPane(); JLabel jl=new JLabel("This is swing Version of a Label"); [Link](jl); } } /* <applet code=[Link] width=200 height=200> </applet> */
/* Write a program of swing and use GridLayoyt on applet */
import [Link].*; import [Link].*; public class Swing2 extends JApplet { public void init() { Container c=getContentPane(); [Link](new GridLayout(3,1)); ImageIcon ii1=new ImageIcon("[Link]"); JLabel ji1=new JLabel("A Left",ii1,[Link]); JLabel ji2=new JLabel("A Center",ii1,[Link]); JLabel ji3=new JLabel("A Right",ii1,[Link]); [Link](ji1); [Link](ji2); [Link](ji3);
} /* <applet code=[Link] width=200 height=200> </applet> */
/* Create a form and set Flow Layout and also create a button on this Form */ import [Link].*; import [Link].*; class ButtonFrame extends Frame { Button b; ButtonFrame() { setTitle("Button Frame"); FlowLayout f=new FlowLayout(); b=new Button("Exit Program"); add(b); setSize(600,400); setVisible(true); } public static void main(String args[]) { new ButtonFrame(); }
/* a program to create a form set Button in Grid Layout
*/
import [Link].*; import [Link].*; class LayoutFrame2 extends Frame { Button b1,b2,b3,b4,b5; LayoutFrame2() { setTitle("LayoutFrame1"); setLayout(new GridLayout(3,2,10,10)); b1 = new Button("Button : 1"); b2 = new Button("Button : 2"); b3 = new Button("Button : 3"); b4 = new Button("Button : 4"); b5 = new Button("Button : 5"); add(b1);add(b2);add(b3);add(b4);add(b5); setSize(600,400); setVisible(true); } public static void main(String args[]) { new LayoutFrame2(); } }
/* a program to create a form set Button in Border Layout
*/
import [Link].*; import [Link].*; class LayoutFrame3 extends Frame { Button b1,b2,b3,b4,b5; LayoutFrame3() { setTitle("Border Layout"); setLayout(new BorderLayout()); b1=new b2=new b3=new b4=new b5=new Button("Button Button("Button Button("Button Button("Button Button("Button : : : : : 1"); 2"); 3"); 4"); 5");
add(b1,"North"); add(b2,"South"); add(b3,"East"); add(b4,"West"); add(b5); setSize(600,400); setVisible(true);
} public static void main(String args[]) { new LayoutFrame3(); } }
/* A program of event handling */
import [Link].*; import [Link].*; class ColorFrame1 extends Frame implements ActionListener { Button b1,b2,b3,b4; ColorFrame1() { setTitle("Color Frame"); setLayout(new FlowLayout()); b1=new b2=new b3=new b4=new add(b1); add(b2); add(b3); add(b4); [Link](this); [Link](this); [Link](this); [Link](this); Button("Red"); Button("Green"); Button("Blue"); Button("Exit");
} public static void main(String args[]) { new ColorFrame1(); } public void actionPerformed(ActionEvent ae) {
setSize(600,400); setVisible(true);
if([Link]()==b1) setBackground([Link]); if([Link]()==b2) setBackground([Link]); if([Link]()==b3) setBackground([Link]); if([Link]()==b4) [Link](0);
} }
/* Use of this
*/
import static [Link].*; class Alpha { int x,y,z; Alpha(int x, int y, int z) { this.x=x; this.y=y; this.z=z; } void print() { [Link]("x = "+x+", y = "+y+", z = "+z); } void sum() { int x; x = this.x+y+z; [Link]("Sum = "+x); }
} class StaticTest5 { public static void main(String args[]) { Alpha a1 = new Alpha(1,2,3); Alpha a2 = new Alpha(4,5,6); Alpha a3 = new Alpha(7,8,9);
[Link](); [Link](); [Link](); } }