Computer Science & Engineering: Department of
Computer Science & Engineering: Department of
Algorithm:
1. Initialize an ArrayList to store Employee objects (ID, Name, Salary).
2. Display menu options for user actions (Add, Update, Remove, Search, Display, Exit).
3. Loop until the user chooses to exit:
o Option 1 (Add Employee):
▪ Take user input (ID, Name, Salary).
▪ Create an Employee object and add it to the list. o Option 2 (Update
Employee):
▪ Take user input for the employee ID to update.
▪ Search the list for the ID.
▪ If found, update the Name and Salary.
o Option 3 (Remove Employee):
▪ Take user input for the employee ID to remove.
▪ Search and remove the matching employee from the list.
o Option 4 (Search Employee):
▪ Take user input for the ID.
▪ Search and display the employee details if found.
o Option 5 (Display All Employees):
▪ Iterate and display all employees.
o Option 6 (Exit):
▪ Terminate the loop and exit the program.
Code:
import
java.util.ArrayList;
import java.util.Scanner;
class Employee { int
id;
String name; double salary; public
Employee(int id, String name, double salary) {
this.id = id; this.name = name;
this.salary = salary;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
public String toString() { return "ID: " + id + ", Name: " +
name + ", Salary: " + salary;
}
} public class Main { // Changed class name from EmployeeManager to
Main public static void main(String[] args) {
ArrayList<Employee> employees = new ArrayList<>();
Scanner scanner = new Scanner(System.in); while
(true) {
System.out.println("\n1. Add Employee\n2. Update Employee\n3. Remove
Employee\n4. Search Employee\n5. Display All Employees\n6. Exit");
System.out.print("Enter your choice: "); int choice =
scanner.nextInt(); switch (choice) { case 1:
System.out.print("Enter ID: ");
int id = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter Name: ");
String name = scanner.nextLine();
System.out.print("Enter Salary: "); double salary =
scanner.nextDouble(); employees.add(new
Employee(id, name, salary));
System.out.println("Employee Added Successfully!");
break; case 2:
System.out.print("Enter ID to Update:
"); int updateId = scanner.nextInt();
scanner.nextLine(); boolean found =
false; for (Employee emp : employees) {
if (emp.id == updateId) {
System.out.print("Enter New Name: ");
emp.name = scanner.nextLine();
System.out.print("Enter New Salary: ");
emp.salary = scanner.nextDouble();
System.out.println("Employee Updated
Successfully!"); found = true; break;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
}
if (!found) System.out.println("Employee Not
Found!"); break; case 3:
System.out.print("Enter ID to Remove: ");
int removeId = scanner.nextInt();
employees.removeIf(emp -> emp.id == removeId);
System.out.println("Employee Removed Successfully!");
break; case 4:
System.out.print("Enter ID to Search:
"); int searchId = scanner.nextInt();
boolean searchFound = false; for
(Employee emp : employees) { if
(emp.id == searchId) {
System.out.println(emp);
searchFound = true;
}
}
if (!searchFound) System.out.println("Employee Not
Found!"); break; case 5:
System.out.println("Employee List:");
for (Employee emp : employees) {
System.out.println(emp);
}
break;
case 6:
System.out.println("Exiting...");
scanner.close(); return;
default:
System.out.println("Invalid Choice! Try Again.");
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
}
Output:
Problem Statement: Create a program to collect and store all the cards to assist
the users in finding all the cards in a given symbol using Collection interface.
Algorithm:
• Initialize a HashMap where
Code:
import java.util.*;
class Card {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
String suit;
String rank;
public Card(String suit, String rank) {
this.suit = suit;
this.rank = rank;
}
public String toString() {
return rank + " of " + suit;
}}
public class Main { public static void
main(String[] args) { List<Card> deck
= new ArrayList<>();
String[] suits = { "Hearts", "Diamonds", "Clubs", "Spades" };
String[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"
}; for (String suit : suits) { for (String rank : ranks) {
deck.add(new Card(suit, rank));
}
}
Scanner scanner = new Scanner(System.in);
System.out.print("Enter suit to search (Hearts, Diamonds, Clubs, Spades): ");
String suitToSearch = scanner.nextLine();
System.out.println("Cards found in " + suitToSearch + ":");
for (Card card : deck) {
if (card.suit.equalsIgnoreCase(suitToSearch)) {
System.out.println(card);
}
}
scanner.close();
}
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Problem Statement: Develop a ticket booking system with synchronized threads to ensure
no double booking of seats. Use thread priorities to simulate VIP bookings being processed
first.
Algorithm:
1. Initialize a shared ticket booking system with a limited number of seats.
2. Create a lock mechanism to ensure only one thread can book a seat at a time.
3. Define a BookingThread class that:
o Takes a user name and priority.
o Calls the synchronized bookTicket() method to attempt booking.
4. Inside bookTicket():
o Check if seats are available.
o If available, book a seat and decrement the count. o If not, display a message indicating
no availability.
5. Create multiple threads, setting VIP users with higher priority.
6. Start the threads to simulate concurrent booking.
7. Threads execute and book seats, prioritizing VIP users first.
8. End the program after all threads finish execution.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Code:
import java.util.*; class Ticket {
seatNumber) {
this.seatNumber = seatNumber;
this.isBooked = false;
Ticket(i));
return;
if (!ticket.isBooked) { ticket.isBooked
= true;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
System.out.println(passengerName + "
} else {
vipBooking.setPriority(Thread.MAX_PRIORITY);
normalBooking.setPriority(Thread.MIN_PRIORITY);
vipBooking.start();
normalBooking.start();
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
COMPUTER SCIENCE & ENGINEERING
Learning Outcomes: