Saturday 28 July 2012

DirList example in java program using file handling


import java.io.*;
class DirList {
      public static void main(String args[]) {
      String dirname="/subha";
      File f1=new File(dirname);
        if(f1.isDirectory()) {
            System.out.println("Directory of " + dirname);
            String s[]=f1.list();
            for(int i=0;i<s.length;i++)
            {
                    File f=new File(dirname + "/" +s[i]);
                    if(f.isDirectory()) {
                            System.out.println(s[i] + " is a Directory");
                    }
            else
             {
                            System.out.println(s[i] + " is a File");
              }
                    }
       }
      else
            {
                         System.out.println(dirname + " is not a directory");
             }
       }
}

Directory of .HTML Files


    // Directory of .HTML Files
        import java.io.*;
        class DirListOnly {
        public static void main(String args[]) {
                String dirname="/subha/html";
                File f1=new File(dirname);
                FilenameFilter only=new OnlyExt("html");
                String s[]=f1.list(only);
                for(int i=0;i<s.length;i++) {
                        System.out.println(s[i]);
                }
             }
        }

FilenameFilter example in java


import java.io.*;
class OnlyExt implements FilenameFilter {
        String ext;
        public OnlyExt(String ext)
       {
        this.ext="."+ ext;
        }
        public boolean accept(File dir,String name)
        {
        return name.endsWith(ext);
        }
}

stack example java program using interface


IntStack.java
interface IntStack {
        void push(int item);
        int pop();
}
FixedStack.java
class FixedStack implements IntStack {
        private int stack[];
        private int tos;
        FixedStack(int size)
        {
        stack=new int[size];
        tos=-1;
        }
        public void push(int item)
        {
        if(tos==stack.length-1)
                System.out.println("stack is full");
        else
                stack[++tos]=item;
        }
        public int pop()
        {
        if(tos<0)
        {
                System.out.println("stack underflow");
                return 0;
        }
        else
                return stack[tos--];
        }
        public static void main(String args[]) {
                FixedStack mystack1=new FixedStack(5);
                FixedStack mystack2=new FixedStack(8);
                for(int i=0;i<6;i++) mystack1.push(i);
                for(int i=0;i<9;i++) mystack2.push(i);
                System.out.println("stack in mystack1");
                for(int i=0;i<6;i++)
                System.out.println(mystack1.pop());
                System.out.println("stack in mystack2");
                for(int i=0;i<9;i++)
                System.out.println(mystack2.pop());
        }
}

interface example java program


Callback.java
interface Callback
{
        public void call(int param);
}
Client.java
class Client implements Callback
{
        public void call(int p)
        {
                System.out.println("Interface Method = " + p);
        }
        public void nonIfaceMeth()
        {
                System.out.println("Non Interface method is also used");
        }
        public static void main(String args[])
        {
                Callback c = new Client();
                c.call(5);
       //         c.nonIfaceMeth();
        }
}

multilevel with constructor example java program


//multilevel with constructor
 class students1
{
        private int sno;
        private String sname;
        students1(int no,String name)
        {
                sno = no;
                sname = name;
        }
        public void dispstud()
        {
                System.out.println("Student No : " + sno);
                System.out.println("Student Name : " + sname);
        }
}
class marks1 extends students1
{
        protected int mark1,mark2;
        marks1(int n1,int n2,int sno,String sname)
        {
                super(sno,sname);
                mark1 = n1;
                mark2 = n2;
        }
        public void dispmarks()
        {
                System.out.println("Mark1 : " + mark1);
                System.out.println("Mark2 : " + mark2);
        }
}
class finaltot1 extends marks1
{
        private int total;
        finaltot1(int n1,int n2,int no,String name)
        {
                super(n1,n2,no,name);
                total = mark1 + mark2;
        }
        public void disptotal()
        {
                System.out.println("Total : " + total);
        }
        public static void main(String args[])
        {
                finaltot1 f = new finaltot1(90,89,100,"Kirthika");
                f.dispstud();
                f.dispmarks();
                f.disptotal();
        }
}

multilevel inheritance example java program


