0% found this document useful (0 votes)
76 views

JAVA Experiment No 4 - Packages in Java

The document discusses Java packages and importing classes. It includes 8 programs that demonstrate: 1) Using a built-in package and class, 2) Creating a package with multiple classes, 3) Importing specific classes vs full packages, 4) Static imports, 5) Including a class in a package, 6) Using fully qualified names instead of imports. The programs output the results of using packages, classes, and imports in Java.

Uploaded by

Akanksha Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

JAVA Experiment No 4 - Packages in Java

The document discusses Java packages and importing classes. It includes 8 programs that demonstrate: 1) Using a built-in package and class, 2) Creating a package with multiple classes, 3) Importing specific classes vs full packages, 4) Static imports, 5) Including a class in a package, 6) Using fully qualified names instead of imports. The programs output the results of using packages, classes, and imports in Java.

Uploaded by

Akanksha Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment no 4

Packages in Java

Program 1: Program using a built-in package.


Theoretical Description:
package Edureka;
import java.util.ArrayList;
 
class BuiltInPackage {
 
    public static void main(String[] args) {
 
        ArrayList<Integer> myList = new ArrayList<>(3);
 
        myList.add(3);
        myList.add(2);
        myList.add(1);
 
        System.out.println("The elements of list are: " + myList);
    }
}

Output:

The elements of list are: [3, 2, 1

Program 2: Program to create a package.


Theoretical Description:

package pack;
class balance
{
String name;
double bal;

balance(String s,double b)
{
name=s;
bal=b;
}

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

class packpro
{
public static void main(String args[])
{
balance current[]=new balance[3];
current[0]=new balance("Someone",2300);
current[1]=new balance("Anyone",345.78);
current[2]=new balance("ABC",-321.89);
for(int i=0;i<3;i++)
current[i].show();
}
}

OUTPUT:

D:\JDK1.3\BIN\PACK>javac packpro.java
D:\JDK1.3\BIN\PACK>java pack.packpro
Someone:$2300.0
Anyone:$345.78
-->
ABC:$-321.89

Program 3: Java program to demonstrate accessing of members when


corresponding classes are imported and not imported.
Theoretical Description:

import java.util.Vector;
   
public class ImportDemo
{
   public ImportDemo()
   {
      // java.util.Vector is imported, hence we are
      // able to access directly in our code.
      Vector newVector = new Vector();
   
      // java.util.ArrayList is not imported, hence 
      // we were referring to it using the complete 
      // package.
      java.util.ArrayList newList = new java.util.ArrayList();
   }
   
   public static void main(String arg[])
   {
      new ImportDemo();
   }
}

Program 4: WAP to import a specific class of the package.

Theoretical Description:

//save by First.java
package learnjava;

public class Hello{


public void msg() {
System.out.println("Hello");
}
}

public class First{


public void msg() {
System.out.println("Welcome");
}
}

//save by Second.java
package Java;
import learnjava.First;
//import learnjava.Welcome;

public class Second {


public static void main(String args[]) {
First obj = new First();
obj.msg();
}
}

OUTPUT:
Welcome
Program 5: WAP to import all classes of the package.

Theoretical Description:

//save by First.java
package learnjava;

public class Hello{


public void msg() {
System.out.println("Hello");
}
}

public class First{


public void msg() {
System.out.println("Welcome");
}
}

//save by Second.java
package Java;
import learnjava.*;

public class Second {


public static void main(String args[]) {
Hello obj = new Hello();
obj.msg();
First obj1 = new First();
Obj1.msg();
}
}

OUTPUT:
Hello
Welcome
Program 6: WAP to import static package in Java.
Theoretical Description:

package MyPackage;
import static java.lang.Math.*; //static import
import static java.lang.System.*; // static import

public class StaticImportDemo {


       public static void main(String args[]) {
          double val = 64.0;
          double sqroot = sqrt(val); // Access sqrt() method directly
          out.println("Sq. root of " + val + " is " + sqroot);
          //We don't need to use 'System.out
       }
    }

OUTPUT:
4096.0
Program 7: Write a program to include a Class in Java Package.
.
Theoretical Description:

package MyPackage;
 
public class Compare {
  int num1, num2;
 
  Compare(int n, int m) {
     num1 = n;
     num2 = m;
}
public void getmax(){
    if ( num1 > num2 ) {
        System.out.println("Maximum value of two numbers is " + num1);
  }
    else {
        System.out.println("Maximum value of two numbers is " + num2);
    }
}
 
 
public static void main(String args[]) {
        Compare current[] = new Compare[3];
            
        current[1] = new Compare(5, 10);
        current[2] = new Compare(123, 120);
          
          for(int i=1; i < 3 ; i++)
              {
              current[i].getmax();
              }
       }
}

Output:

1. Maximum value of two numbers is 10


2. Maximum value of two numbers is 123
Program 8: Write a program to use fully qualified name while
importing a class.

Theoretical Description:

package Edureka;
public class Demo{
    public static void main(String args[]) {
        int n=10, m=11;
        //Using fully qualified name instead of import
        MyPackage.Compare current = new MyPackage.Compare(n, m);
        if(n != m) {
             current.getmax();
        }
        else {
            System.out.println("Both the values are same");
        }
}
}

Output:

1. Maximum value of two numbers is 11

You might also like