0% found this document useful (0 votes)
43 views8 pages

Experiment - 7: Source Code

The document describes two Java programs: 1) A producer-consumer program that uses threads to simulate production and consumption of materials in a shared shop. A producer thread puts materials in the shop and a consumer thread gets materials from the shop. 2) An analog clock applet that uses graphics to draw the face of a clock and update the hands every second to show the current time. It draws the clock face as a circle with number labels and draws the hour, minute, and second hands as lines rotating around the circle.

Uploaded by

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

Experiment - 7: Source Code

The document describes two Java programs: 1) A producer-consumer program that uses threads to simulate production and consumption of materials in a shared shop. A producer thread puts materials in the shop and a consumer thread gets materials from the shop. 2) An analog clock applet that uses graphics to draw the face of a clock and update the hands every second to show the current time. It draws the clock face as a circle with number labels and draws the hour, minute, and second hands as lines rotating around the circle.

Uploaded by

MD NADEEM ASGAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

EXPERIMENT– 7

Write a java program to show producer consumer application.

SOURCE CODE:
public class ProducerConsumer
{
public static void main(String[] args)
{
Shop c = new Shop();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
p1.start();
c1.start();
}
}

class Shop
{
private int materials;
private boolean available = false;
public synchronized int get()
{
while (available == false)
{
try
{
wait();
}
catch (InterruptedException ie)
{
}
}
available = false;
notifyAll();
return materials;
}
public synchronized void put(int value)
{
while (available == true)
{
try
{
wait();
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
materials = value;
available = true;
notifyAll();
}
}

class Consumer extends Thread


{
private Shop Shop;
private int number;
public Consumer(Shop c, int number)
{
Shop = c;
this.number = number;
}
public void run()
{
int value = 0;
for (int i = 0; i < 10; i++)
{
value = Shop.get();
System.out.println("Consumed value " + this.number+ " got: " + value);
}
}
}

class Producer extends Thread


{
private Shop Shop;
private int number;
public Producer(Shop c, int number)
{
Shop = c;
this.number = number;
}
public void run()
{
for (int i = 0; i < 10; i++)
{
Shop.put(i);
System.out.println("Produced value " + this.number+ " put: " + i);
try
{
sleep((int)(Math.random() * 100));
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
System.out.println("Code By Nadeem Asgar");
}
}

OUTPUT:
EXPERIMENT – 8

Write a java program to show life cycle of an applet.

SOURCE CODE:

import java.applet.Applet;
import java.awt.Graphics;
public class AppletLifeCycleDemo extends Applet {
String msg = "";
public void init() {
msg += "init( ) -> ";
}
public void start() {
msg += "start( ) -> ";
}
public void paint(Graphics g) {
msg += "paint( )";
g.drawString(msg, 20, 20);
}
public void stop() {
msg += " -> stop( )";
}
public void destroy() {
msg += "-> destroy( )";
}
}
OUTPUT:
EXPERIMENT– 9

Write a java program to make an analog clock using applet.

SOURCE CODE:
import java.applet.Applet;
import java.awt.*;
import java.util.*;
public class analogClock extends Applet
{
public void init()
{
this.setSize(new Dimension(800, 400));
setBackground(new Color(50, 50, 50));
new Thread() {
public void run()
{
while (true) {
repaint();
delayAnimation();
}
}
}.start();
}

private void delayAnimation()


{
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}

public void paint(Graphics g)


{
Calendar time = Calendar.getInstance();
int hour = time.get(Calendar.HOUR_OF_DAY);
int minute = time.get(Calendar.MINUTE);
int second = time.get(Calendar.SECOND);
if (hour > 12) {
hour -= 12;
}
g.setColor(Color.white);
g.fillOval(300, 100, 200, 200);
g.setColor(Color.black);
g.drawString("12", 390, 120);
g.drawString("9", 310, 200);
g.drawString("6", 400, 290);
g.drawString("3", 480, 200);
double angle;
int x, y;
angle = Math.toRadians((15 - second) * 6);
x = (int)(Math.cos(angle) * 100);
y = (int)(Math.sin(angle) * 100);
g.setColor(Color.red);
g.drawLine(400, 200, 400 + x, 200 - y);
angle = Math.toRadians((15 - minute) * 6);
x = (int)(Math.cos(angle) * 80);
y = (int)(Math.sin(angle) * 80);
g.setColor(Color.blue);
g.drawLine(400, 200, 400 + x, 200 - y);
angle = Math.toRadians((15 - (hour * 5)) * 6);
x = (int)(Math.cos(angle) * 50);
y = (int)(Math.sin(angle) * 50);
g.setColor(Color.black);
g.drawLine(400, 200, 400 + x, 200 - y);
}
}

OUTPUT:

You might also like