Friday, 8 June 2012

Friday (June 8' 2012)

Today, we were taught about Exception Handling. An exception is a run-time error. Exception Handling is managed by 5 keywords - try, throw, throws, catch and finally. All exception types are subclass of built-in class "Throwable". This concept has 2 advantages:-
*We can fix errors ourselves.
*It prevents programs from automatically terminating.

Example:-

public class ExceptionHandling {
    public static void main(String args[])
    {
        int a,d;
        try{
            d=11;
            a=d/0;
            System.out.println("This wont be printed.");
        }
        catch(ArithmeticException e)   
        {
            System.out.println("Division by Zero");
        }
            System.out.println("This is an after Catch statement");
    }
    
}




OUTPUT:




Division by Zero
This is an after Catch statement




Tuesday, 5 June 2012

Tuesday (June 5' 2012)

Today we were taught "How to Import a Package in ECLIPSE?" and about Interfaces . We can create as many classes in a package as we want and then by using that package in a separate class, we can call all those classes that have been previously created. Creating our own package saves memory, as the package stores all those classes inside it a separate buffer memory.
                                                         Using the keyword interface, you can fully abstract a class’ interface from its implementation.That is, using interface, you can specify what a class must do, but not how it does it.Using the keyword interface, you can fully abstract a class’ interface from its implementation.That is, using interface, you can specify what a class must do, but not how it does it.


Example:-

1.Creating a Package and Importing it in a separate class.

//Creating Package

package mypackage;
    public class Normal
{
        public void display()
        {
        System.out.println("Hello World");
        }
}

//Importing Package


import mypackage.*;
public class Myclass
{
    public static void main(String []args)
    {
        Normal n=new Normal();
        n.display();
    }
}
       

OUTPUT:
                    Hello World

Monday, 4 June 2012

Monday (June 4' 2012)

A whole new week starts. Got up at 6 a.m, was at the bus stand by 7a.m. and finally reached Chandigarh at about 9:30 a.m. Today, we were taught about the concepts of Vectors, String Buffer, Inheritance and use of Super keyword  in case of Java. Vector class can be used to create a generic dynamic array known as vector that can hold objects of any type and any number. While String creates strings of fixed length, String Buffer created strings of flexible length. Inheritance in Java has same concept as Inheritance in C++ , though Java do not support multiple and hybrid types of Inheritance.


Example:-

1. Java code showing use of Super keyword and also involving concept of Inheritance.


import java.util.*;
public class abc {
    int a,b;
    abc(int x,int y)
    {
        a=x;
        b=y;
    }
    void show()
    {
        System.out.println("A="+a);
        System.out.println("B="+b);
    }
}
    class Area extends abc
    {
        Area()
        {
            super(4,6);
        }
            void calarea()
            {
                int ar=a*b;
                System.out.println("Area="+ar);
            }
    }
class Alpha
{
    public static void main(String [] args)
    {
        Area obj=new Area();
        obj.show();
        obj.calarea();
    }
}



OUTPUT:-

   A=4
   B=6
   Area=24

Friday, 1 June 2012

Friday (June 1' 2012)

This was one of the hottest and the most tiring day since my training started. We had an Objective Type Test today to check if we had so far understood the concepts of Java or not. It consisted of ten questions, covering topics like constants, variables, data types, operators, expressions , decision making and branching. It had some tricky questions, but went well finally. I would be posting the result as soon as we get the marks. In the evening, i left for my home(Ludhiana) . Reached by 9:45 p.m , and finally slept after having dinner.

Thursday, 31 May 2012

Thursday (May 31' 2012)

As it was declared India Bandh today, So most of the educational institutes stayed close. No classes for this day, except for few hours of self programming practice and some search on the Google about Project topics.

Wednesday, 30 May 2012

Wednesday (May 30 '2012)

Today, @HCL  i was taught STRINGS in java. At the end of today's class, i was able to do String Manipulation, which is the most common part of many Java programs. Now, i know which package is imported to perform String related functions in a Java program, and how to use functions like- Concatenation of 2 strings, changing case of strings, replacing a part or whole of the string with another set of characters, how to display a sub-string , finding the length of a string and comparison of 2 strings.

Examples:

1. Concatenation of 2 strings.

              import java.lang.String;
            public class String1
               {
                 public static void main(String args[])
                    {
                      String S = " Java " ;
                      String R = " Core " ;
                      String T = (S.concat(R));
                 System.out.println(T);
                     }
                }

OUTPUT: Java Core


2. Replacing characters in a string.

                    
import java.lang.String;
            public class String2
               {
                 public static void main(String args[])
                    {
                      String S = "Hydrophobia" ;
                      R = S .replace ("o" , "a");
                  System.out.println(R);
                     }
                }
 
OUTPUT: Hydraphabia

3. Displaying a Sub-string.
     
                      import java.lang.String;
            public class String3
               {
                 public static void main(String args[])
                    {
                      String S = "Hydrophobia" ;
                      R = S .substring (1,5);
                  System.out.println(R);
                     }
                }

 OUTPUT: ydro
          



                    
  

Tuesday, 29 May 2012

Tuesday (May 29' 2012)

Today , ARRAYS were discussed in the class. An Array is a group of contiguous or related data items that share a common name.  It enables us to develop concise and efficient programs. Arrays can be of any variable type. This lecture covered - Creating arrays, Declaration of arrays, Initialization of arrays , One Dimensional arrays, Multidimensional arrays , etc.


Examples:

1. Java code for finding the Largest & Smallest number in an Array

   import java.util.*;
    public class test1
    {
        public static void main(String [ ] args)
        {
            int i;
            int numbers []=new int[]{4,65,89,34,71,70,49,22,56,13};
            int smallest= numbers[0];
            int largest= numbers[0];
                    for(i=0;i<numbers.length;i++)
                    {
                        if(numbers[i]>largest)
                             largest = numbers[i];
                     
                          else if(numbers[i]<smallest)
                             smallest=numbers[i];
                           
                      }      
System.out.println("The largest number in this array is:" + largest);
 System.out.println("The smallest number in this array is:" + smallest);
        }
    }




  OUTPUT:
The largest number in this array is:89
The smallest number in this array is:4