A PACKAGE in Java is a collection of classes, sub-packages(package inside a package), and interfaces. It helps to organize classes into a folder structure and make it easy to locate and use them. More importantly, it helps to improve code reusability.
In java we have several built-in packages, for example when we need user input, we import a package as follows:
import java.util.Scanner; Here:
How to Create a package?
- java is a top level package
- util is a sub package of java
- Scanner is a class which is present in the sub package util.
Reusability: While developing a project in java, we often feel that there are few things that we are writing again and again in our code. Using packages, you can create such things in form of classes inside a package and whenever you need to perform that same task, just import that package and use the class.
Better Organization: Again, in large java projects where we have several hundreds of classes, it is always required to group the similar types of classes in a meaningful package name so that you can organize your project better and when you need something you can quickly locate it and use it, which improves the efficiency.
Name Conflicts: We can define two classes with the same name in different packages so to avoid name collision, we can use packages
Types of packages in Java
1)User defined package: The package we create is called user-defined package.
2)Built-in package: The packages that are already defined,like java.io.*, java.lang.* etc are known as built-in packages.
Syntax to create a package:
package <nameOfPackage>;
Command:package calculate;
How to Create a package?
- Choose the name of the package
- Include the package command as the first line of code in your Java Source File.
- The Source file contains the classes, interfaces, etc you want to include in the package
- Compile to create the Java packages.
Step 1) Type the following package program in notepad.
package p1;
class Test
{
public void m1()
{
System.out.println("It is method m1 of the class Test");
}
public static void main(String args[])
{
Test t = new Test();
t.m1();
}
}
Step 2) In next step, save this file as Test.java
Step 3) In this step, we compile the file.
I:\>javac Test.java
Note: Here Test.class will be created but no package is created.
Note:
We can create a package in two ways
Method 1 : Manually creating a directory for package.
Method 2 : Creating a directory for package using a command.
Method 1 : Manually creating a directory for package.
Method 2 : Creating a directory for package using a command.
Method1
Step 4) To create a package using Method2, use the following command during compilation. javac –d . Test.java
Step 3: use the following command to run the package programubuntu@ubuntu:~/Desktop$ mkdir pk
ubuntu@ubuntu:~/Desktop$ cd pk
ubuntu@ubuntu:~/Desktop/pk$ gedit Rk.java
ubuntu@ubuntu:~/Desktop/pk$ javac -d . Rk.java
ubuntu@ubuntu:~/Desktop/pk$ ls
pk Rk.class Rk.java
ubuntu@ubuntu:~/Desktop/pk$ cd ..
ubuntu@ubuntu:~/Desktop$ gedit Kk.java
ubuntu@ubuntu:~/Desktop$ javac -d . Kk.java
ubuntu@ubuntu:~/Desktop$ ls K*
Kk.class Kk.java
ubuntu@ubuntu:~/Desktop$ java Kk 4 2
Addition is:6
Division is:2
Multiplication is:8
Subtraction is:2
(OR)
Manually creating a directory for package.
Step 4) create a folder named by package p1
C:\Users\Student\Desktop\java prg\p1>dir
Volume in drive C has no label.
Volume Serial Number is B460-E456
Directory of C:\Users\Student\Desktop\java prg\p1>
24-05-2022 13:15 <DIR> .
24-05-2022 13:15 <DIR> ..
24-05-2022 13:16 517 Test.class
24-05-2022 12:28 202 Test.java
2 File(s) 719 bytes
2 Dir(s) 246,153,756,672 bytes free
Step 5) Compile the same file using the following code
C:\Users\Student\Desktop\java prg\p1>javac -d . Test.java
Here –d creates a directory and the "." operator represents the current working directory.
Step 6)Compile the same file using the following code
C:\Users\Student\Desktop\java prg\p1>javac -d .. Test.java
Here “..” indicates the parent directory. In our case file will be saved in parent directory which is C Drive
Step 7) To execute the code mention the fully qualified name of the class i.e. the package name followed by the sub-package name followed by the class name
C:\Users\Student\Desktop\java prg\p1>java p1.Test
It is method m1 of the class Test
Example:
Procedure to create a package
//save the below program as Test.java
//save the below program as Test.java
package tstpkg;
public class Test
{
public static void main(String args[])
{
System.out.println("Testing a Package functionality");
}
}
Compile and create a package “tstpkg” in current directory as follows
javac -d . Test.java
Run/execute Java package program
java tstpkg.Test
java tstpkg.Test
Output:
Testing a Package functionality
Testing a Package functionality
Example:
package com.rkblog;
public class Packrk
{
public static void main(String[] args)
{
System.out.println(" It will dispaly package name");
}
}
public class Packrk
{
public static void main(String[] args)
{
System.out.println(" It will dispaly package name");
}
}
Output:
ubuntu@ubuntu:~/Desktop/pk$ ls
Packrk.java
ubuntu@ubuntu:~/Desktop/pk$ javac -d . Packrk.java
ubuntu@ubuntu:~/Desktop/pk$ ls
com Packrk.java
ubuntu@ubuntu:~/Desktop/pk$ cd com/
ubuntu@ubuntu:~/Desktop/pk/com$ ls
rkblog
ubuntu@ubuntu:~/Desktop/pk/com$ cd rkblog/
ubuntu@ubuntu:~/Desktop/pk/com/rkblog$ ls
Packrk.class
ubuntu@ubuntu:~/Desktop/pk/com/rkblog$ pwd
/home/ubuntu/Desktop/pk/com/rkblog
ubuntu@ubuntu:~/Desktop/pk/com/rkblog$ cd ..
ubuntu@ubuntu:~/Desktop/pk/com$ cd ..
ubuntu@ubuntu:~/Desktop/pk$ ls
com Packrk.java
ubuntu@ubuntu:~/Desktop/pk$ javac -d . Packrk.java
ubuntu@ubuntu:~/Desktop/pk$ java com.rkblog.Packrk
It will dispaly package name
Packrk.java
ubuntu@ubuntu:~/Desktop/pk$ javac -d . Packrk.java
ubuntu@ubuntu:~/Desktop/pk$ ls
com Packrk.java
ubuntu@ubuntu:~/Desktop/pk$ cd com/
ubuntu@ubuntu:~/Desktop/pk/com$ ls
rkblog
ubuntu@ubuntu:~/Desktop/pk/com$ cd rkblog/
ubuntu@ubuntu:~/Desktop/pk/com/rkblog$ ls
Packrk.class
ubuntu@ubuntu:~/Desktop/pk/com/rkblog$ pwd
/home/ubuntu/Desktop/pk/com/rkblog
ubuntu@ubuntu:~/Desktop/pk/com/rkblog$ cd ..
ubuntu@ubuntu:~/Desktop/pk/com$ cd ..
ubuntu@ubuntu:~/Desktop/pk$ ls
com Packrk.java
ubuntu@ubuntu:~/Desktop/pk$ javac -d . Packrk.java
ubuntu@ubuntu:~/Desktop/pk$ java com.rkblog.Packrk
It will dispaly package name
Method 2 : Creating a directory for package using a command.
Note: Here –d creates a directory and the "." operator represents the current working directory.
Step 5) When you execute the above command, it creates a package(directory) p1 in the current working directory, that is the location where we use above command. P1 directory contains the file named Test.class.
Example 1:
Filename : Rk, Kk
Step 1: create a directory pk and then create a file Rk.java as shown
ubuntu@ubuntu:~/Desktop/rkjava/$ mkdir pk
ubuntu@ubuntu:~/Desktop/rkjava/$ cd pk
ubuntu@ubuntu:~/Desktop/rkjava/pk$gedit Rk.java
ubuntu@ubuntu:~/Desktop/rkjava/pk$ ls
lsRk.class Rk.java
//Program :file name: Rk.java
package pk;
public class Rk
{
public int add(int a,int b)
{
return a+b;
}
public int sub(int a,int b)
{
return a-b;
}
public int mul(int a,int b)
{
return a*b;
}
public int div(int a,int b)
{
return a/b;
}
}
Step 2: create a file with Kk.java
public class Rk
{
public int add(int a,int b)
{
return a+b;
}
public int sub(int a,int b)
{
return a-b;
}
public int mul(int a,int b)
{
return a*b;
}
public int div(int a,int b)
{
return a/b;
}
}
Step 2: create a file with Kk.java
import Pk.Rk; // import <Package name>.<Class name>
class Kk
{
public static void main(String []args)
{
Rk c=new Rk();
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int z=c.mul(a,b);
int v=c.sub(a,b);
System.out.println("Addition is:"+c.add(a,b));
System.out.println("Division is:"+c.div(a,b));
System.out.println("Multiplication is:"+z);
System.out.println("Subtraction is:"+v);
}
}
class Kk
{
public static void main(String []args)
{
Rk c=new Rk();
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int z=c.mul(a,b);
int v=c.sub(a,b);
System.out.println("Addition is:"+c.add(a,b));
System.out.println("Division is:"+c.div(a,b));
System.out.println("Multiplication is:"+z);
System.out.println("Subtraction is:"+v);
}
}
Step 3: use the following command to run the package program
ubuntu@ubuntu:~/Desktop$ cd pk
ubuntu@ubuntu:~/Desktop/pk$ gedit Rk.java
ubuntu@ubuntu:~/Desktop/pk$ javac -d . Rk.java
ubuntu@ubuntu:~/Desktop/pk$ ls
pk Rk.class Rk.java
ubuntu@ubuntu:~/Desktop/pk$ cd ..
ubuntu@ubuntu:~/Desktop$ gedit Kk.java
ubuntu@ubuntu:~/Desktop$ javac -d . Kk.java
ubuntu@ubuntu:~/Desktop$ ls K*
Kk.class Kk.java
ubuntu@ubuntu:~/Desktop$ java Kk 4 2
Addition is:6
Division is:2
Multiplication is:8
Subtraction is:2
(OR)
Command to compile and to create a package Rk
ubuntu@ubuntu:~/Desktop/rkjava$ javac -d . Rk.java
Command to compile
ubuntu@ubuntu:~/Desktop/rkjava$ javac Kk.java
Command to execute
ubuntu@ubuntu:~/Desktop/rkjava$ java Kk 44 22
ubuntu@ubuntu:~/Desktop/rkjava$ javac -d . Rk.java
ubuntu@ubuntu:~/Desktop/rkjava$ javac Kk.javaubuntu@ubuntu:~/Desktop$ java Kk 4 2
Addition is:6
Division is:2
Multiplication is:8
Subtraction is:2
ubuntu@ubuntu:~/Desktop/rkjava$ javac Kk.javaubuntu@ubuntu:~/Desktop$ java Kk 4 2
Addition is:6
Division is:2
Multiplication is:8
Subtraction is:2
Example 2:
Filename : Mensuration.java
package Calc;
public class Mensuration
{
public double volume(double l,double b,double h)
{
return l*b*h;
}
public double tsa(double l,double b,double h)
{
return 2*(l*b+b*h+l*h);
}
public double lsa(double l,double b,double h)
{
return 2*h*(l+b);
}
public double diag(double l,double b,double h)
{
return Math.pow(l*l+b*b+h*h,0.5);
}
public double peri(double l,double b,double h)
{
return 4*(l+b+h);
}
}
import Calc.Mensuration;
import java.util.Scanner;
class UseCalc
{
public static void main(String x[])
{
double l,b,h;
Scanner s=new Scanner(System.in);
Mensuration m=new Mensuration();
System.out.println("Enter Length, Breadth & Height:");
l=s.nextDouble();
b=s.nextDouble();
h=s.nextDouble();
System.out.println("Volume Of Cuboid: "+m.volume(l,b,h));
System.out.println("Total Surface Area :"+m.tsa(l,b,h));
System.out.println("Lateral Surface Area: "+m.lsa(l,b,h));
System.out.println("Length of diagonal of the cuboid: "+Math.round(m.diag(l,b,h)));
System.out.println("Perimeter of Cuboid: "+m.peri(l,b,h));
}
}
Command to compile and to create a package Calc
I:\>javac -d . Mensuration.java
Command to compile
I:\>javac UseCalc.java
Command to execute
I:\>java UseCalc
Output:
Enter Length, Breadth & Height:
Enter Length, Breadth & Height:
12
13
14
Volume Of Cuboid: 2184.0
13
14
Volume Of Cuboid: 2184.0
Total Surface Area :1012.0
Lateral Surface Area: 700.0
Length of diagonal of the cuboid: 23
Length of diagonal of the cuboid: 23
Perimeter of Cuboid: 156.0
No comments:
Post a Comment