class students
{
        private int sno;
        private String sname;
        public void setstud(int no,String name)
        {
                sno = no;
                sname = name;
        }
        public void putstud()
        {
                System.out.println("Student No : " + sno);
                System.out.println("Student Name : " + sname);
        }
}
class marks extends students
{
        protected int mark1,mark2;
        public void setmarks(int m1,int m2)
        {
                mark1 = m1;
                mark2 = m2;
        }
        public void putmarks()
        {
                System.out.println("Mark1 : " + mark1);
                System.out.println("Mark2 : " + mark2);
        }
}
class finaltot extends marks
{
        private int total;
        public void calc()
        {
                total = mark1 + mark2;
        }
        public void puttotal()
        {
                System.out.println("Total : " + total);
        }
        public static void main(String args[])
        {
                finaltot f = new finaltot();
                f.setstud(100,"Nithya");
                f.setmarks(78,89);
                f.calc();
                f.putstud();
                f.putmarks();
                f.puttotal();
        }
}

single interitance example java program


class emp1
{
        private int eno;
        private String ename;
        emp1(int no,String name)
        {
                System.out.println("Base Constructor");
                eno = no;
                ename=  name;
        }
        public void putemp()
        {
                System.out.println("Empno : " + eno);
                System.out.println("Empname : " + ename);
        }
}
class dept1 extends emp1
{
        private int dno;
        private String dname;
        dept1(int no,String name,int eno,String ename)
        {
                super(eno,ename);
                System.out.println("Derived Constructor");
                dno = no;
                dname = name;
        }
        public void putdept()
        {
                System.out.println("Deptno : " + dno);
                System.out.println("Deptname : " + dname);
        }
        public static void main(String args[])
        {
                dept1 d = new dept1(20,"Sales",100,"Kirthika");
                d.putemp();
                d.putdept();
        }
}

Single inheritance example program


class employee
{
        private int eno;
        private String ename;
        public void setemp(int no,String name)
        {
                eno = no;
                ename = name;
        }
        public void putemp()
        {
                System.out.println("Empno : " + eno);
                System.out.println("Ename : " + ename);
        }
}
class department extends employee
{
        private int dno;
        private String dname;
        public void setdept(int no,String name)
        {
                dno = no;
                dname = name;
        }
        public void putdept()
        {
                System.out.println("Deptno : " + dno);
                System.out.println("Deptname : " + dname);
        }
        public static void main(String args[])
        {
                department d = new department();
                d.setemp(100,"aaaa");
                d.setdept(20,"Sales");
                d.putemp();
                d.putdept();
        }
}
            super () -> Keyword used to call the base constructor;

StringBuffer example


class StringBuf
{
        public static void main(String args[])
        {
                StringBuffer sb = new StringBuffer("Hello World");
                System.out.println(sb + ".capacity() = " + sb.capacity());
                System.out.println(sb + ".length() = " + sb.length());
                System.out.println(sb + ".insert(5,aaa) = " + sb.insert(5,"aaa"));
                System.out.println(sb + ".capacity() = " + sb.capacity());
                System.out.println(sb + ".length() = " + sb.length());
                System.out.println(sb + ".append(bbbb) = " + sb.append("bbbb"));
                System.out.println(sb + ".reverse() = " + sb.reverse());
        }
}

StringMeth examples


