program to demonstrate basic concept of class methods and objects in java

 


Program to demonstrate basic concept of Class, Methods and Objects:

Problem Statement :

     Create a class employee that includes three pieces of a information as instance variables- first name, and last name and a monthly salary. Your class should have a constructor that initializes the 3 instance variable. Provide a set and to get method for each instancevariables. If the monthly salary is not positive, then set it to zero. Write a test application named Employee Test that demonstrates class Employee' capabilitie. Create a 2 Employee objects and display the each object yearly salary. Then each Employee a 10% raise and display the each Employee's yearly salary again.

Introduction

·       Object - Objects have states and behaviors.

  

·    Class - A class can be defined as a template/blueprint that describes the  behavior/state that the object of its type supports. 

·       Methods - A method is basically a behavior. A class can contain many methods.It is in methods where the logics are written, data is manipulated and all the actions are executed.

import java.io.*;
import java.util.*;
public class emp {
    String fnm,lnm;
    double msal=0,ysal=0,rais=0,fsal=0;
    Scanner s=new Scanner(System.in);
    public emp()
    {
        fnm="abc";
        lnm="xyz";
        msal=0.0;
    }
    public void get()
    {
        System.out.println("enter first name");
        fnm=s.next();
        System.out.println("enter lasst name");
        lnm=s.next();
        System.out.println("enter monthly salary");
        msal=s.nextInt();
    }
    public void set(){
        System.out.println("first nmae:"+fnm);
        System.out.println("last name:"+lnm);
        System.out.println("mont salary:"+msal);
        System.out.println("yerly salary:"+ysal);
        System.out.println("salary after raise of 10%:"+rais);
        System.out.println("final salary:"+fsal);
    }
    public void cal()
    {
        ysal=(msal*12);
        rais=(ysal*10)/100;
        fsal=ysal+rais;
    }
    public static void main(String arg[])
    {
        System.out.println("enter detsils of two employee");
        emp e=new emp();
        emp e1=new emp();
        e.get();
        e.cal();
        System.out.println("   ");
        e1.get();
        e1.cal();
       
        System.out.println("******detail information of an employee****");
        e.set();
        System.out.println("     ");
        e1.set();

    }

   
}


out put

enter detsils of two employee

enter first name

ajay

enter lasst name

patil

enter monthly salary

5000

   

enter first name

vijay

enter lasst name

patil

enter monthly salary

8000

******detail information of an employee****

first nmae:ajay

last name:patil

mont salary:5000.0

yerly salary:60000.0

salary after raise of 10%:6000.0

final salary:66000.0


first nmae:vijay

last name:patil

mont salary:8000.0

yerly salary:96000.0

salary after raise of 10%:9600.0

final salary:105600.0

2 Comments

Previous Post Next Post