Thursday, February 18, 2010

Package Example Program in Java

Java Package Example Codes

Create a Package "packName"

Write a java Program Name is "Employee"

-------------------------------------------------------

package packName;

// the classes in this file are part of this package

import java.util.*;

// import statements come after the package statement

/**
* @version 1.10 1999-12-18
* @author Cay Horstmann
*/
public class Employee
{
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
// GregorianCalendar uses 0 for January
hireDay = calendar.getTime();
}

public String getName()
{
return name;
}

public double getSalary()
{
return salary;
}

public Date getHireDay()
{
return hireDay;
}

public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}

private String name;
private double salary;
private Date hireDay;
}

----------------------------------------------------------

import packName.*;
// the Employee class is defined in that package

import static java.lang.System.*;

/**
* This program demonstrates the use of packages.
*/
public class PackageTest
{
public static void main(String[] args)
{
// because of the import statement, we don't have to use cpackName.Employee here
Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);

harry.raiseSalary(5);

// because of the static import statement, we don't have to use System.out here
out.println("name=" + harry.getName() + ",salary=" + harry.getSalary());
}
}


0 comments:

About This Blog

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP