kirran

Friday, June 1, 2012

A sample Java program

Whenever we are writing JAVA program, it is better to write methods (nothing but functions in C  (or )  Member functions in C++ ) inside the class ,where as in case of C++,  you can write member functions inside the class or outside the class.

Generally there are many approaches to write a program,but the programmer should think in such away that code should be optimized,this is most important thing.

If we declare variables outside of a function then,those variables are called instance variables or non static variables.these variables are applicable to all the functions

If we declare variables inside a function then ,those variables are called Local variables.

Here I am going to illustrate  a basic program of an employee details like his DA,HRA,ENO (employe number) ,TA ,Loans to be paid , ENAME (employe name) , EPLACE (employee place).



import java.util.*;    

/* we are using scanner in the program,to  take input from keyboard */

public class Employee
 {
   
    int DA,HRA,ENO,TA,LOANS;  /*Initialising variables
   
    String ENAME,EPLACE;    
   
    void input()
  {

        System.out.println("PLEASE ENTER YOUR DETAILS   DA,HRA,ENO,TA,LOANS,ENAME,EPLACE");

        Scanner S = new Scanner(System.in);

        DA = S.nextInt();
        HRA = S.nextInt();
        ENO = S.nextInt();
        TA = S.nextInt();
        LOANS = S.nextInt();
        ENAME = S.next();
        EPLACE = S.next();   
       
    }
  void output()
  {
      System.out.println("DA IS "+DA);
      System.out.println("HRAA IS"+HRA);
      System.out.println("EMPLOYEE NUM IS"+ENO);
      System.out.println("TA IS"+TA);
      System.out.println("Loan IS "+LOANS);
      System.out.println("EMPLOYEE NAME IS"+ENAME);
      System.out.println("EMPLOYEE PLACE IS "+EPLACE);
     
  }
 
 int  total()

 {
     return(DA+HRA+TA+LOANS);
   
 }

 void NETSAL(int GROSS)

 {
     int NETSAL =GROSS - LOANS;
     System.out.println("YOUR GROSS SALARY IS "+GROSS);
     System.out.println("YOUR NET SALARY IS "+NETSAL);
 }


    public static void main(String[] args) 
{
       
        Employee E = new Employee();
       
        E.input();
        E.output();
        int GROSS = E.total();
        E.NETSAL( GROSS);

  }

}

OUTPUT: 

PLEASE ENTER YOUR DETAILS DA,HRA,ENO,TA,LOANS,ENAME,EPLACE
101
1200
22
1333
2000
kirran
hyderabad
DA IS 101
HRA IS1200
EMPLOYEE NUM IS22
TA IS1333
Loan IS 2000
EMPLOYEE NAME IS kirran
EMPLOYEE PLACE IS hyderabad
YOUR GROSS SALARY IS 4634
YOUR NET SALARY IS 2634.


In class, all methods such as input,output,total ,gross salary are defined and all variables are declared.In the main function we are creating object and calling those methods .










 




2 comments:


  1. It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training
    Selenium training in bangalore

    ReplyDelete