You are on page 1of 8

Assignment (OOPM-JAVA

)

Compiled by : Prof. Deepak Gaikar

ASSIGNMENT 3

(1)

Write notes on the following with the help of suitable program Segments in JAVA Packages.
Ans :
Packages are containers for classes that are used to keep the class namespace compartmentalized.
For example a package allows you to create a class named List which you can store in your own
package without concern that it will collide with some other class named List stored elsewhere.
Packages are stored in hierarchical manner.
Defining a package
To create a package use the package command as the first statement in your JAVA source file.
Any class defined in the source file will belong to that package.
The package statement defines a namespace in which classes are stored, if no package statement
is defined all the classes are stored in default package, which has no name.
The general form of package statement is:
package myPkg;
We can also create hierarchy of packages. To do so separate package name using the dot (.)
operator.
The general form of multilevel package statement is:
package pkg1[.pkg2[.pkg3[.pkg4];
For example, a package declared as
package java.awt.image;
Access protection in packages
Private
Same Class
YES
Same package
NO
Subclass
Same package
NO
Non Subclass
Different
NO
package
Subclass
Different
NO
package Non
Subclass

Default
YES
YES

Protected
YES
YES

Public
YES
YES

YES

YES

YES

NO

YES

YES

NO

NO

YES

Features of a Java package
It provides reusability feature.
It resolves the naming collision for the types it contains.

Importing an Entire Package import util. you can call a Thread object's interrupt() method to interrupt the corresponding thread if it is sleeping or waiting. Deepak Gaikar A package may have the following types. These built-in functionalities are coded in the form of interfaces and classes. Interfaces Classes Enumerated types Annotations Create your own package: package mypackage. These classes & interfaces are grouped into packages according (2) Explain any two methods of thread class.println(“Hello World”). we looked at how to construct a thread in Java. So on this page. For more details. via the Runnable and Thread objects.*. class HelloWorld{ public static void main(String [] arg){ System. see: Thread.sleep() (separate page). we take a high-level look at the most important methods on this class. We mentioned that the Threadclass provides control over threads.sleep() We actually saw a sneak preview of Thread. }} Importing a Package Member import util. A thread can be interruptedfrom its sleep. Thread.sleep() in our Java threading introduction. interrupt() As mentioned. effectively allowing us to implement a "pause".Vector. Ans: Thread methods in Java On the previous page.QUESTIONS BANK (CP-II) ( Using java) Compiled by : Prof. public void interrupt() Send an interrupt to a thread . Java API Packages Java provides some built-in functionality that you can use in your programs. The corresponding thread will "wake up" with an IOException at some point in the future. See thread interruption for more details. This static method asks the system to put the current thread to sleep for (approximately) the specified amount of time.out.

It tells the current thread to wait for the other thread to complete. (Some of the variants of the Threadconstructor actually allow you to pass in a name from the start. you can't preempt the mouse pointer thread by setting a thread to MAX_PRIORITY!).MAX_PRIORITY. . you can use a CountDownLatch. for example. WorkerThread-10 etc. setName() / getName() Threads have a name attached to them.MIN_PRIORITY. These are generally the range of values of "normal" user threads.Assignment (OOPM-JAVA) Compiled by : Prof. there is no safe method to stop a thread. where the behaviour on different platforms is compared. When calculating a priority value. Thread. and local thread priorities in the case of Windows. Java will attach a fairly dull name such as Thread-12. join() The join() method is called on the Thread object representing enother thread. But for debugging purposes you might want to attach a more meaningful name such as Animation Thread. in trying to use them for some purpose.NORM_PRIORITY and Thread. it's good practice to always do so in relation to the constants Thread.yield() (separate page). their behaviour depends on the platform and Java version: e. in Linux. values go from 1 to 10. you may actually interfere with more sensible scheduling decisions that the OS would have made anyway to achieve your purpose.g. see: Thread. To wait for multiple threads at a time. see the section on thread scheduling and the discussion on thread priorities.) Other thread functions There are occasionally other things that you may wish to do with a thread that don't correspond to a single method. and themapping of Java to OS priorities changed under Windows between Java 5 and Java 6. Thread. but you can always change it later.yield() This method effectively tells the system that the current thread is "willing to relinquish the CPU". By default. Deepak Gaikar setPriority() / getPriority() Sets and queries some platform-specific priority assignment of the given thread. and map on to some machine-specific range of values: nice values in the case of Linux. and instead you should simply let the corresponding run() method exit. Three main issues with thread priorities are that: they don't always do what you might intuitively think they do. What it actually does is quite system-dependent. priorities don't work at all in Hotspot before Java 6. In practice. In particular. and the OS will actually still run other threads beyond these values (so. For more details. For more information.

But for better understanding the threads. we are explaining it in the 5 states. non-runnable and terminated.QUESTIONS BANK (CP-II) ( Using java) Compiled by : Prof. According to sun. 3) Running The thread is in running state if the thread scheduler has selected it. runnable. 4) Non-Runnable (Blocked) This is the state when the thread is still alive. 2) Runnable The thread is in runnable state after invocation of start() method. There is no running state. but is currently not eligible to run. there is only 4 states in thread life cycle in java new. Deepak Gaikar (3) Describe the complete lifecycle of a thread? Ans : A thread can be in one of the five states. . 5) Terminated A thread is in terminated or dead state when its run() method exits. The life cycle of the thread in java is controlled by JVM. but the thread scheduler has not selected it to be the running thread. The java thread states are as follows: 1)New 2)Runnable 3)Running 4)Non-Runnable (Blocked) 5)Terminated 1) New The thread is in new state if you create an instance of Thread class but before the invocation of start() method.

