[go: up one dir, main page]

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

Program - 11: Aim: Write A Program To Create A Simple Calculator Using Awt. Code

Uploaded by

Vani Vats
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)
13 views8 pages

Program - 11: Aim: Write A Program To Create A Simple Calculator Using Awt. Code

Uploaded by

Vani Vats
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
You are on page 1/ 8

Program – 11

Aim : Write a program to create a simple calculator using awt.


Code:
import java.awt.*;
import
java.awt.event.*;

public class Calculator extends Frame implements ActionListener {


TextField tf;
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bAdd, bSub, bMul, bDiv, bEqual,
bClear;

double num1, num2,


result; int operator;

Calculator() {
tf = new TextField();
tf.setBounds(50, 50, 300,
50);

b1 = new Button("1");
b1.setBounds(50, 120, 50,
50); b2 = new Button("2");
b2.setBounds(120, 120, 50,
50); b3 = new Button("3");
b3.setBounds(190, 120, 50,
50); bAdd = new Button("+");
bAdd.setBounds(260, 120, 50, 50);

b4 = new Button("4");
b4.setBounds(50, 190, 50,
50); b5 = new Button("5");
b5.setBounds(120, 190, 50,
50); b6 = new Button("6");
b6.setBounds(190, 190, 50,
50); bSub = new Button("-");
bSub.setBounds(260, 190, 50, 50);

b7 = new Button("7");
b7.setBounds(50, 260, 50,
50); b8 = new Button("8");
b8.setBounds(120, 260, 50,
50); b9 = new Button("9");
b9.setBounds(190, 260, 50,
50); bMul = new Button("*");
bMul.setBounds(260, 260, 50, 50);

b0 = new Button("0");
b0.setBounds(50, 330, 50, 50);
bEqual = new Button("=");
bEqual.setBounds(120, 330, 50,
50); bClear = new Button("C");
bClear.setBounds(190, 330, 50,
50);
bDiv = new Button("/");
bDiv.setBounds(260, 330, 50,
50);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
bAdd.addActionListener(this);
bSub.addActionListener(this);
bMul.addActionListener(this);
bDiv.addActionListener(this);
bEqual.addActionListener(this
);
bClear.addActionListener(this
);

add(tf);
add(b1);
add(b2);
add(b3);
add(bAdd);
add(b4);
add(b5);
add(b6);
add(bSub);
add(b7);
add(b8);
add(b9);
add(bMul);
add(b0);
add(bEqual);
add(bClear);
add(bDiv);

setSize(400, 400);
setLayout(null);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


String command = e.getActionCommand();
if (command.charAt(0) >= '0' && command.charAt(0) <= '9') {
tf.setText(tf.getText() + command);
} else if (command.charAt(0) == 'C') {
tf.setText("");
} else if (command.charAt(0) == '=') {
num2 =
Double.parseDouble(tf.getText());
switch (operator) {
case 1:
result = num1 + num2;
break
; case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
result = num1 / num2;
break;
}
tf.setText("" + result);
} else {
num1 = Double.parseDouble(tf.getText());
operator = command.charAt(0) == '+' ? 1 : command.charAt(0) == '-' ? 2
: command.charAt(0) == '*' ? 3 : 4;
tf.setText("");
}
}

public static void main(String[]


args) { new Calculator();
}
}

Output:
Program – 10
Aim : Write a program to show multithreaded producer and consumer
application.
Code:
import java.util.LinkedList;

class ProducerConsumer {
LinkedList<Integer> buffer = new
LinkedList<>(); int capacity = 2;

public void produce() throws


InterruptedException { int value = 0;
while (true) {
synchronized (this)
{
while (buffer.size() == capacity)
wait();

System.out.println("Producer produced-" +
value); buffer.add(value++);
notify();
Thread.sleep(1000
);
}
}
}

public void consume() throws


InterruptedException { while (true) {
synchronized (this) {
while (buffer.size() == 0)
wait();

int val = buffer.removeFirst();


System.out.println("Consumer consumed-" +
val); notify();
Thread.sleep(1000);
}
}
}
}

public class Main {


public static void main(String[] args) {
final ProducerConsumer pc = new ProducerConsumer();

Thread producerThread = new Thread(new Runnable() {


@Override
public void run()
{ try {
pc.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});

Thread consumerThread = new Thread(new Runnable() {


@Override
public void run()
{ try {
pc.consume();
} catch (InterruptedException e)
{ e.printStackTrace();
}
}
});

producerThread.start(
);
consumerThread.start(
);
}
}

Output:

You might also like