Auto AdSense

Sunday, 30 November 2014

A First Python Program

  • First Python Program:
    #!/usr/bin/python

    print "Hello, Python!";
  • Quotation in Python:
    word = 'word'
    sentence = "This is a sentence."
    paragraph = """This is a paragraph. It is
    made up of multiple lines and sentences."""
  • Comments in Python:
    #!/usr/bin/python

    # First comment
    print "Hello, Python!"; # second comment
  • Waiting for the User:
    #!/usr/bin/python

    raw_input("\n\nPress the enter key to exit.")
  • Multiple Statement Groups as Suites:
    if expression :
    suite
    elif expression :
    suite
    else :
    suite
  • Assigning Values to Variables:
    #!/usr/bin/python

    counter = 100 # An integer assignment
    miles = 1000.0 # A floating point
    name = "John" # A string

    print counter
    print miles
    print name
  • Multiple Assignment:
    a = b = c = 1
    a, b, c = 1, 2, "john"
  • Python Strings:
    #!/usr/bin/python

    str = 'Hello World!'

    print str # Prints complete string
    print str[0] # Prints first character of the string
    print str[2:5] # Prints characters starting from 3rd to 5th
    print str[2:] # Prints string starting from 3rd character
    print str * 2 # Prints string two times
    print str + "TEST" # Prints concatenated string
  • Python Lists:
    #!/usr/bin/python

    list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
    tinylist = [123, 'john']

    print list # Prints complete list
    print list[0] # Prints first element of the list
    print list[1:3] # Prints elements starting from 2nd till 3rd
    print list[2:] # Prints elements starting from 3rd element
    print tinylist * 2 # Prints list two times
    print list + tinylist # Prints concatenated lists
  • Python Tuples:
    #!/usr/bin/python

    tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
    tinytuple = (123, 'john')

    print tuple # Prints complete list
    print tuple[0] # Prints first element of the list
    print tuple[1:3] # Prints elements starting from 2nd till 3rd
    print tuple[2:] # Prints elements starting from 3rd element
    print tinytuple * 2 # Prints list two times
    print tuple + tinytuple # Prints concatenated lists
  • Python Dictionary:
    #!/usr/bin/python

    dict = {}
    dict['one'] = "This is one"
    dict[2] = "This is two"

    tinydict = {'name': 'john','code':6734, 'dept': 'sales'}


    print dict['one'] # Prints value for 'one' key
    print dict[2] # Prints value for 2 key
    print tinydict # Prints complete dictionary
    print tinydict.keys() # Prints all the keys
    print tinydict.values() # Prints all the values

Saturday, 29 November 2014

Java Program to Write file using FileOutputStream

      public class WriteFile
   {
     public static void main(String[] args)
     {
       String strFilePath = "C://FileIO//demo.txt";
       try
       {
         FileOutputStream fos = new FileOutputStream(strFilePath);
         byte b = 01;
         /*
         * To write byte data to a file, use
         * void write(int i) method of Java FileOutputStream class.
         * This method writes given byte to a file.
         */
        
         fos.write(b);
        
         /*
         * Close FileOutputStream using,
         * void close() method of Java FileOutputStream class.
         *
         */
         fos.close();
       }
       catch(FileNotFoundException ex)
       {
         System.out.println("FileNotFoundException : " + ex);
       }
       catch(IOException ioe)
       {
          System.out.println("IOException : " + ioe);
       }
     }
   }   

Java Program to Write byte array to file using BufferedOutputStream

       public class WriteByteArrayToFile
   {
     public static void main(String[] args)
     {
       String strFileName = "C:/FileIO/BufferedOutputStreamDemo";
       BufferedOutputStream bos = null;
       try
       {
         //create an object of FileOutputStream
         FileOutputStream fos = new FileOutputStream(new File(strFileName));
        
         //create an object of BufferedOutputStream
         bos = new BufferedOutputStream(fos);
        
         String str = "BufferedOutputStream Example";
        
         /*
         * To write byte array to file use,
         * public void write(byte[] b) method of BufferedOutputStream
         * class.
         */
         System.out.println("Writing byte array to file");
        
         bos.write(str.getBytes());
        
         System.out.println("File written");
       }
       catch(FileNotFoundException fnfe)
       {
         System.out.println("Specified file not found" + fnfe);
       }
       catch(IOException ioe)
       {
         System.out.println("Error while writing file" + ioe);
       }
       finally
       {
         if(bos != null)
         {
           try
           {
            
             //flush the BufferedOutputStream
             bos.flush();
            
             //close the BufferedOutputStream
             bos.close();
            
           }
           catch(Exception e){}
         }
       }
     }
   }   

Java Vector Example

       public class SimpleVectorExample
   {
     public static void main(String[] args)
     {
      
       //create a Vector object
       Vector v = new Vector();
      
       /*
       Add elements to Vector using
       boolean add(Object o) method. It returns true as a general behavior
       of Collection.add method. The specified object is appended at the end
       of the Vector.
       */
       v.add("1");
       v.add("2");
       v.add("3");
      
       /*
       Use get method of Java Vector class to display elements of Vector.
       Object get(int index) returns an element at the specified index in
       the Vector
       */
      
       System.out.println("Getting elements of Vector");
       System.out.println(v.get(0));
       System.out.println(v.get(1));
       System.out.println(v.get(2));
     }
   }    

Java Example on TreeSet

      public class SimpleJavaTreeSetExample
   {
     public static void main(String[] args)
     {
      
       //create object of TreeSet
       TreeSet tSet = new TreeSet();
      
       /*
       Add an Object to TreeSet using
       boolean add(Object obj) method of Java TreeSet class.
       This method adds an element to TreeSet if it is not already present in TreeSet.
       It returns true if the element was added to TreeSet, false otherwise.
       */
      
       tSet.add(new Integer("1"));
       tSet.add(new Integer("2"));
       tSet.add(new Integer("3"));
      
       /*
       Please note that add method accepts Objects. Java Primitive values CAN NOT
       be added directly to TreeSet. It must be converted to corrosponding
       wrapper class first.
       */
      
       System.out.println("TreeSet contains.." + tSet); }
   }