Friday, July 16, 2010

JWindow

JWindow
Java Swing Tutorial Explaining the JWindow Component. JWindow is
Swing’s version of Window and is descended directly from that class. Like
Window, it uses BorderLayout by default. Almost all Swing components are
lightweight except JApplet, JFrame, JDialog, and JWindow.

JWindow Source Code


public class JWindowDemo extends JWindow {

 private int X = 0;
 private int Y = 0;
 public JWindowDemo() {
  setBounds(60, 60, 100, 100);
  addWindowListener(new WindowAdapter() {

   public void windowClosing(WindowEvent e) {
    System.exit(0); // An Exit Listener
   }
  });
  // Print (X,Y) coordinates on Mouse Click
  addMouseListener(new MouseAdapter() {

   public void mousePressed(MouseEvent e) {
    X = e.getX();
    Y = e.getY();
    System.out.println("The (X,Y) coordinate of window is ("
      + X + "," + Y + ")");
   }
  });
  addMouseMotionListener(new MouseMotionAdapter() {

   public void mouseDragged(MouseEvent e) {
    setLocation(getLocation().x + (e.getX() - X),
      getLocation().y + (e.getY() - Y));
   }
  });
  setVisible(true);
 }
 public static void main(String[] args) {
  new JWindowDemo();
 }
}
Output


Java JWindow Hierarchy

javax.swing


Class JWindow 


java.lang.Object

java.awt.Component

java.awt.Container

java.awt.Window
All Implemented Interfaces:

Accessible, ImageObserver, MenuContainer, Serializable

Direct Known Subclasses: 

BasicToolBarUI.DragWindow, Dialog, Frame, JWindow

JWindow Constructor


Window(Frame owner)


Constructs a new invisible window with the specified Frame as its owner.

Window(Window owner)


Constructs a new invisible window with the specified Window as its owner.

Window(Window owner, GraphicsConfiguration gc)


Constructs a new invisible window with the specified window as its owner and a
GraphicsConfiguration of a screen device.

JSwings - JFrames

JFrame
Java Swing Tutorial Explaining the JFrame class. The components added to the
frame are referred to as its contents; these are managed by the contentPane. To
add a component to a JFrame, we must use its contentPane instead.JFrame is a
Window with border, title and buttons. When JFrame is set visible, an event
dispatching thread is started. JFrame objects store several objects including a
Container object known as the content pane. To add a component to a JFrame, add
it to the content pane.

JFrame Features
It’s a window with title, border, (optional) menu bar and user-specified
components.

It can be moved, resized, iconified.

It is not a subclass of JComponent.

Delegates responsibility of managing user-specified components to a content
pane, an instance of JPanel.

Centering JFrame’s
By default, a Jframe is displayed in the upper-left corner of the screen. To
display a frame

at a specified location, you can use the setLocation(x, y) method in the JFrame
class. This

method places the upper-left corner of a frame at location (x, y).
The Swing API keeps improving with abstractions such as the
setDefaultCloseOperation method

for the JFrame

Crating a JFrame Window
Step 1: Construct an object of the JFrame class.
Step 2: Set the size of the Jframe.
Step 3: Set the title of the Jframe to appear in the title bar (title bar
will be blank if no title is set).


Step 4: Set the default close operation. When the user clicks the close
button, the program stops running.
Step 5: Make the Jframe visible.

How to position JFrame on Screen?
frame.setLocationRelativeTo( null );


JFrame Source Code


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JFrameDemo {

 public static void main(String s[]) {
  JFrame frame = new JFrame("JFrame Source Demo");
  // Add a window listner for close button
  frame.addWindowListener(new WindowAdapter() {

   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });
  // This is an empty content area in the frame
  JLabel jlbempty = new JLabel("");
  jlbempty.setPreferredSize(new Dimension(175, 100));
  frame.getContentPane().add(jlbempty, BorderLayout.CENTER);
  frame.pack();
  frame.setVisible(true);
 }
}
Output

Java JFrame Hierarchy

javax.swing


Class JFrame


java.lang.Object

java.awt.Component

java.awt.Container

java.awt.Window

java.awt.Frame

javax.swing.JFrame

All Implemented Interfaces:

