User Defined Package Example Java Program

Definition

A Java package is a technique for organizing Java classes into namespaces similar to the modules of Modula, providing modular programming in Java. Java packages can be stored in compressed files called JAR files, allowing classes to be downloaded faster as groups rather than individually. Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality. A package provides a unique namespace for the types it contains. Classes in the same package can access each other's package-private and protected members.

User Defined Package Example Program

/*package treepackage;
public class tree
{
public void disp()
{
int i,j,k,l;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j?)
{
System.out.print(? ?);
}
for(k=1;k<=i;k++)
{
System.out.print(?*?);
}
for(l=i-1;l>=1;l?)
{
System.out.print(?*?);
}
System.out.print(?\n?);
}}
public void root()
{
int k,i,j;
for( i=1;i<=5;i++)
-{
for( j=1;j<5;j++)
{
System.out.print(? ?);
}
for(k=1;k<=3;k++)
{
System.out.print(?*?);
}
System.out.println(? ?);
}
}
}//Save this package as tree.java
*/
import treepackage.tree;
class UserDefinedPackageDemo{
	public static void main(String args[]){
		UserDefinedPackageDemo obj=new UserDefinedPackageDemo();
		obj.disp();
		obj.disp();
		obj.disp();
		obj.disp();
		obj.root();
	}
}

Sample Output

Output is:
            *
	   ***
	  *****
	 *******
	*********
            *
	   ***
	  *****
	 *******
	*********
            *
	   ***
	  *****
	 *******
	*********
            *
	   ***
	  *****
	 *******
	*********
	   ***
	   ***
	   ***
	   ***
	   ***