christmas tree using package


//Write a program in Java to show christmas tree using package.

//First create a package

package pack3;
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 with tree.java in bin

//Compile the program with this command like below:

 

E:\jdk1.6.0\bin>javac -d e:\jdk1.6.0\bin tree.java

 

//Now import the package e.g

import pack3.tree;
class christtree
{
public static void main(String args[])
{
tree a=new tree();
a.disp();
a.disp();
a.disp();
a.disp();
a.root();
}
}

//Save the program and compile it as below:

output:

java9

Related Posts

Comments are closed.