Sie sind auf Seite 1von 6

Topic 6 HTML

Assignment No. 2
Q1. What is package?
A package is a namespace that organizes a set of related classes and interfaces to provide access protection and namespace management. We can think of packages as being similar to different folders on your computer. Software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages. JDK has set of previously defined packages consisting of relevant classes. e.g IO package contains all the classes related to input output. Moreover, many times when we get a chance to work on a small project, one thing we intend to do is to put all java files into one single directory. It is quick, easy and harmless. However if our small project gets bigger, and the number of files is increasing, putting all these files into the same directory would be a nightmare for us. In java we can avoid this sort of problem by using Packages.

Benefits: We can easily determine that the classes and interfaces in a single package are related. The names of your classes and interfaces won't conflict with the names in other packages because the package creates a new namespace. You can allow classes within the package to have unrestricted access to one another yet still restrict access for types outside the package. To create a package, you choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package. If you do not use a package statement, your type ends up in an unnamed package (Use an unnamed package only for small or temporary applications). 1

Syntax: package <packageName>; e.g:- package MyPackage;

Q.2 How to access the content of package?


package MyPackage; class xx { . . } class yy { public static void main(String args[]) { . . } } then a directory called MyPackage should be created under the actual directory and the file should stay in that directory. When this particular file is saved it should be saved as yy.java within the directory MyPackage. So when it is compiled the .class files xx.class and yy.class are automatically going to stay in MyPackage directory. A Hierarchy of packages may be created. To do so, simply each package name should be separated from the one above it by use of a period. package pkg1[.pkg2[.pkg3]]]; But it should also stay in the corresponding directory. For eg. a package declared as package java.awt.image should stay in java\awt\image directory.

Now, again if a particular. java file is made to stay in a directory because it is written as a part of a corresponding package, then the file should be executed accordingly. For eg. the execution of yy.java in the earlier example should be done as java MyPackage.yy An Example:Code: // A simple package package MyPackage; 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 + : $ + bal); } } class AccountBalance { public static void main(String args[]) { Balance current[] = new Balance[3]; current[0] = new Balance(K. J. Fielding, 123.23); current[1] = new Balance(Will Tell, 157.02); 3 being in the actual directory i.e path just above the package.

current[0] = new Balance(Tom Jackson, -12.33); for (int i = 0; i < 3; i ++) current[i].show( ); } } This file should be called AccountBalance.java and should be placed in a directory named MyPackage. Then after compilation the AccountBalance.class will stay in the MyPackage directory. However, if this does not happen by default then all the .class files should be placed in the MyPackage directory. Then the file should be executed by the command java MyPackage.AccountBalance

Q3. How to use java system package


Java provides a large number of classes groped into different packages based on their functionality. Java System Packages / Java API Packages are collection of classes, interfaces and enumerated types. Different Java System Packages / Java API Packages are:

java.lang Package This java.lang package is a Java system package / Java API package that contains core classes related to language functionality and runtime system.

java.util Package The java.util package is a Java system package / Java API package that includes collection of data structure related classes. java.io Package This java.io package is a Java system package / Java API package that offers the input / output operations and other file operations. java.math Package This java.math package is a Java system package / Java API package that performs multi-precision arithmetic in your java program. java.nio Package The word nio in this java.nio package refers to new i/o. The java.nio package is a Java system package / Java API package that contains classes and interfaces related to new input / output framework.

java.net Package The java.net package is a Java system package / Java API package that is imported to perform network operations, socket programming, DNS lookup and much more. 4

java.security Package This java.security package is a Java system package / Java API package that performs security related activities like encryption, decryption and key generation.

java.sql Package This java.sql package is a Java system package / Java API package that is imported to establish connectivity between Java and SQL through Java Database Connectivity (JDBC).

java.awt Package This java.awt package is a Java system package / Java API package that is used to create user interfaces and perform graphics programming on it.

java.applet package This java.applet package is a Java system package / Java API package that is used to create applets.

Using System Packages The packages are organised in a hierarchical structure. For example, a package named java contains the package awt, which in turn contains various classes required for implementing GUI (graphical user interface). Accessing Classes from Packages There are two ways of accessing the classes stored in packages: Using fully qualified class name java.lang.Math.sqrt(x); Import package and use class name directly. import java.lang.Math Math.sqrt(x); Selected or all classes in packages can be imported: import package.class; import package.*; Implicit in all programs: import java.lang.* ; package statement(s) must appear first

Q4. Explain life cycle of Applet Applet runs in the browser and its lifecycle method are called by JVM when it is loaded and destroyed. Here are the lifecycle methods of an Applet: init(): This method is called to initialized an applet start(): This method is called after the initialization of the applet. stop(): This method can be called multiple times in the life cycle of an Applet. destroy(): This method is called only once in the life cycle of the applet when applet is destroyed. init () method: The life cycle of an applet is begin on that time when the applet is first loaded into the browser and called the init() method. The init() method is called only one time in the life cycle on an applet. The init() method is basically called to read the PARAM tag in the html file. The init () method retrieve the passed parameter through the PARAM tag of html file using get Parameter() method All the initialization such as initialization of variables and the objects like image, sound file are loaded in the init () method .After the initialization of the init() method user can interact contains the init() method. start () method: The start method of an applet is called after the initialization method init(). This method may be called multiples time when the Applet needs to be started or restarted. For Example if the user wants to return to the Applet, in this situation the start Method() of an Applet will be called by the web browser and the user will be back on the applet. In the start method user can interact within the applet. stop () method: The stop() method can be called multiple times in the life cycle of applet like the start () method. Or should be called at least one time. There is only miner difference between the start() method and stop () method. For example the stop() method is called by the web browser on that time When the user leaves one applet to go another applet and the start() method is called on that time when the user wants to go back into the first program or Applet. destroy() method: The destroy() method is called only one time in the life cycle of Applet like init() method. This method is called only on that time when the browser needs to Shut down. with the Applet and mostly applet

Das könnte Ihnen auch gefallen