JAVA PROGRAMMING ASSIGNMENT-2
NAME:T.Sai varshitha Roll
number:21691A04K6
1)public class AlternateNumberPrinting {
private static final int MAX_COUNTS = 10;
private static int currNumber = 1;
private static final Objlock = new Object();
public static void main(String args[]) {
Threadthread1 = new Thread(new NumPrinter(1));
Threadthread2 = new Thread(new NumPrinter(2));
thread1.start();
thread2.start();
static class NumPrinter implements Runnable {
private final int thread3;
public NumberPrinter(int threadId) {
this.thread3= thread3;
public void run() {
while (true) {
synchronized (lock) {
if (currNumber >
MAX_COUNTS) {
break;
if (currNumber % 2 == thread3 - 1) {
System.out.println("Thread-" + thread3 + ": " +
currNumber);
currNumber++;
lock.notifyAll();
} else {
try {
lock.wait()
} catch (InterruptedException e) {
e.printStackTrace();
2)
import java.util.LinkedList4;
import java.util.Scanner;
public class LinkedList4SumAve {
public static void main(String args[]) {
LinkedList4<Integer> numbers = new LinkedList4<>();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a Value:");
while (scanner.hasNextInt()) {
int num = scanner.nextInt();
numbers.add(num);
. }
int sum = 0;
for (int num :
numbers) {
double average = (double) sum /
numbers.size();
System.out.println("Numbers entered: " + numbers);
System.out.println("Sum: " + sum);
System.out.println("Ave: " + average);
scanner.close();
}}
3)
import java.awt.*;
import java.awt.event.*;
public class SimpleCalculator extends Frame implements ActionListener {
Public TextField textField;
public Button[] numberButtons;
private Button addButton, subtractButton, multiplyButton, divideButton, equalsButton,
clearButton;
private double num1 = 0, num2 = 0;
public SimpleCalculator() {
setTitle("Simple Calculator");
setSize(300, 300);
setLayout(new BorderLayout());
setResizable(false);
textField = new TextField();
textField.setEditable(false);
add(textField,
Panel buttonPanel = new Panel();
buttonPanel.setLayout(new GridLayout(4, 4));
numberButtons = new
Button[10];
for (int i = 0; i < 10