Experiment - 7: Source Code

Download as pdf or txt
Download as pdf or txt
You are on page 1of 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