Accessible, ImageObserver, MenuContainer, RootPaneContainer, Serializable,
WindowConstants

JFrame Constructor


JFrame()


Constructs a new frame that is initially invisible.

JFrame(GraphicsConfiguration gc)


Creates a Frame in the specified GraphicsConfiguration of a screen device and a
blank title.

JFrame(String title)


Creates a new, initially invisible Frame with the specified title.

JFrame(String title, GraphicsConfiguration gc)


Creates a JFrame with the specified title and the specified
GraphicsConfiguration of a screen device.

Friday, June 18, 2010

Salary Negotiation and General Questions

IT Interview Question and Answer
This is the collection of Interview Question and Answers, I have collected form different source, I try to give answer of all questions, if you are not satisfied or you think answers are not correct you can post your comment for that, I also appreciate you if you submit interview question and answers for VB6, VB.Net, C#, ASP, COM, DCOM, COM+, ASP.Net, Crystal Report, Oracle, SQL Server, MySql, PHP, XML, AJAX. DBA.
If you are in information technology (IT) industry and working as a software engineer or developer, database administrator, you may need to give interview very often, as retration rate of our industry is very high, so be prepare for that. Interviewing of technical job position is very difficult as you have to demonstrate your "soft skill" in terms of your communication skills, personality and your "hard skill" means programming, analytical, implementation knowledge.
Tips for success in technical interview
·         Get enough information about the organization and product.
·         Practicing for technical question answer
·         Practicing for personal, general topics, other than technical question
·         Review the job description and profile.
·         Be on time
·         Follow Up, don't let them forget about you
·         Be ready for technical test and hands on exercise
·         Avoid arrogance
·         Dress and personality
·         Ask question, show interest for the post.
·         Try to control the flow of interview, remain composed.
·         Show confident, don't be confused
·         Don't use jargon and tech term if you don't know the things in details.
·         Don't try to misguide the interviewee as they are very experienced.
Salary Negotiation for IT job.
This is the probably the weakest area of for technical person. They are not from the sales and are not good in negotiators. Here is come tips for salary negotiations.
·         Do a study of what is the salary trend.
·         Do not mentioned your salary expected on the resume.
·         Do not be harsh during salary negotiations.
·         Talk with the employer in what frequency does the hike happen.
·         take care of hidden cost attached in salary clarify what exactly you will get.
Salary trend in IT industry in India and Abroad.

Years of Experience
Salary in India CTC
Salary in Abroad CTC
Freshers
INR 60 K to 100 K
$ 40 K to 50 K
1 to 2 Years
INR 120 K to 240 K
$ 50 K to 55 K
3 to 4 Years
INR 300 K to 360 K
$ 55 K to 60 K
5 to 6 Years
INR 360 K to 480 K
$ 60 K to 65 K
7 to 8 Years
INR 480 K to 720 K
$ 70 K to 80 K
8 Years and above
As per negotiations
As per negotiations

Non IT/Software/Database and HR Round related questions
You must ready and prepare for some non technical question in interview like.
·         Can you say something about yourself?
·         Why do you want to leave the current company?
·         where do you see yourself after three years?
·         What are your positive and negative points?
·         How much you rate yourself in .Net and C# in the range of 1 to 10 ?
·         Do you work on Saturday and Sunday?
·         Tell about your family background ?
·         How much time will you need to join our company ?
·         What is notice period for your current company ?
·         How you handle pressure situation ?
·         Why I should hire you ?
·         Do you work late nights ?
·         Do you like to work as a team or as individual ?

Thursday, June 10, 2010

Structure as Argument

Structure Arguments
There are many objects defined throughout the library as structures of
varying and possibly large sizes. Due to the inefficiency of passing structures
by value (copying the whole structure to a function), structures are generally
passed to functions by reference. Because of this, there is always a potential
to pass an invalid pointer to a function which expects a pointer to a structure.
The following examples illustrate the right and wrong ways to use such a
function. Given a structure and a library function which initializes it,
typedef  struct
{
    int   a, b, c, d;
} big_struct;

void    initialize( struct  big_struct   *s )
{
    s->a = 1;   s->b = 2;   s->c = 3;    s->d = 4;
}
the incorrect method of using this function is:

