Sei sulla pagina 1di 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.

QUESTIONS BANK (CP-II) ( Using java)

Compiled by : Prof. Deepak Gaikar

A package may have the following types.


Interfaces
Classes
Enumerated types
Annotations
Create your own package:
package mypackage;
class HelloWorld{
public static void main(String [] arg){
System.out.println(Hello World);
}}
Importing a Package Member
import util.Vector;
Importing an Entire Package
import util.*;
Java API Packages
Java provides some built-in functionality that you can use in your programs. These built-in
functionalities are coded in the form of interfaces and classes. These classes & interfaces are
grouped into packages according

(2) Explain any two methods of thread class.


Ans:
Thread methods in Java
On the previous page, we looked at how to construct a thread in Java, via
the Runnable and Thread objects. We mentioned that the Threadclass provides control over
threads. So on this page, we take a high-level look at the most important methods on this class.
Thread.sleep()
We actually saw a sneak preview of Thread.sleep() in our Java threading introduction. This static
method asks the system to put the current thread to sleep for (approximately) the specified
amount of time, effectively allowing us to implement a "pause". A thread can be interruptedfrom
its sleep.
For more details, see: Thread.sleep() (separate page).
interrupt()
As mentioned, you can call a Thread object's interrupt() method to interrupt the corresponding
thread if it is sleeping or waiting. The corresponding thread will "wake up" with
an IOException at some point in the future. See thread interruption for more details.
public void interrupt()
Send an interrupt to a thread

Assignment (OOPM-JAVA)

Compiled by : Prof. Deepak Gaikar

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.

QUESTIONS BANK (CP-II) ( Using java)

Compiled by : Prof. Deepak Gaikar

(3) Describe the complete lifecycle of a thread?


Ans :
A thread can be in one of the five states. According to sun, there is only 4 states in thread
life cycle in java new, runnable, non-runnable and terminated. There is no running state.
But for better understanding the threads, we are explaining it in the 5 states.
The life cycle of the thread in java is controlled by JVM. 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.
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)

Compiled by : Prof. Deepak Gaikar

(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);
}
}

QUESTIONS BANK (CP-II) ( Using java)

Compiled by : Prof. Deepak Gaikar

(5) Explain Life cycle of an applet(diagram, explanation, program)


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

Assignment (OOPM-JAVA)

Compiled by : Prof. Deepak Gaikar

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

QUESTIONS BANK (CP-II) ( Using java)

Compiled by : Prof. 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 .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 .

Potrebbero piacerti anche