You are on page 1of 29

1. Write a program for finding length of an array.

class length { public static void main(String args[]) { int arr[]={1,2,3,4,5,6,7,8,9}; int len=0,i; len=arr.length; System.out.println("The length of Array=" +len); } }

Output:-

2.Write program for swap the value.


class swap { public static void main(String args[]) { int a=10,b=20; System.out.println("Before Swaping"); System.out.println("The value of a=" +a +"the value of b=" +b); System.out.println("After Swaping"); a=a+b; b=a-b; a=a-b; System.out.println("The value of a=" +a +"the value of b=" +b); } }

Output:-

3. Write a program for reverse number.


class rev { public static void main(String args[]) { int arr=1234; int len=0,i; while(arr>0) { System.out.print(arr%10); arr=arr/10; } } }

Output:-

4.Write a program for inheritance.


//Program for java inheritance. // Create a superclass. class A { int i, j; void showij() { System.out.println("i and j: " + i + " " + j); } } // Create a subclass by extending class A. class B extends A { int k; void showk() { System.out.println("k: " + k); } void sum() { System.out.println("i+j+k: " + (i+j+k)); } } class inheritance { public static void main(String args[]) { A superOb = new A(); B subOb = new B(); // The superclass may be used by itself. superOb.i = 10;

superOb.j = 20; System.out.println("Contents of superOb: "); superOb.showij(); System.out.println(); /* The subclass has access to all public members of its superclass. */ subOb.i = 7; subOb.j = 8; subOb.k = 9; System.out.println("Contents of subOb: "); subOb.showij(); subOb.showk(); System.out.println(); System.out.println("Sum of i, j and k in subOb:"); subOb.sum(); } } /*The output from this program is shown here: Contents of superOb: i and j: 10 2 Contents of subOb: i and j: 7 8 k: 9 Sum of i, j and k in subOb: i+j+k: 24 As you can see, the subclass B includes all of the members of its superclass, A. This is why subOb can access i and j and call showij( ). Also, inside sum( ), i and j can be referred to directly, as if they were part of B.*/

Output:-

5.Write a program for palindrome no.


import java.io.*; class palin { public static void main(String args[]) throws IOException { InputStreamReader r1=new InputStreamReader(System.in); BufferedReader read=new BufferedReader(r1); int n,n1,a,inv=0; System.out.print("Enter a number:"); n=Integer.parseInt(read.readLine()); n1=n; while(n!=0) { a=n%10; inv=inv*10+a; n=n/10; } if(inv==n1) System.out.print("Palindrome Number"); else System.out.print("Not a Palindrome Number"); } }

Output:-

6. Write a program for overloading.


// Demonstrate method overloading. class OverloadDemo { void test() { System.out.println("No parameters"); } // Overload test for one integer parameter. void test(int a) { System.out.println("a: " + a); } // Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload test for a double parameter double test(double a) { System.out.println("double a: " + a); return a*a; } }

class overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; // call all versions of test() ob.test(); ob.test(10); ob.test(10, 20); result = ob.test(123.25); System.out.println("Result of ob.test(123.25): " + result); } } /*This program generates the following output: No parameters a: 10 a and b: 10 20 double a: 123.25 Result of ob.test(123.25): 15190.5625*/

Output:-

7. Write a program for overrinding.


// Method overriding. class A { int i, j; A(int a, int b) { i = a; j = b; } // display i and j void show() { System.out.println("i and j: " + i + " " + j); } } class B extends A { int k; B(int a, int b, int c) { super(a, b); k = c; } // display k this overrides show() in A