int  main()
{
    big_struct   *s;

    initialize( s );   /* WRONG */
}
because the variable s is
an uninitialized pointer. The correct method is to define a structure variable,
not a pointer to a structure, and pass a pointer to the structure:

int  main()
{
    big_struct   s;

    initialize( &s );
}
Alternately, the incorrect example above could have been corrected by allocating
the s pointer
before calling initialize.

Demo Project

#include<stdio.h>
#include<conio.h>
 struct student
 {
 int rlno;
 char na[20];
 }; //structure for student data

struct student stu; //global variable for structure

//Function to recieve value of structure

void getdata()
 {
 printf("\n Enter Roll Num ");
 scanf("%d",&stu.rlno);
 printf("\n Enter Name ");
 scanf("%s",stu.na);
 }
 //function to display values of structure

void showdata()
 {
 // printf("\n Name is %s ",stu.na);
 // printf("\n Roll Num is %d ",stu.rlno);
 printf("\n%d\t\t%s\n",stu.rlno,stu.na);
 }

 void main()
 {
 int ch; // var for choice
 int choice;
 int rl;
 FILE *fp, *fp1;//var to open a file
 while(choice!=5)
 {
 clrscr();
 printf("\n 1. Add data ");
 printf("\n 2. List Records ");
 printf("\n 3. Delete Record ");
 printf("\n 4. Modify Record ");
 printf("\n 5. Quit");
 printf("\n Enter your choice ");
 scanf("%d",&choice);
 switch(choice)
 {
 case 1:
 fp=fopen("stud.txt","a"); //openning a file
 while(ch!=0)//loop
 {
 getdata();//getting data
 fwrite(&stu,sizeof(stu),1,fp);//writing to a file
 printf("\n Press 1 to add more, 0 to quit ");//prompt the user for cont...
 scanf("%d",&ch);
 }
 fclose(fp);//closing a file
 break;
 case 2:
 fp=fopen("stud.txt","r"); //reopening a file
 printf("\n Roll Num\t\tName");
 printf("\n======================================\n");
 while(fread(&stu,sizeof(stu),1,fp)==1)//loop for reading data
 {
 showdata();//display a data
 }
 fclose(fp);//closing a file
 getch();
 break;
 case 3:
 fp=fopen("stud.txt","r");
 fp1=fopen("temp.txt","a");
 printf("\n Enter rollnum u wanna delete ");
 scanf("%d",&rl);
 while(fread(&stu,sizeof(stu),1,fp)==1)
 {
 if(stu.rlno==rl)
 {
 printf("\n Record found ");
 showdata();
 printf("\n Press any Key to cont...");
 getch();
 }
 else
 {
 fwrite(&stu,sizeof(stu),1,fp1);
 }
 }
 fclose(fp);
 fclose(fp1);
 remove("stud.txt");
 rename("temp.txt","stud.txt");
 break;
 case 4:
 fp=fopen("stud.txt","r");
 fp1=fopen("temp.txt","a");
 printf("\n Enter rollnum u wanna Modify ");
 scanf("%d",&rl);
 while(fread(&stu,sizeof(stu),1,fp)==1)
 {
 if(stu.rlno==rl)
 {
 printf("\n Record found ");
 showdata();
 printf("\n Press any Key to enter new Values");
 getch();
 getdata();
 fwrite(&stu,sizeof(stu),1,fp1);
 }
 else
 {
 fwrite(&stu,sizeof(stu),1,fp1);
 }
 }
 fclose(fp);
 fclose(fp1);
 remove("stud.txt");
 rename("temp.txt","stud.txt");
 break;
 case 5:
 printf("\n Thanks for using this software ");
 getch();
 break;
 default:
 printf("\n Invalid Option ");
 getch();
 }//switch
 }//while
 }

Monday, June 7, 2010

Multiply 2 Matrices

