You are on page 1of 4

Practical 20

X. Write a program to implement user defined packages in terms of creating a new


package and importing the same.
Class PackageDemo in MyPack Package 🡪
===========================================================================

package MyPack;
public class PackageDemo
{
public void show()
{
System.out.println("show() method in class PackageDemo of Package
MyPack");
}
}

Class PackageImport🡪
===========================================================================

import MyPack.PackageDemo;

class PackageImport
{
public static void main(String[] s)
{
PackageDemo pd=new PackageDemo();
pd.show();
}
}

Output:
XIII. 2. Define a package named myInstitute include class named as department with one
method to display the staff of that department. Develop a program to import this package in
a java application and call the method defined in the package.
Class department in myInstitute Package 🡪
===========================================================================
package myInstitute;
import java.util.Scanner;

public class department


{
String name;
int i=0;
Scanner in=new Scanner(System.in);

public void getstaff()


{
System.out.println("Enter the name of staff: ");
name=in.next();
}
public void putstaff()
{
i++;
System.out.println(i+" "+name);
}
}

Class ImportDept 🡪
===========================================================================
import myInstitute.department;
import java.util.Scanner;
class ImportDept
{
public static void main(String[] s)
{
Scanner in=new Scanner(System.in);
int n,i,j;

department d[]=new department[50];


System.out.println("Enter the no of staff: ");
n=in.nextInt();

for(i=0;i<n;i++)
{
d[i]=new department();
d[i].getstaff();
}

System.out.println("No. Name");

for(j=0;j<n;j++)
{
d[j].putstaff();
}

}
}
Output:
3. Develop a program which consists of the package named let_me_calculate with a class
named calculator and a method named add to add two integer numbers. Import
let_me_calculate package in another program to add two numbers

Class calculator in let_me_calculate Package 🡪


===========================================================================
package let_me_calculate;
public class calculator
{
public void add(int a,int b){
System.out.println("Addition = "+(a+b));
}
}

Class Calculate 🡪
===========================================================================
import let_me_calculate.calculator;
class calculate
{
public static void main(String[] s){
calculator c=new calculator();
c.add(5,5);
}
}

Output:

You might also like