Tuesday, 26 June 2012

(June 25' 2012 & June 26' 2012)

We were taught about Applets these days. I was quite bored  writing the codes and seeing the  output on the console and dancing with joy that the code actually worked, but now began the interesting part when we started with the graphical side of java.
                An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a Java-compatible web browser. Applets use the AWT.
     Applet provides all necessary support for applet execution, such as starting and stopping. It also provides methods that load and display images, and methods that load and play audio clips.

      Example :  A simple example that shows colored text using java applet and color classes.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;


public class ColorHello extends Applet{
      
        public void paint(Graphics g){
              
                g.setColor(Color.blue);
             
                g.drawString("Hello World...",30,180);
        }
}


OUTPUT:-








Friday, 22 June 2012

(June 18' 2012 - June 22' 2012)

First 3 days of this week we learned about Generics & Collections and on 21st-22nd, we had tests and brush-up sessions.TheA collection is a group of data manipulate as a single object. term generics means parametrized types. Using generics, it is possible to create
a single class that automatically works with different types of data.

Example:

import java.util.ArrayList;
import java.util.List;
public class GenericList {
   
public static void main(String[] args) {
     
List list=new ArrayList();
         list
.add("aaa");
         list
.add("bbb");
         list
.add("fff");
         list
.add("ccc");
         list
.add("ddd");
         
System.out.println(list);
   
}
}
 
Output:

[aaa, bbb, fff, ccc, ddd]


Collections A collection is a group of data manipulate as a single object. These are similar to C++'s Standard Template Library (STL) and contain only Objects (reference types).

Example:-

import java.util.HashSet;
import java.util.Collections;

public class Collection{

  public static void main(String[] args) {
 
    HashSet hashSet = new HashSet();
  
    hashSet.add(new Long("92"));
    hashSet.add(new Long("42"));
    hashSet.add(new Long("23"));
    hashSet.add(new Long("32"));
  
    Object obj = Collections.min(hashSet);
  
    System.out.println("Minimum Element of Java HashSet is : " + obj);
  }
}


OUTPUT:

Minimum Element of Java HashSet is : 23





Friday, 15 June 2012

(June 14' 2012 - June 15' 2012)

We discussed the concept of Threads these 2 days. Java uses threads to enable the entire environment to be asynchronous. This helps reduce inefficiency by preventing the waste of CPU cycles. Also threads exist in several states such as running, ready to run, suspended, resumed, blocked.

          Creating a Thread: This can be do.ne by instantiating an object of type Thread. For this we can,
i) Implement the Runnable interface ,or
ii) Extend the Thread class itself.

Example:

          public class Threads{
  public static void main(String[] args){
  Thread th = new Thread();
  System.out.println("Numbers are printing line by line after 2 seconds : ");
  try{
  for(int i = 1;i <= 5;i++)
    {
  System.out.println(i);
  th.sleep(2000);
  }
  }
  catch(InterruptedException e){
    System.out.println("Thread interrupted!");
  e.printStackTrace();
  }
  }
}


OUTPUT-

Numbers are printing line by line after 2 seconds :
1
2
3
4

5



   Similarly, our program can spawn as many threads as it needs.

Wednesday, 13 June 2012

(June 9' 2012 - June 13' 2012)

All these days, we had tests and brush-up sessions. Our trainers focused on making strong our previously done Java concepts. I also had decided about my project by now and have started researching about it. We will be starting new concepts from tomorrow onwards and hopefully will start working on my project this week..!!

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.