void main()
{
    int m1[10][10],i,j,k,m2[10][10],add[10][10],mult[10][10],r1,c1,r2,c2;
    printf("Enter 
number of rows and columns of first matrix MAX 10\n");
    scanf("%d%d",&r1,&c1);
    printf("Enter 
number of rows and columns of second matrix MAX 10\n");
    scanf("%d%d",&r2,&c2);
    if(r2==c1)
    {
        printf("Enter 
rows and columns of First matrix \n");
        printf("Row 
wise\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                scanf("%d",&m1[i][j]);
        }
        printf("You 
have entered the first matrix as follows:\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                printf("%d\t",m1[i][j]);
            printf("\n");
        }
        printf("Enter 
rows and columns of Second matrix \n");
        printf("Again 
row wise\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                scanf("%d",&m2[i][j]);
        }
        printf("You 
have entered the second matrix as follows:\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                printf("%d\t",m2[i][j]);
            printf("\n");
        }
        if(r1==r2&&c1==c2)
        {
            printf("Now 
we add both the above matrix \n");
            printf("The 
result of the addition is as follows;\n");
            for(i=0;i<r1;i++)
            {
                for(j=0;j<c1;j++)
                {
                    add[i][j]=m1[i][j]+m2[i][j];
                    printf("%d\t",add[i][j]);
                }
                printf("\n");
            }
        }
        else
        {
            printf("Addition 
cannot be done as rows or columns are not equal\n");
        }
        printf("Now 
we multiply both the above matrix \n");
        printf("The 
result of the multiplication is as follows:\n");
        /*a11xA11+a12xA21+a13xA31 
a11xA12+a12xA22+a13xA32 a11xA13+a12xA23+a13xA33*/
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                mult[i][j]=0;
                for(k=0;k<r1;k++)
                {
                    mult[i][j]+=m1[i][k]*m2[k][j];
                    /*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/
                }
                printf("%d\t",mult[i][j]);
            }
            printf("\n");
        }
        getch();
    }
    else
    {
        printf("Matrix 
multiplication cannot be done");
    }
}


Binary Search



  1. int main() 
  2. {
  3.   int a[20] = {0};
  4.   int n,

  5. i, j, temp;   int *beg,

  6. *end, *mid, target;
      printf(" enter the total integers you want to enter (make it less then 20):\n");
  7.   scanf("%d", &n);
  8.   if (n >= 20) return 0;   //  ouch!
  9.   printf(" enter the integer array elements:\n" );
  10.   for(i = 0; i < n; i++)
  11.   {
  12.     scanf("%d", &a[i]);
  13.   }
  14. // sort the loaded array, a  must for binary search!
  15.   // you can apply qsort or  other algorithms here
  16.   for(i = 0;  i < n-1; i++)
  17.   { 
  18.     for(j = 0; j < n-i-1; j++)
  19.     {
  20.       if (a[j+1] < a[j])
  21.       {
  22.         temp = a[j];
  23.         a[j] = a[j+1];
  24.         a[j+1] = temp;
  25.       }
  26.  }
  27.  }
  28. printf(" the sorted numbers  are:");
  29.  for(i = 0; i < n; i++)
  30.   {
  31.  printf("%d ", a[i]);


  32.   }
      // point to beginning and end of the array

  33.   beg = &a[0];
      end = &a[n];  // use n = one element past the loaded array!
  34.   printf("\n beg points to address %d and end to %d",beg, end);  // test
  35.   // mid should point somewhere in the middle of these addresses
  36.   mid = beg += n/2;
  37.   printf("\n mid points to address %d", mid);  // test
  38.   printf("\n enter the number to be searched:");
  39.   scanf("%d",&target);
  40.   // binary search, there is an AND in the middle of while()!!!


  41.   while((beg <= end) && (*mid != target))
      {
  42.     // is the target in lower or upper half?
  43.   if (target < *mid)
  44.   {
  45.       end = mid - 1;     // new end
  46.   n = n/2;
  47.  mid = beg += n/2;  // new middle
  48.     }
  49.     else
  50.     {
  51.  beg = mid + 1;     // new beginning
  52.   n = n/2;
  53.   mid = beg += n/2;  // new middle     
  54. }
  55.   }
  56.  // did you find the target?


  57.   if (*mid == target)
      {
  58.  printf("\n %d found!", target);
  59.   }
  60.   else
  61.   {
  62. printf("\n %d not found!", target);
  63.  }
  64.   getchar();  // trap enter
  65.  getchar();  // wait
  66.   return 0;
  67. }