class StringMeth
{
        public static void main(String args[])
        {
                String s1 = "This is a sample String";
                String s2 = "    Hello     ";
                String s3 = "Hello World";
                String s4 = "Hello";
                String s5 = "HELLO";
                System.out.println(s1 + ".length() : " + s1.length());
                System.out.println(s1 + ".charAt(2) : " + s1.charAt(2));
                System.out.println("Character Array");
                char c[] = new char[s1.length()+3];
                c[0] = 'a';c[1] = 'b';c[2] = 'c';
                s1.getChars(0,s1.length(),c,2);
                for(int i=0;i<c.length;i++)
                        System.out.print(c[i] + " ");
                System.out.println();
                System.out.println("Byte Array");
                byte b[] = new byte[s3.length()];
                s3.getBytes(0,s3.length(),b,0);
                for(int i=0;i<b.length;i++)
                        System.out.print(b[i] + " ");
                System.out.println();
               System.out.println(s4 + ".compareTo(" + s3 + ") = " +                                                                                            s4.compareTo(s3));
                System.out.println(s3 + ".startsWith(H) = " + s3.startsWith("H"));
                System.out.println(s3 + ".endsWith(u) = " + s3.endsWith("u"));
                System.out.println(s3 + ".indexOf(o) = " + s3.indexOf('o'));
                System.out.println(s3 + ".indexOf(o,5) = " + s3.indexOf('o',5));
                System.out.println(s3 + ".lastIndexOf(o) = " + s3.lastIndexOf('o'));
                System.out.println(s3 + ".lastIndexOf(o,6) = " +                                                                                        s3.lastIndexOf('o',6));
                System.out.println(s3 + ".substring(2,7) = " + s3.substring(2,7));
                System.out.println(s1 + ".concat(s2) = " + s1.concat(s2));
                System.out.println(s4 + ".replace(l,w) = " + s4.replace('l','w'));
                System.out.println(s4 + ".toUpperCase() = " + s4.toUpperCase());
                System.out.println(s5 + ".toLowerCase() = " + s5.toLowerCase());
                System.out.println(s2 + ".trim() = " + s2.trim());
        }
}

String class example


class Strings {
        int i;
        String name[]={"Aswath","Aswin","Anand","Aditya","Anirudh"};
        void show() {
        System.out.println("My favourite names are ");
        for(i=0;i<5;i++) {
                System.out.println(name[i]);
        }
        }
        public static void main(String args[]) {
                Strings s=new Strings();
                s.show();
        }
}

Equal function example program


class equaldemo {
public static void main(String args[]) {
String s1="Hello";
String s2="Hello";
String s3="Good bye";
String s4="HELLO";
System.out.println(s1 + " equals " + s2 + " is " + s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " is " + s1.equals(s3));
System.out.println(s1 + " equals " + s4 + " is " + s1.equals(s4)); 
System.out.println(s1 + " equals " + s4 + " is " + s1.equalsIgnoreCase(s4));
}
}

Hashcode Example


class hash {
public static void main(String args[]) {
        String s1="world";
        String s2="Hello";
System.out.println("The hash code for " + s1 + " is " + s1.hashCode());
System.out.println("The hash code for " + s2 + " is " + s2.hashCode());
}
}

String Example


class Arith {
        String fname="Aswath";
        String lname="Narayanan";
        void show() {
        System.out.println("The fullname is " + fname + " " + lname);
        }
public static void main(String args[]) {
        Arith a1=new Arith();
        a1.show();
}

Addtion program using matrix


class Matrix1
{
        public static void main(String args[]) throws Exception
        {
                int row,col;
                int a[][] = new int[3][3];
                int i,j;
                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                System.out.print("Enter the Row and Column Value : ");
                row = Integer.parseInt(br.readLine());
                col = Integer.parseInt(br.readLine());
               for(i=0;i<row;i++)
                {
                        for(j=0;j<col;j++)
                        {
                                System.out.print("Enter a[" + i + "][" + j + "] : ");
                                a[i][j] = Integer.parseInt(br.readLine());
                        }
                }
                System.out.println("The Given Matrix is ");
                for(i=0;i<row;i++)
                {
                        for(j=0;j<col;j++)
                        {
                                System.out.print(a[i][j] + "\t");
                        }
                        System.out.println();
                }
        }
}

Arrays


import java.io.*;
class Sort
{
        public static void main(String args[]) throws Exception
        {
                int a[] = new int[10];
                int n,i,j,temp;
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                System.out.print("Enter the value of n : ");
                n = Integer.parseInt(br.readLine());
                System.out.println(n);
                for(i=0;i<n;i++)
                {
                        System.out.print("Enter a[" + i + "] : ");
                        a[i] = Integer.parseInt(br.readLine());
                }
                for(i=0;i<n;i++)
                {
                        for(j=i+1;j<n;j++)
                        {
                                if(a[i] > a[j])
                                {
                                        temp = a[i];
                                        a[i] = a[j];
                                        a[j] = temp;
                                }
                        }
                }
                System.out.println("Sorted Values are");
                for(i=0;i<n;i++)
                        System.out.println(a[i]);
        }
}