println(" Hello World "). import java.*.out.Applet. outside the Applet run in the context of a web browser browser . } } . 1) 2) 3) 4) 5) Java Application Java applet java applications are stand-alone. so utilities Since applet runs inside the browser it enjoys like event handling user interface etc should be all the inbuilt facilities of event handling explicitly written by the programmer No control over the execution of the program There is some control over the execution of the program Example : Example: class Hello import java.applet.Assignment (OOPM-JAVA) Compiled by : Prof. WIDTH=400 HEIGHT=100> } } </applet>*/ Public class helloworld extends Applet { Public void paint(Graphics g) { g.class" System.10. Deepak Gaikar (4) Difference between java application and java applet? With example.10).awt. public static void main(String args[]) { /*<applet CODE="helloworld.being typically embedded within an html typically embedded within an html page Application can run by itself Applications cannot run by itself and requires browser software to run it Since it can be run by independently. { // Your program begins with a call to main( ).drawString(“hello world!”.

"). } public void stop() { addItem("stopping...append(newWord). explanation. g. } public void destroy() { addItem("preparing for unloading.out. ")..Graphics. 15). import java. buffer.java C:\appletviewer SimpleLife. Deepak Gaikar (5) Explain Life cycle of an applet(diagram. }} /* C:\javac SimpleLife. public void init() { buffer = new StringBuffer(). 5.such as when the user first visits the page to applets on.QUESTIONS BANK (CP-II) ( Using java) Compiled by : Prof. Program to demonstrate life cycle of an applet import java.. } public void start() { addItem("starting... addItem("initializing..toString().println(newWord).drawString(buffer. "). The simpleLife applet display a descriptive string whenever it encounters a major milestone in a life .java .class" WIDTH=400 HEIGHT=100> </applet>*/ public class SimpleLife extends Applet { StringBuffer buffer. /*<applet CODE="SimpleLife.Applet. repaint().applet. } private void addItem(String newWord) { System. program) Ans : APPLET LIFE CYCLE : The following source code for the “SimpleLife” applet.").. } public void paint(Graphics g) { //Draw the current string inside the rectangle.awt.

.starting…” above ..when the user returns to the page. */ The result is follows: You should see “Initialization…. the browser starts the applet.the applet initializes itself 3. before an applet is unloaded its given the chance to stop itself and then to perform a final cleanup.as the result of the applet being loaded .Assignment (OOPM-JAVA) Compiled by : Prof.. Deepak Gaikar initializing. stopping. we can say that applet goes through 4 states: 1. It can perform a final cleanup... An applet can react to major event in the following ways: 1.when an applet is loaded.. which consists of unloading the applet and then loading it again. It can stop running 4. It can start running 3.Destroyed ..the applet starts running When the user leaves the page-for example..It can Initialize itself 2. the applet is unloaded and then loaded again. so that the applet can release any resources it holds.Running 3.the applet has the chance to stop itself and do final cleanup before the browser exits.. in preparation for being unloaded Considering these four major events. preparing for unloading.Born 2.an instance of the applets controlling class (an applet subclass) is created 2.Idle 4. When the user quits the browser (or whatever application is displaying the applet). Some browser let the user reload applets. starting. heres what happens: 1. After that. to go another page –the browser stops the applet .

we may leave the web page containing the applet temporarily and again come back to the web page ..note that the start() method may be called more than once Idle state: An applet become idle when it is stopped from running . We can also do so by calling the stop() method explicitly. This occurs automatically by invoking destroy() method when we quit the browser. .an applet is stopped when we leave the web page containing the applet.load image or fonts 4.we can do the following : 1.QUESTIONS BANK (CP-II) ( Using java) Compiled by : Prof.setup colors Applet enter into this state only once in its life cycle because init() method is called only once in the beginning Running State : Applet enter into running state when the system calls the start() method of applet class this occurs automatically after the applet is initialized.this again starts the applet running . An applet gets destroyed only once its life cycle .set up initial values 3.create objects needed by the applet 2.in this state . Destroyed state : The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory.this is achieved by calling the init() method of applet class . Deepak Gaikar Illustrates how an applets travels through these states: init() Applet born Paint( ) start( ) Applet running Start( ) Stop( ) Applet idle Destroy( ) Applet destroyed Born state : Applet enters the „born‟ state when it is first loaded .