Java package program Examples

Content 

 

1. Program to explain Creating Package.

package  Pack1;

class First
{
public void view( )
{
System.out.println( "Easy Programming24" );
}
}

public class  ImportPack2
{
public static void main( String args[ ] )
{
First f = new First();
f.view();
}
}

Output:

Easy Programming24





2. Program to explain Importing Package.

package  Pack1;

public class  First
{
public void view( )
{
System.out.println( "Easy Programming24" );
}
}

// Second File as (ImportPack1.java in current folder) :

import Pack1.*;

public class  ImportPack1
{
public static void main( String args[ ] )
{
First f = new First();
f.view();
}
}

Output:

Easy Programming24





3. Program to explain Creating Package.

package MyPack;

class  Balance
{
String name;
double bal;

Balance(String n, double b)
{
name = n;
bal = b;
}

void show()
{
if(bal<0)
System.out.print("=> ");
System.out.println(name + ": Rs. " + bal);
}
}

class  AccountBalance
{
public static void main(String args[ ])
{
Balance current[ ] = new Balance[3];

current[0] = new Balance("aliver", 123.45);
current[1] = new Balance("arsu", 345.12);
current[2] = new Balance("chetan", -12.34);

for(int i=0; i<3; i++)
{
current[ i ].show();
}
}
}

Output:

aliver:  Rs.  123.45
arsu:  Rs.  345.12
=> chetan:  Rs.  -12.34





4. Program to explain Importing Package

package MyPack;

public class  Balance
{
String name;
double bal;

public Balance(String n, double b)
{
name = n;
bal = b;
}

public void show()
{
if(bal<0)
System.out.print("=> ");
System.out.println(name + ": Rs. " + bal);
}
}

// Second File as( BalanceImport.java in current folder) :

import  MyPack.*;

class  BalanceImport
{
public static void main(String args[ ])
{
Balance ob = new Balance("aliver", 234.56);
ob.show();
}
}

Output:

aliver:  Rs. 234.56





5. Using class with import.

import java.util.Scanner;

class ImportTest2
{
public static void main( String args[ ] )
{
double r, a;
Scanner sc = new Scanner( System.in );

System.out.print("Enter Radius : ");
r = sc.nextDouble();

a = 3.14 * r * r;

System.out.println("Area of Circle : " + a);
}
}

Output:

Enter Radius : 10
Area of Circle : 314.0




6. Using class without import.

class  ImportTest
{
public static void main( String args[ ] )
{
double  r, a;
java.util.Scanner sc = new java.util.Scanner( System.in );

System.out.print("Enter Radius : ");
r = sc.nextDouble();

a = 3.14 * r * r;

System.out.println("Area of Circle : " + a);
}
}

Output:

Enter Radius : 10
Area of Circle : 314.0




7. import with * sign.

import  java.util.*;

class  ImportTest2
{
public static void main( String args[ ] )
{
double r, a;
Scanner sc = new Scanner( System.in );

System.out.print("Enter Radius : ");
r = sc.nextDouble();

a = 3.14 * r * r;

System.out.println("Area of Circle : " + a);
}
}

Output:

Enter Radius : 10
Area of Circle : 314.0




8. Program to explain static import statement.

import static java.lang.Math.*;
//   import static java.lang.Math.sqrt;

class StaticImport
{
public static void main( String args[ ] )
{
double a = 3.0, b = 4.0;
double c = sqrt( a*a + b*b );
System.out.println(" C = " + c );
}
}

Output:

C  =  5.0





9. program to explain static import for static Field.

import java.util.Scanner;
import static java.lang.Math.PI;

class StaticImport2
{
public static void main( String args[ ] )
{
double r, a;
Scanner sc = new Scanner( System.in );

System.out.print("Enter Radius : ");
r = sc.nextDouble();

a = PI * r * r;

System.out.println("Area of Circle : " + a);
}
}

Output:

Enter Radius : 10
Area of Circle : 314.1592653589793


Reactions

Post a Comment

0 Comments