User-defined packages

User-defined packages

The user of the java langauge can also create their own packages. They are called user-defined package.User-defined packages can also be imported into other classes and used exact in the same way as the built in packages.
let us see how to create a package

package packagename;

Program

//save as Maths.java

package Maths;

public class MathsOperations
{
public static void maximum(int n1,int n2,int n3)
{
int max1=0;
if(n1>n2 && n1>n3)
max1=n1;
if(n2>n1 && n2>n3)
max1=n2;
if(n3>n1 && n3>n2)
max1=n3;
System.out.println(“The maximum of 3 numbers is ” + max1);
}
public static void minimum(int n1,int n2,int n3)
{
int min1=n1;
if(n1<n2 && n1<n3)
min1=n1;
if(n2<n1 && n2<n3)
min1=n2;
if(n3<n1 && n3<n2)
min1=n3;
System.out.println(“The minimum of 3 numbers is ” + min1);
}
}


 

//save as test.java

import Maths.MathsOperations;
class test
{
public static void main(String []arg)
{
if(arg.length!=3)
{
System.out.println(“Invalid number of arguments”);
System.exit(0);
}
int a=Integer.parseInt(arg[0]);
int b=Integer.parseInt(arg[1]);
int c=Integer.parseInt(arg[2]);
MathsOperations.maximum(a,b,c);
MathsOperations.minimum(a,b,c);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *