0% found this document useful (0 votes)
2 views9 pages

Program 16-21 Java

The document contains multiple Java programs demonstrating various concepts, including multithreading with a producer-consumer model, custom exception handling, file manipulation to convert text to uppercase, creating an analog clock applet, and handling button and mouse events in applets. Each program is accompanied by source code and aims to illustrate specific programming techniques. Overall, the document serves as a practical guide for Java programming applications.

Uploaded by

jbansal1275
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Program 16-21 Java

The document contains multiple Java programs demonstrating various concepts, including multithreading with a producer-consumer model, custom exception handling, file manipulation to convert text to uppercase, creating an analog clock applet, and handling button and mouse events in applets. Each program is accompanied by source code and aims to illustrate specific programming techniques. Overall, the document serves as a practical guide for Java programming applications.

Uploaded by

jbansal1275
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Program 16

Aim: Write a Java program to show multithreaded producer and consumer application.

Source Code:
import [Link];

public class Sixteenth {


public static void main(String[] args) throws Exception {
PC pc = new PC();
Thread t1 = new Thread(() -> {
try {
[Link]();
} catch (Exception e) {
}
});
Thread t2 = new Thread(() -> {
try {
[Link]();
} catch (Exception e) {
}
});
[Link]();
[Link]();
[Link]();
[Link]();
}

static class PC {
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;

public void produce() throws InterruptedException {


int value = 0;
while (true) {
synchronized (this) {
while ([Link]() == capacity)
wait();
[Link]("Producer produced-" + value);
[Link](value++);
notify();
}
[Link](1000);
}
}

public void consume() throws InterruptedException {


while (true) {
synchronized (this) {
while ([Link]())
wait();
int val = [Link]();
[Link]("Consumer consumed-" + val);
notify();
}
[Link](1000);
}
}
}
}

Output:
Program 17

Aim: Create a Customized Exception and also make use of all the 5 exception keywords.

Source Code:
class InvalidAgeException extends Exception {
InvalidAgeException(String msg) {
super(msg);
}
}

public class Seventeenth {


static void checkAge(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Underage not allowed");
} else {
[Link]("Access granted");
}
}

public static void main(String[] args) {


try {
checkAge(15);
} catch (InvalidAgeException e) {
[Link]("Caught: " + [Link]());
} finally {
[Link]("Finally block executed");
}
}
}

Output:
Program 18

Aim: WAP to Convert the content of given file in to uppercase content of same file.

Source Code:
import [Link].*;

public class Eighteenth {


public static void main(String[] args) {
String filePath = "src/[Link]";

try {
BufferedReader reader = new BufferedReader(new
FileReader(filePath));
StringBuilder content = new StringBuilder();
String line;

while ((line = [Link]()) != null) {


[Link]([Link]())
.append([Link]());
}

[Link]();

BufferedWriter writer = new BufferedWriter(new


FileWriter(filePath));
[Link]([Link]());
[Link]();

[Link]("File converted to uppercase


successfully!");
} catch (IOException e) {
[Link]();
}
}
}
Output:
Program 19

Aim: WAP to Develop an Analog clock using Applet.

Source Code:
import [Link];
import [Link].*;
import [Link];

public class AnalogClock extends Applet implements Runnable {


Thread t;
int hours, minutes, seconds;
public void init() {
setSize(400, 400);
t = new Thread(this);
[Link]();
}
public void run() {
while (true) {
Calendar cal = [Link]();
hours = [Link]([Link]);
minutes = [Link]([Link]);
seconds = [Link]([Link]);

repaint();

try {
[Link](1000);
} catch (InterruptedException e) {
}
}
}
public void paint(Graphics g) {
int xCenter = 200, yCenter = 200;
[Link](50, 50, 300, 300);
[Link]("12", 190, 70);
[Link]("3", 320, 205);
[Link]("6", 195, 340);
[Link]("9", 60, 205);
double secondAngle = [Link](seconds * 6 - 90);
double minuteAngle = [Link](minutes * 6 - 90);
double hourAngle = [Link]((hours * 30 + minutes * 0.5)
- 90);
int xSec = (int) (xCenter + 120 * [Link](secondAngle));
int ySec = (int) (yCenter + 120 * [Link](secondAngle));
[Link]([Link]);
[Link](xCenter, yCenter, xSec, ySec);
int xMin = (int) (xCenter + 100 * [Link](minuteAngle));
int yMin = (int) (yCenter + 100 * [Link](minuteAngle));
[Link]([Link]);
[Link](xCenter, yCenter, xMin, yMin);
int xHour = (int) (xCenter + 70 * [Link](hourAngle));
int yHour = (int) (yCenter + 70 * [Link](hourAngle));
[Link]([Link]);
[Link](xCenter, yCenter, xHour, yHour);
}
}

Output:
Program 20

Aim: WAP to show event handling in applet that prints a message by click on the button

Source Code:
import [Link];
import [Link].*;
import [Link].*;

public class ButtonEventApplet extends Applet implements ActionListener


{
Button btn;
String message = "";
public void init() {
btn = new Button("Click Me");
add(btn);
[Link](this);
}
public void actionPerformed(ActionEvent e) {
message = "Button Clicked!";
repaint();
}
public void paint(Graphics g) {
[Link](message, 150, 100);
}
}

Output:
Program 21

Aim: WAP to show event handling in applet that prints a message by click on the button

Source Code:
import [Link];
import [Link].*;
import [Link].*;

public class MouseDragPaint extends Applet implements


MouseMotionListener {
int x, y;
public void init() {
addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent e) {
x = [Link]();
y = [Link]();
Graphics g = getGraphics();
[Link](x, y, 5, 5);
}
public void mouseMoved(MouseEvent e) {
}
}

Output:

You might also like