Search This Blog

Thursday, 30 January 2014

writing to a file in java

/* program to illustrate the use of FileWriter to write to a file */


package javaio;

import java.io.FileWriter;
import java.io.*;

class MainClass
{
    public static void main(String[] args)
    {
        String fName="E://MyFile.txt";
        String content="sanjay kumar singh";
        try
        {
         /* This one is the first method  
          * FileWriter f=new FileWriter(fName,true);
            BufferedWriter bw=new BufferedWriter(f);
            bw.write(content);
            f.write(content);
            f.close();*/
            
            File f=new File(fName);
            FileWriter f2=new FileWriter(f,true);
            f2.write(content);
            System.out.println("Writing in a file is successful ...");
            f2.close();
        }
        catch(Exception e)
        {
            System.err.println("Error is :"+e.getMessage());
        }
    }
}

output

run:
Writing in a file is successful ...
BUILD SUCCESSFUL (total time: 0 seconds)


create a text file in java

/*program which creates a text file in your hard drive */


package file_create;

import java.io.*;

class MyFile
{
    public static void main(String[] args)
    {
        try
        {
            File f;
            f=new File("E://MyFile.txt");
            if(!f.exists())
            {
                f.createNewFile();
                System.out.println("File created successfully...");
            }
            else
                System.out.println("File already exits...");
        }
        catch(Exception e)
        {
            System.out.println("Error : "+e.getMessage());
        }
            
    }
}

output


run:
File created successfully...
BUILD SUCCESSFUL (total time: 0 seconds)

After that , you check your hard drive of the system,a file exits in E:/ drive with name MyFile.txt.

Wednesday, 29 January 2014

clone method in java

/* program to illustrate clone() method */


package clonemethod;
class Student implements Cloneable
{
    int roll;
    String name;
    Student(int roll,String name)
    {
        this.roll=roll;
        this.name=name;
        System.out.println("details are : "+roll+" "+name);
    }
public Object clone()throws CloneNotSupportedException{  
return super.clone();  
}  

public static void main(String args[]){  
try{  
Student s1=new Student(21,"sanjay");  
  
Student s2=(Student)s1.clone();  
  
System.out.println(s1.roll+" "+s1.name);  
System.out.println(s2.roll+" "+s2.name);  
  
}catch(CloneNotSupportedException c){}  
  
}  
}  

output

details are : 21 sanjay
21 sanjay
21 sanjay

Tuesday, 28 January 2014

constructors in java

/* program illustrating the concept of three constructors in java*/


package constructors;

class Parent
{
    int n;
    String s;
    Parent()  /*Default Constructor Calls*/
    {
        n=2;
        s="sanjay";
        System.out.println("Number is :"+n+" and name is :"+s);
    }
    Parent(int a,String s)/*Parametrised Constructor Calls*/
    {
       n=a;
       this.s=s;
       System.out.println("Number is :"+n+" and name is :"+s);
    }
    Parent(Parent p) /*Copy constructor Calls*/
    {
      n=p.n;
      this.s=p.s;
      System.out.println("Number is :"+n+" and name is :"+s);  
    }
}
class MainClass
{
    public static void main(String[] args)
    {
        Parent a=new Parent();
        Parent b=new Parent(3,"neeraj");
        Parent c=new Parent(b);
    }
}

output


Number is :2 and name is :sanjay
Number is :3 and name is :neeraj
Number is :3 and name is :neeraj

Scanner Class in java

/* Simple intro of a student using Scanner class in java */


import java.util.*;

class Student
{
    public static void main(String[] args)
    {
        int roll;
        String name,branch;
        Scanner s=new Scanner(System.in);
        System.out.println("Enter roll number : ");
        roll=s.nextInt();
//         System.out.println("Enter marks number : ");
//         float f=s.nextFloat();
        System.out.println("Enter branch of the student : ");
        branch=s.next();
        System.out.println("Enter name of the student : ");
        name=s.next();
        System.out.println("Name is :"+name);
        System.out.println("Branch is : "+branch);
        System.out.println("Roll no is : "+roll);
        
    }
}

output


Enter roll number : 
21
Enter branch of the student : 
cse
Enter name of the student : 
sanjay 
Name is :sanjay
Branch is : cse
Roll no is : 21

instance of operator

/* Understanding the concept of instance_of_operator  */


package instanceof_operator;

class Parent
{
}
class Child extends Parent
{    
}

class MainClass
{
    public static void main(String [] args)
    {
        Parent p=new Parent();
        Child c=new Child();
        System.out.println(p instanceof Parent);
        System.out.println(c instanceof Child);
        System.out.println(c instanceof Parent);
        System.out.println(p instanceof Child);
        p=c;
        System.out.println(c instanceof Child);
        System.out.println(p instanceof Parent);
        System.out.println(c instanceof Parent);
        System.out.println(p instanceof Child);
    }
}

output


true
true
true
false
true
true
true
true

Inheritance Demo

/* Simple program to illustrate Inheitance in java */


package inheritanceexample;

class ParentOne
{
    public int Add(int a,int b,int c)
    {
        return a+b+c;
    }
    public float Sub(float a,int b)
    {
        return a-b;
    }
}

