Java Ela 22mic0101 4
Java Ela 22mic0101 4
if (selectedSeat != null) {
System.out.println(userName + " booked seat at row " + (selectedSeat[0]
+ 1) + ", column " + (selectedSeat[1] + 1));
} else {
System.out.println(userName + " couldn't book a seat.");
}
};
try {
for (Thread userThread : userThreads) {
userThread.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
seatMatrix[row][column] = true;
return new int[]{row, column};
} finally {
lock.unlock();
}
}
INPUT/OUTPUT:
PROGRAMMING IN JAVA ELA DIGITAL ASSIGNMENT 4
PROGRAMMING IN JAVA ELA DIGITAL ASSIGNMENT 4
QUESTION 2:
SOURCE CODE:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Customer {
private String name;
private String address;
private String accountNumber;
private double balance;
private String phoneNumber;
if (amount > 0) {
balance -= amount;
System.out.println("Withdrawn $" + amount);
} else {
System.out.println("Invalid withdrawal amount.");
}
}
while (true) {
System.out.println("Welcome to the Online Banking Application");
System.out.println("1. Create Customer");
System.out.println("2. View Balance");
System.out.println("3. Deposit");
System.out.println("4. Withdraw");
System.out.println("5. Exit");
System.out.print("Please select an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter customer name: ");
String name = scanner.nextLine();
System.out.print("Enter customer address: ");
String address = scanner.nextLine();
System.out.print("Enter account number: ");
PROGRAMMING IN JAVA ELA DIGITAL ASSIGNMENT 4
String accountNumber = scanner.nextLine();
System.out.print("Enter initial balance: $");
double balance = scanner.nextDouble();
scanner.nextLine(); // Consume newline
System.out.print("Enter phone number: ");
String phoneNumber = scanner.nextLine();
customers.put(accountNumber, new Customer(name, address,
accountNumber, balance, phoneNumber));
System.out.println("Customer created successfully.");
break;
case 2:
System.out.print("Enter account number: ");
String viewAccountNumber = scanner.nextLine();
Customer viewCustomer = customers.get(viewAccountNumber);
if (viewCustomer != null) {
System.out.println("Account Balance: $" +
viewCustomer.getBalance());
} else {
System.out.println("Customer not found.");
}
break;
case 3:
System.out.print("Enter account number: ");
String depositAccountNumber = scanner.nextLine();
Customer depositCustomer = customers.get(depositAccountNumber);
if (depositCustomer != null) {
System.out.print("Enter deposit amount: $");
double depositAmount = scanner.nextDouble();
depositCustomer.deposit(depositAmount);
} else {
System.out.println("Customer not found.");
}
break;
case 4:
System.out.print("Enter account number: ");
String withdrawAccountNumber = scanner.nextLine();
Customer withdrawCustomer = customers.get(withdrawAccountNumber);
if (withdrawCustomer != null) {
System.out.print("Enter withdrawal amount: $");
double withdrawAmount = scanner.nextDouble();
withdrawCustomer.withdraw(withdrawAmount);
} else {
System.out.println("Customer not found.");
}
break;
case 5:
System.out.println("Thank you for using the Online Banking
Application. Goodbye!");
System.exit(0);
default:
System.out.println("Invalid option. Please select a valid
option.");
}
}
}
}
INPUT/OUTPUT:
PROGRAMMING IN JAVA ELA DIGITAL ASSIGNMENT 4
PROGRAMMING IN JAVA ELA DIGITAL ASSIGNMENT 4
PROGRAMMING IN JAVA ELA DIGITAL ASSIGNMENT 4
QUESTION 3:
SOURCE CODE:
public class Main {
while (true) {
System.out.println("Recruiting Company - Candidate Entry System");
PROGRAMMING IN JAVA ELA DIGITAL ASSIGNMENT 4
System.out.print("Enter candidate name: ");
String name = scanner.nextLine();
System.out.print("Enter candidate phone number: ");
String phoneNumber = scanner.nextLine();
System.out.print("Enter candidate email: ");
String email = scanner.nextLine();
System.out.print("Enter one-page write-up on your field of expertise: ");
String writeUp = scanner.nextLine();
INPUT/OUTPUT:
FILE:
QUESTION 4:
a. No, the provided class Algorithm will not compile. The reason is that the < operator
and the > operator cannot be used with generic types in this manner. In the line return
x > y ? x : y;, the compiler doesn't have enough information to compare x and y
because generic types are not guaranteed to have comparison operators defined. To
make this code compile, you would need to use a type that can be compared, such as
Comparable, and specify the generic type with an upper bound that indicates it
implements the Comparable interface.
b. public class ArrayUtils {
public static <T> void exchangeElements(T[] array, int index1, int index2) {
if (index1 < 0 || index1 >= array.length || index2 < 0 || index2 >= array.length) {
throw new IndexOutOfBoundsException("Invalid indices");
}
T temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
PROGRAMMING IN JAVA ELA DIGITAL ASSIGNMENT 4
Code Clarity: Generics improve code readability and maintainability by expressing the
intended types in the code. This makes the code more self-documenting and
understandable to developers, as the types are explicitly stated.
Code Reusability: Generics enable you to write generic classes and methods that work
with different data types. This promotes code reusability and reduces code duplication, as
the same logic can be applied to various data types.
d. After type erasure, the Pair class will be transformed by replacing the type parameters
K and V with their respective bounds, which are Object since they are not bounded.
Here's the class after type erasure:
public class Pair {
public Pair(Object key, Object value) {
this.key = key;
this.value = value;
}
QUESTION 5:
SOURCE CODE:
import java.util.HashMap;
import java.util.Map;
INPUT/OUTPUT:
PROGRAMMING IN JAVA ELA DIGITAL ASSIGNMENT 4
PROGRAMMING IN JAVA ELA DIGITAL ASSIGNMENT 4