Saturday, 8 June 2013

PACKAGES --INBUILT OR USER DEFINED

it was very good we were taught to create user defined packages it was interesting
PACKAGES ------->
Packages r java's way of grouping a variety of classes or interfaces together
in this we can create a package and use if again and again or u can say code re useability
like u use a import java.io. package (inbilt) we will use our own packages (user defined packages)
some points ----)two different packages can have classes of same name
------)they provide a way to provide classes for excidential   (chori) use
TYPES OF PACKAGES
as i told u above we have two types of packages inbuilt and user defined


inbuilt ---- java.io.............java.util...........java.awt..............java.lang........java.applet etc
user defined packages r -----like u created a package containing factorial class and then call it while writing a prog for factorial
these packages r stored in a new folder



and then save the classes created in this package

and then make your program using mypack



import mypack.*;
import java.io.*;
import java.util.*;
class Testmypack
{
public static void main(String arg[])throws IOException
{
DataInputStream dis= new DataInputStream(System.in);
System.out.println("enter the no whose factorial to be calculated");
int n=Integer.parseInt(dis.readLine());
Factorial obj=new Factorial();
int res=obj.fact(n);
System.out.println("factorial of"+n+" is"+res);
}}






PROG FOR CREATING PACKAGE IS

package mypack;
 public class Factorial
{
public int fact(int n)
{
int f=1;
for(int i=1;i<=n;i++)
{
f=f*i;
}
return(f);
}}


TRY THIS PROG U WILL SURELY GET HOW TO CREATE YOUR OWN PACKAGES



ONE MORE EXAMPLE

 package mypack;
 public class Reverse
{
public int rev(int n)
{
int r=0;
while(n>0)
{
r=(r*10)+(n%10);
n=n/10;
}
return(r);
}}


import mypack.*;
import java.io.*;
class Testreverse
{
public static void main(String arg[])throws IOException
{
DataInputStream dis= new DataInputStream(System.in);
System.out.println("enter the no whose reverse to be calculated");
int m=Integer.parseInt(dis.readLine());
Reverse obj=new Reverse();
int res=obj.rev(m);
System.out.println("reverse of"+m+" is"+res);
}}






No comments:

Post a Comment