Untitled Document
Untitled Document
Simple calculator:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public Calculator() {
// Set up the frame
setTitle("Simple Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 400);
setLayout(new BorderLayout());
// Add buttons
String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+",
"C", "%", "(", ")"
};
add(buttonPanel, BorderLayout.CENTER);
}
if (command.matches("[0-9.]")) {
if (start) {
display.setText(command);
start = false;
} else {
display.setText(display.getText() + command);
}
} else if (command.matches("[+\\-*/%]")) {
try {
double x = Double.parseDouble(display.getText());
calculate(x);
lastCommand = command;
start = true;
} catch (NumberFormatException ex) {
display.setText("Error");
}
} else if (command.equals("=")) {
try {
double x = Double.parseDouble(display.getText());
calculate(x);
lastCommand = "=";
start = true;
} catch (NumberFormatException ex) {
display.setText("Error");
}
} else if (command.equals("C")) {
result = 0;
lastCommand = "=";
start = true;
display.setText("0");
}
}
private void calculate(double x) {
try {
if (lastCommand.equals("+")) result += x;
else if (lastCommand.equals("-")) result -= x;
else if (lastCommand.equals("*")) result *= x;
else if (lastCommand.equals("/")) {
if (x == 0) throw new ArithmeticException("Division by zero");
result /= x;
}
else if (lastCommand.equals("%")) {
if (x == 0) throw new ArithmeticException("Division by zero");
result %= x;
}
else if (lastCommand.equals("=")) result = x;
display.setText(String.valueOf(result));
} catch (ArithmeticException ex) {
display.setText("Cannot divide by zero");
result = 0;
start = true;
}
}
}
2.Write a Java program that creates a user interface to perform integer divisions. The
user enters two numbers in the text fields, Num1 and Num2. The division of Num1 and
Num 2 is displayed in the Result field when the Divide button is clicked. If Num1 or Num2
were not an integer, the program would throw a Number Format Exception. If Num2 were
Zero, the program would throw an Arithmetic Exception. Display the exception in a
message dialog box.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public IntegerDivisionCalculator() {
setTitle("Integer Division Calculator");
setLayout(new GridLayout(4, 2, 10, 10));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Initialize components
JLabel num1Label = new JLabel("Num1:");
JLabel num2Label = new JLabel("Num2:");
JLabel resultLabel = new JLabel("Result:");
public MultiThreadApplication() {
setTitle("Multi-Thread Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Initialize components
outputArea = new JTextArea(20, 40);
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
add(scrollPane, BorderLayout.CENTER);
generatorThread.start();
evenThread.start();
oddThread.start();
if (number % 2 == 0) {
appendText("Even number detected: " + number);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}
}
}
if (number % 2 != 0) {
appendText("Odd number detected: " + number);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}
}
}
class Node {
int data;
Node prev;
Node next;
Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
class DoublyLinkedList {
Node head;
Node tail;
DoublyLinkedList() {
head = null;
tail = null;
}
// Insert at front
void insertFront(int data) {
Node newNode = new Node(data);
if (head == null) {
head = tail = newNode;
} else {
newNode.next = head;
head.prev = newNode;
head = newNode;
}
System.out.println("Inserted " + data + " at front");
}
// Insert at back
void insertBack(int data) {
Node newNode = new Node(data);
if (tail == null) {
head = tail = newNode;
} else {
newNode.prev = tail;
tail.next = newNode;
tail = newNode;
}
System.out.println("Inserted " + data + " at back");
}
// If list is empty
if (head == null) {
System.out.println("List is empty");
return;
}
System.out.println("List contents:");
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}
while (true) {
System.out.println("\nDoubly Linked List Operations:");
System.out.println("1. Insert at Front");
System.out.println("2. Insert at Back");
System.out.println("3. Delete Element");
System.out.println("4. Display List");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
switch (choice) {
case 1:
System.out.print("Enter element to insert at front: ");
int frontElement = scanner.nextInt();
list.insertFront(frontElement);
break;
case 2:
System.out.print("Enter element to insert at back: ");
int backElement = scanner.nextInt();
list.insertBack(backElement);
break;
case 3:
System.out.print("Enter element to delete: ");
int deleteElement = scanner.nextInt();
list.delete(deleteElement);
break;
case 4:
list.display();
break;
case 5:
System.out.println("Exiting program...");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice! Please try again.");
}
}
}
}
5.Write a Java program that simulates a traffic light. The program lets the user select one
of three lights: red, yellow, or green with radio buttons. On selecting a button, an
appropriate message with “Stop” or “Ready” or “Go” should appear above the buttons in
the selected color. Initially, there is no message shown.
import javax.swing.*;
import java.awt.*;
public TrafficLightSimulation() {
setTitle("Traffic Light Simulation");
setLayout(new BorderLayout(10, 10));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create panels
JPanel messagePanel = new JPanel();
JPanel buttonPanel = new JPanel();
// Initialize components
messageLabel = new JLabel(" ");
messageLabel.setFont(new Font("Arial", Font.BOLD, 24));
case "yellow":
messageLabel.setText("READY");
messageLabel.setForeground(Color.YELLOW);
break;
case "green":
messageLabel.setText("GO");
messageLabel.setForeground(Color.GREEN);
break;
}
}