Java 20 TH Exp
Java 20 TH Exp
Java 20 TH Exp
20
• Program to implement user defined packages in terms of creating a new package and
importing the same.
//Creating Package
package Pattern;
public class Pattern
{
public void pattern1()
{
int rows = 5;
Expt20 Page 1
for(int j = 1; j <= i; ++j)
{
System.out.print("* ");
}
System.out.println();
}
}
Expt20 Page 2
Output:
E:\Pragati\Expt 20>
• Define a package names myInstitute include class named as department with one method
to display the staff of that department. Develop a program to import this package in java
application and call the method defined in the package.
//Creating Package
packagemyInstitute; Output:
public class department
{ E:\Pragati\Expt 20>cd myInstitute
public void display_staff() E:\Pragati\Expt 20\myInstitute>javac department.java
{ E:\Pragati\Expt 20\myInstitute>cd..
System.out.println("SVS");
E:\Pragati\Expt 20>javac usemyInst.java
System.out.println("PPC");
System.out.println("RNP"); E:\Pragati\Expt 20>java usemyInst
System.out.println("SRK"); SVS
System.out.println("APD"); PPC
System.out.println("RMP"); RNP
} SRK
} APD
RMP
//Using Package
import myInstitute.*; E:\Pragati\Expt 20>
class usemyInst
{
public static void main(String args[])
{
department d1=new department();
d1.display_staff();
}
}
Expt20 Page 3
• Define a package names let_me_calculate include class named as calculatorand a method
named add to add two integer numbers.Import let_me_calculate package in another
program named Demo to add two numbers.
//Creating Package
packagelet_me_calculate;
importjava.util.*;
public class calculator
{
public void add(int a, int b)
{
int c=a+b;
System.out.println("Sum is: "+c);
}
}
//Using Package
importjava.util.*;
importlet_me_calculate.*;
public class Demo
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter two integer numbers");
int a=sc.nextInt();
int b=sc.nextInt();
calculator c1=new calculator();
c1.add(a,b);
}
}
Output:
E:\Pragati\Expt 20>
Expt20 Page 4