Packages - Java

Java provides a mechanisam for partioning the class namespace into more manageble chunks. This mechanisam is the package. The package is both a naming  and a visability control mechanisam,. You can define class inside a package . you can also define class members that are only exposed to other members of the same package. Ceating a package is quite easy. Simply include a package command as the first statement in a java source file.

Syntax: package packagename

Importing packages: java includes the import statement to bring certain classes, or entire packages, into visiblilty.

WRITE A PROGRAM ON PACKAGES.

package pac;
class Abc
{
public void get()
{
System.out.println("This is simple package");
}
}

class Sample
{
public static void main(String args[])
{
Abc a=new Abc();
a.get();
}
}

Public Classes: When the class is having public access specifier the class nane and java programe name should be the same.

 

WRITE A PROGRAM TO FIND THE SIMPLE INTEREST USING PACKAGES.

                                                    ( base.java)
package b;
public class Base
{
int p,n,r;
public void get()
{
p=10;
n=20;
r=30;
System.out.println("S.i"+(p*n*r)/100);
}                                     
}

 ( base1.java)

package b;
class Base1 extends Base
{
Base1()
{
System.out.println("This is Base");
get();
}
}

                                                             ( base2.java)
package b;
class Base2
{
public void put()
{
Base b=new Base();
b.get();
}
}

 

(base3.java)

package b;
class Sample
{
public static void main(String args[])
{
Base1  b1=new Base1();
Base2  b2=new Base2();
b2.put();
}
}

WRITE A PROGRAM TO FIND THE SIMPLE INTERSET WITH DIFFERENT PACKAGES.

 (pln.java)
package vision;
public class Pln
{
int p,n,r;
public void get()
{
p=10;
n=20;
r=30;
System.out.println((p*n*r)/100);
}
}

                                                            (prasad.java)

package a;
public class Prasad extends vision.Pln
{
Prasad()
{
get();
}
}

class Sample
{
public static void main(String args[])
{
Prasad b=new Prasad();
}
}

WRITE A PROGRAM TO FIND THE AREA OF TRIANGLE,AREA OF CIRCLE,SIMPLE INTEREST USING PACKAGES IMPORT.

           (areat.java)
package pac;
public  class Areat
{
int b,h;
public void get(int x,int y)
{
b=x;
h=y;
System.out.println("Arera of Triangle"+(b*h)/2);
}
}

                                                            (arear.java)
package pac;
public class Arear
{
int l,b;
public void get(int x,int y)
{
l=x;
b=y;
System.out.println("Area of triangle: "+(l*b))
}
}

(interest.java)
package pac;
public class Interest
{
int p,n,r;
public void get(int x,int y,int z)
{
p=x;
n=y;
r=z;
System.out.println("S.i: "+(p*n*r)/100);
}
}
(test.java)
import pac.*;
class Sample
{
public static void main(String args[])
{
Areat t=new Areat();
Arear r=new Arear();
Interest i=new Interest();
t.get(5,6);
r.get(10,10);
i.get(100,5,6);
}
}

 

For
More Explanation
&
Online Classes

Contact Us:
+919885348743