Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
Assignment (OOPM-JAVA)
setPriority() / getPriority()
Sets and queries some platform-specific priority assignment of the given thread. When
calculating a priority value, it's good practice to always do so in relation to the
constants Thread.MIN_PRIORITY, Thread.NORM_PRIORITY and Thread.MAX_PRIORITY.
In practice, values go from 1 to 10, and map on to some machine-specific range of
values: nice values in the case of Linux, and local thread priorities in the case of Windows. These
are generally the range of values of "normal" user threads, and the OS will actually still run other
threads beyond these values (so, for example, you can't preempt the mouse pointer thread by
setting a thread to MAX_PRIORITY!).
Three main issues with thread priorities are that:
they don't always do what you might intuitively think they do;
their behaviour depends on the platform and Java version: e.g. in Linux, priorities don't
work at all in Hotspot before Java 6, and themapping of Java to OS priorities changed
under Windows between Java 5 and Java 6;
in trying to use them for some purpose, you may actually interfere with more sensible
scheduling decisions that the OS would have made anyway to achieve your purpose.
For more information, see the section on thread scheduling and the discussion on thread
priorities, where the behaviour on different platforms is compared.
join()
The join() method is called on the Thread object representing enother thread. It tells
the current thread to wait for the other thread to complete. To wait for multiple threads at a
time, you can use a CountDownLatch.
Thread.yield()
This method effectively tells the system that the current thread is "willing to relinquish the CPU".
What it actually does is quite system-dependent. For more details, see: Thread.yield() (separate
page).
setName() / getName()
Threads have a name attached to them. By default, Java will attach a fairly dull name such
as Thread-12. But for debugging purposes you might want to attach a more meaningful name
such as Animation Thread, WorkerThread-10 etc. (Some of the variants of the Threadconstructor
actually allow you to pass in a name from the start, but you can always change it later.)
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. In particular, there is no safe method to stop a thread, and instead you should
simply let the corresponding run() method exit.
1) New
The thread is in new state if you create an instance of Thread class but before the invocation of
start() method.
2) Runnable
The thread is in runnable state after invocation of start() method, but the thread scheduler has not
selected it to be the running thread.
3) Running
The thread is in running state if the thread scheduler has selected it.
4) Non-Runnable (Blocked)
This is the state when the thread is still alive, but is currently not eligible to run.
5) Terminated
A thread is in terminated or dead state when its run() method exits.
Assignment (OOPM-JAVA)
(4) Difference between java application and java applet? With example.
1)
2)
3)
4)
5)
Java Application
Java applet
java applications are stand-alone, outside the Applet run in the context of a web browser
browser
,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, 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.Applet;
{
// Your program begins with a call to main( ).
import java.awt.*;
public static void main(String args[])
{
/*<applet CODE="helloworld.class"
System.out.println(" Hello World ");
WIDTH=400 HEIGHT=100>
}
}
</applet>*/
Public class helloworld extends Applet
{
Public void paint(Graphics g)
{
g.drawString(hello world!,10,10);
}
}
Assignment (OOPM-JAVA)
initializing...
starting...
stopping...
preparing for unloading...
*/
The result is follows:
You should see Initialization..starting above ,as the result of the applet being loaded .when
an applet is loaded, heres what happens:
1.an instance of the applets controlling class (an applet subclass) is created
2.the applet initializes itself
3.the applet starts running
When the user leaves the page-for example, to go another page the browser stops the
applet .when the user returns to the page, the browser starts the applet.
Some browser let the user reload applets, which consists of unloading the applet and then
loading it again. before an applet is unloaded its given the chance to stop itself and then to
perform a final cleanup, so that the applet can release any resources it holds. After that, the applet
is unloaded and then loaded again.
When the user quits the browser (or whatever application is displaying the applet),the applet has
the chance to stop itself and do final cleanup before the browser exits.
An applet can react to major event in the following ways:
1.It can Initialize itself
2. It can start running
3. It can stop running
4. It can perform a final cleanup, in preparation for being unloaded
Considering these four major events, we can say that applet goes through 4 states:
1.Born
2.Running
3.Idle
4.Destroyed
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 .this is achieved by calling the init() method
of applet class .in this state ,we can do the following :
1.create objects needed by the applet
2.set up initial values
3.load image or fonts
4.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..we may leave the web page containing the
applet temporarily and again come back to the web page .this again starts the applet running
.note that the start() method may be called more than once
Idle state:
An applet become idle when it is stopped from running .an applet is stopped when we leave the
web page containing the applet. We can also do so by calling the stop() method explicitly.
Destroyed state :
The destroy( ) method is called when the environment determines that your applet
needs to be removed completely from memory. This occurs automatically by invoking destroy()
method when we quit the browser. An applet gets destroyed only once its life cycle .