Java 20 TH Exp

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

//Experiment No.

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;

for(int i = 1; i <= rows; ++i)


{
for(int j = 1; j <= i; ++j)
{
System.out.print("* ");
}
System.out.println();
}
}

public void pattern2()


{
int rows = 5;

for(int i = 1; i <= rows; ++i)


{
for(int j = 1; j <= i; ++j)
{
System.out.print(j + " ");
}
System.out.println();
}
}

public void pattern3()


{
int rows = 5;

for(int i = rows; i >= 1; --i)


{

Expt20 Page 1
for(int j = 1; j <= i; ++j)
{
System.out.print("* ");
}
System.out.println();
}
}

public void pattern4()


{
int rows = 5;

for(int i = rows; i >= 1; --i)


{
for(int j = 1; j <= i; ++j)
{
System.out.print(j + " ");
}
System.out.println();
}
}

public void pattern5()


{
int alphabet = 65;

for (int i = 0; i <= 5; i++)


{
for (int j = 0; j <= i; j++)
{
System.out.print((char) alphabet + " ");
}
alphabet++;
System.out.println();
}
}

Expt20 Page 2
Output:

E:\Pragati\Expt 20\Pattern>javac Pattern.java


//Using Package
E:\Pragati\Expt 20\Pattern>cd..
import Pattern.*; E:\Pragati\Expt 20>javac usePattern.java
class usePattern E:\Pragati\Expt 20>java usePattern
{ *
public static void main(String args[]) **
{ ***
****
Pattern p1=new Pattern();
*****
p1.pattern1(); A
p1.pattern5(); BB
} C CC
D DDD
} E EEEE
F FFFFF

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\let_me_calculate>javac calculator.java


E:\Pragati\Expt 20\let_me_calculate>cd..
E:\Pragati\Expt 20>javac Demo.java
E:\Pragati\Expt 20>java Demo
Enter two integer numbers
12
23
Sum is: 35

E:\Pragati\Expt 20>

Expt20 Page 4

You might also like