void show() { System.out.println("k: " + k); } } class override { public static void main(String args[]) { B subOb = new B(1, 2, 3); subOb.show(); // this calls show() in B } }

Output:-

8.Write a program for package.


/* package*/ package mypack; public class student { int rno; String name; public student(int r,String n) { rno=r; name=n; } public void display() { System.out.println("R.No. is " + rno +" and name is " +name); } }

import mypack.*; class javapackage { public static void main(String args[]) {

student s1=new student(200,"Astbhuja"); s1.display(); } }

Output :-

9. Write a program for Armstrong.


class arm { public static void main(String args[]) { int up=0,lp=0,j=0,n=0,m=0,c=0,a=0,b=0; a=Integer.parseInt(args[0]); b=Integer.parseInt(args[1]); if(a>b) { up=a; lp=b; } else { up=b; lp=a; } System.out.println("Armstrong No between " +lp +" to " +up); for(;lp<=up;lp++) { n=lp; while(n>=1) {

c=n%10; n=n/10; m=m+c*c*c; } if(m==lp) System.out.println("the armstrong is="+lp); m=0; } } }

Output:-

10. Write a program for print the length of string and reverse of that string.
class revstr { public static void main(String args[]) { StringBuffer arr=new StringBuffer("astbhuja pandey"); int len=0,i; len=arr.length(); System.out.println("the Length of string=" +len); arr.reverse(); System.out.println("reverse String =" +arr);

} }

Output:-

11. Write a for thread.


class mythread implements Runnable { Thread t; mythread() { t=new Thread(); t.start(); } public void run() { int i; i=10; try { while(i<15) System.out.println(i); t.sleep(1000); i++;

} catch(Exception e) { } } }

public class thread1 { public static void main(String args[]) { new mythread(); int x; x=0; try { while(x<15) { x++; System.out.println(x); Thread.sleep(1000); } } catch(Exception e) { System.out.println(e); } } }

Output:-

12. Write a program print the following applet.


Circle Triangle Line import java.awt.*; import java.applet.*; /* <applet code="apple" width=300 height=300> </applet> */

public class apple extends Applet { public void paint(Graphics g) { g.drawOval(40,40,140,140); g.drawLine(20,200,100,250); g.drawLine(10,10,10,80); g.drawLine(10,10,55,45); g.drawLine(10,80,55,45); } }

Output:-

13.

PROGRAM FOR CONSTRUCTOR

class Constructor { int age; String name; public Constructor(int a,String s) {

age=a; name=s; } } public class Info { public static void main(String[] as) { Constructor con=new Constructor(25,"Anand Mishra"); System.out.println("Name of Emp: "+con.name); System.out.println("Age of Emp: "+con.age); } }

Output:Name of Emp: Anand Mishra Age of Emp: 25

14. PROGRAM FOR INTERFACE


interface a{ public void fun(); public void sun(); }

class b implements a{ public void fun() { System.out.println("from class fun"); } public void sun() { System.out.println("from class sun"); }

} class Inter{ public static void main(String a[]) { b n=new b(); n.sun(); n.fun(); } } Output:-

c:\>javac Inter.java c:\>java Inter from class sun from class fun

15. PROGRAME FOR ABSTRACT METHOD


abstract class a{ public void fun() { System.out.println("from class a");

} public abstract void sun(); } class b extends a{ public void sun() { System.out.println("from class b"); } } class Abs{ public static void main(String a[]) { b n=new b(); n.sun(); n.fun(); } } output:c:\>javac Abs.java c:\>java Abs from class b

from class a

16.PROGRAM FOR SERVLET


import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class MyServlet extends HttpServlet

{ public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException { String Name =req.getParameter("txtName"); res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("Welcome " + Name); out.close(); } } HTML CODE <html> <head> <title> First web application </title> </head> <body> <form method="get" action="welcomeservlet"> name <Input Type ="text" name ="txtName"> <br> <input type ="Submit" Value = "Submit">

</form> </body> </html> XML CODE <web-app> <servlet> <servlet-name>s1</servlet-name> <servlet-class>MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>s1</servlet-name> <url-pattern>welcomeservlet</url-pattern> </servlet-mapping> </web-app>

output:-

17.PROGRAM FOR MENU ITEAM


import java.awt.*; import java.awt.event.*; class Nawframe extends Frame implements ActionListener

{ MenuBar mb; Menu mfile,medit; MenuItem mnew,mexit;

Nawframe(String f) { super(f); mb=new MenuBar(); mfile=new Menu("File"); medit=new Menu("Edit"); mnew=new MenuItem("New"); mexit=new MenuItem("Exit");

mnew.addActionListener(this); mexit.addActionListener(this);

mfile.add(mnew); mfile.add(mexit); mb.add(mfile);

mb.add(medit); setMenuBar(mb); } public void actionPerformed(ActionEvent w) { if(w.getSource()==mexit) { System.exit(0); } else if(w.getSource()==mnew) { Nawframe n=new Nawframe("New Frame"); n.setSize(100,100); n.setVisible(true); } } } public class Menitem { public static void main(String s[])

{ Nawframe d=new Nawframe("My Frame"); d.setSize(100,100); d.setVisible(true); } }

output:-

You might also like