0% found this document useful (0 votes)
5 views

Java Unit-4 Assignment Answers

Uploaded by

karanwork167
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Unit-4 Assignment Answers

Uploaded by

karanwork167
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JAVA Unit – 4

Assignment Answers
Theory

1. Write a Life cycle of Thread.


Ans : The life cycle of a thread in Java consists of several
states, and a thread transitions through these states during
its execution. The Thread class in Java provides methods to
control and monitor the life cycle of a thread. Here are the
various states in the life cycle of a thread:
New State:
A thread is in the new state when an instance of the Thread
class is created, but before the start() method is called.
In this state, the thread is not yet scheduled for execution.
Runnable State:
A thread enters the runnable state when the start() method
is invoked.
The thread is now eligible to run, and the Java Virtual
Machine (JVM) will schedule it to run on the CPU whenever
it gets a chance.
However, it does not necessarily mean that the thread is
currently running; it's just ready to run.
Blocked (or Waiting) State:
A thread enters the blocked state when it needs to wait for a
monitor lock to enter a synchronized block or method.
It can also be in a blocked state when waiting for I/O
operations to complete.
Timed Waiting State:
A thread enters the timed waiting state when it calls a
method with a specified waiting time, such as sleep() or
wait(timeout).
The thread remains in this state until the specified time
elapses or it receives a notification to wake up.
Waiting State:
A thread enters the waiting state when it is waiting
indefinitely for another thread to perform a particular action.
It is typically waiting for a notification from another thread.
Terminated (or Dead) State:
A thread enters the terminated state when its run() method
completes or when the stop() method is explicitly called.
A terminated thread cannot be started again. Once in this
state, a thread cannot transition to any other state.
2. Write a Life cycle of Applet.
Ans : The life cycle of an applet in Java refers to the sequence
of method calls and events that occur from the initialization to
the destruction of the applet. The Applet class in Java provides
several methods that you can override to control the life cycle
of your applet. Here are the key methods involved in the life
cycle of an applet:
1. init():
 This method is called when the applet is first loaded
into memory.
 It is used for one-time initialization tasks, such as
creating GUI components and setting up initial values.
 It is called only once during the lifetime of the applet.
2. start():
 The start() method is called after the init() method and
also whenever the user returns to the web page
containing the applet.
 It is used to start or resume execution of the applet after
it has been stopped (e.g., due to the user navigating
away).
3. stop():
 The stop() method is called when the user leaves the
web page containing the applet.
 It is used to stop or suspend the execution of the
applet.
 Resources may be released or paused during this
method.
4. destroy():
 The destroy() method is called when the applet is no
longer needed or when the web page is closed.
 It is used to perform cleanup operations, release
resources, and finalize the applet.
5. paint(Graphics g):
 The paint() method is called whenever the applet
needs to redraw its content.
 It is responsible for rendering graphics on the applet's
display area.
6. update(Graphics g):
 The update() method is called automatically after
paint().
 It clears the applet's display area before calling paint().
 It is commonly overridden when double buffering is
used for smoother graphics.
7. repaint():
 The repaint() method is called to request the repainting
of the applet.
 It causes the update() and paint() methods to be
called.
8. getAppletInfo():
 The getAppletInfo() method provides information
about the applet.
 It returns a string that can be displayed by an applet
viewer.
3. Write a difference between Applet and Application.
Ans :
APPLET APPLICATION

1. It is small program uses another application 1. An application is the programs executed on

program for its execution. the computer independently.

2. Do not use the main method 2. Uses the main method for execution

3. Cannot run independently require API's (Ex. Web 3. Can run alone but require JRE.

API).

4. Prior installation is not needed 4. Requires prior explicit installation on the

local computer.

5. The files cannot be read and write on the local 5. Applications are capable of performing

computer through applet. those operations to the files on the local

computer.

6. Cannot communicate with other servers. 6. Communication with other servers is

probably possible.

7. Applets cannot access files residing on the local 7. Can access any data or file available on the

computer. system.

8. Requires security for the system as they are 8. No security concerns are there.

untrusted.
Practical :

1. Write a java program to create 2 threads each thread


calculates the sum and average of 1 to 10 and 11 to 20
respectively. After all thread finish, main thread should print
message “ Task Completed”. Write this program with use of
runnable interface.
Code :
class SumAverageCalculator implements Runnable {
private int start;
private int end;

SumAverageCalculator(int start, int end) {


this.start = start;
this.end = end;
}

@Override
public void run() {
int sum = 0;
for (int i = start; i <= end; i++) {
sum += i;
}

double average = (double) sum / (end - start + 1);

System.out.println("Sum of numbers " + start + " to " + end + ": " +


sum);
System.out.println("Average of numbers " + start + " to " + end + ": " +
average);
}
}

public class ThreadExample {


public static void main(String[] args) {
Thread thread1 = new Thread(new SumAverageCalculator(1, 10));
Thread thread2 = new Thread(new SumAverageCalculator(11, 20));

thread1.start();
thread2.start();

try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("Task Completed");
}
}

2. Create an applet which draws a line, rectangle and filled


circle in applet display area.
Code :
import java.applet.Applet;
import java.awt.Graphics;

public class DrawingApplet extends Applet {


public void paint(Graphics g) {
// Draw a line
g.drawLine(20, 20, 100, 20);

// Draw a rectangle
g.drawRect(20, 40, 80, 40);

// Draw a filled circle


g.fillOval(20, 100, 80, 80);
}
}

HTML FILE :
<html>
<body>
<applet code="DrawingApplet.class" width="200" height="200"></applet>
</body>
</html>
3. Write an applet that take 2 numbers as parameter and
display their average and sum.
Code :
import java.applet.Applet;
import java.awt.Graphics;

public class AverageSumApplet extends Applet {


private int num1;
private int num2;
private double sum;
private double average;

public void init() {


// Retrieve parameters from HTML file
String num1Str = getParameter("num1");
String num2Str = getParameter("num2");

// Convert string parameters to integers


num1 = Integer.parseInt(num1Str);
num2 = Integer.parseInt(num2Str);

// Calculate sum and average


sum = num1 + num2;
average = sum / 2.0;
}

public void paint(Graphics g) {


// Display sum and average
g.drawString("Sum: " + sum, 20, 20);
g.drawString("Average: " + average, 20, 40);
}
}

HTML FILE :
<html>
<body>
<applet code="AverageSumApplet.class" width="200" height="200">
<param name="num1" value="5">
<param name="num2" value="10">
</applet>
</body>
</html>

You might also like