class Child extends ParentOne
{
    
}

class MainClass
{
    public static void main(String[] args)
    {
        Child c=new Child();
        
        int resAdd=c.Add(2,3,4);
        float resSub=c.Sub(3,2);
        System.out.println("Addition is :"+resAdd);
        System.out.println("Subtration is :"+resSub);
    }
}

output


Addition is :9
Subtration is :1.0

function overloading

/* program to illustrate Function Overloading */


package functionoverloading;
class Parent
{
   public int Larger(int a,int b,int c)
   {
       return (a>b?(a>c?a:c):(b>c?b:c));
   }
   public int Larger(int a,int b)
   {
       return (a>b?a:b);
   }
}
class Mainclass
{
    public static void main(String[] args)
    {
        Parent p=new Parent();
        System.out.println("Largest number is :"+p.Larger(2, 3));
       System.out.println("Largest no is :"+ p.Larger(1, 5, 7));
    }
}

output


Largest number is :3
Largest no is :7


Monday, 27 January 2014

using "final" keyword in Java

/* program to illustrate final keyword */

package finalkeyword;
class Parent //final class cannot be inherited.
{
   final int r=5;
   final void show()
   {
//       r=7; cannot be re defined because it is decalred as final
      System.out.println("Final method executes ...");
   }
}
class Child extends Parent
{
//    void show() method cannot be override because it is final in parent class
//    {
//       
//    }
   
}
class MainClass
{
    public static void main(String[] args)
    {
        Child c=new Child();
        c.show();
    }
}

output

using "this" keyword in Java

/* program to illustrate this keyword */

package thiskeyword;
class Parent
{
    int h;
    int n;
    String name;
    public void show(int h,int n,String name)
    {
       this.h=h;
       this.n=n;
       this.name=name;
       System.out.println("Values are : "+h+" "+n+" "+name);
    }
    public static void main(String [] args)
    {
        Parent p=new Parent();
        p.show(2,6,"sanjay");
    }
}

output

Scanner Class in Java

/* program for taking input using Scanner Class*/

package scanner;
import java.util.Scanner;

class Parent
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int a,b,c;
        System.out.println("Enter the first value :");
        a=sc.nextInt();
        System.out.println("Enter 2nd value :");
        b=sc.nextInt();
        c=a+b;
        System.out.println("Sum is :"+c);
        sc.close();
    }
   
}

output

Aggregation in Java

/* program to show the concept of Aggregation */

package aggregation;

class Aggregation2
{
     int roll;
     String name;
     String branch;
     Aggregation2(int roll,String name,String branch)
     {
         this.roll=roll;
         this.name=name;
         this.branch=branch;
     }
     public int getRollNo()
     {
         return roll;
     }
      public String getBranch()
     {
         return branch;
     }
       public String getName()
     {
         return name;
     } 
}

class Child
{
    int marks;
    int age;
    Aggregation2 ag;
    Child(int m,int a,Aggregation2 t)
    {
        marks=m;
        age=a;
        ag=t;
    }
    public void show()
    {
        System.out.println("The roll number of the student is : "+ag.getRollNo());
        System.out.println("Name is : "+ag.getName());
        System.out.println("Branch is : "+ag.branch);
        System.out.println("Student Age is : "+age);
        System.out.println("Student marks is : "+marks);
    }
}
class MainClass
{
    public static void main(String[] args)
    {
        Aggregation2 a=new Aggregation2(21,"sanjay","cse");
        Child c=new Child(78,14,a);
        c.show();
    }
}

output



2-D matrix in java

/*program for two dimensional matrix */

package array;
import java.util.Scanner;
class ArrayClass3
{
    public static void main(String[] args)
    {
       Scanner s=new Scanner(System.in);
       int[][]ar=new int[2][2];
       int i,j;
       System.out.println("Enter elements of the array :");
       for(i=0;i<2;i++)
       {
           for(j=0;j<2;j++)
           {
               ar[i][j]=s.nextInt();
           }
       }
       System.out.println("Two dimension matrix are are : ");
       for(i=0;i<2;i++)
       {
           for(j=0;j<2;j++)
           {
               System.out.print(ar[i][j]+" ");
           }
           System.out.println();
       }
    }
}

output

simple program for array

/* Entry of 5 integer values using the concept of array*/

package array;
import java.io.*;
class ArrayClass2
{
   public static void main(String[] args) throws IOException
   {
       int[] ar=new int[5];
       int i;
       InputStreamReader inp=new InputStreamReader(System.in);
       BufferedReader br=new BufferedReader(inp);
       System.out.println("Enter five integer values : ");
       for(i=0;i<5;i++)
       {
          ar[i]=Integer.parseInt(br.readLine()); 
       }
       System.out.println(" five integer values are : ");
       for(i=0;i<5;i++)
       {
          System.out.print(ar[i]+" ");
       }
   }
}

output

foreach loop in Java

/*program for foreach loop*/

package array;
// Foreach loop
 class ArrayClass
 {
     public static void main(String[] args)
     {
         int[] arr={1,2,3,4,5,6,7,8,9};
         for(int i:arr)
         {
             System.out.println(i);
         }
     }
 }

output

Popular Posts