NAME: VIVEK MAINALI
UNIVERSITY ROLL NO: 2025592
SECTION: B
QUES14 Build a Library Management System . The system needs to handle Books,
Magazines, and E-books, each with behaviors such as borrowing, returning, and
calculating late fees. You need to design an interface that defines common behaviors for all
types of items, such as borrowing, returning, and calculating late fees. You will also define
specific methods that each type of item must implement, as well as default methods that
provide common behavior shared across different item types. Additionally, the system
should handle exceptions such as invalid return dates and overdue items.
CODE:
import [Link];
import [Link];
import [Link];
class InvalidItemException extends Exception {
public InvalidItemException(String msg) {
super(msg);
}
}
interface LibraryOperations {
void borrowItem(LocalDate borrowDate);
void returnItem(LocalDate returnDate);
double calculateLateFee(LocalDate returnDate);
static boolean isValidItemId(String itemId) {
return [Link]("ITEM\\d{4}");
}
}
class Book implements LibraryOperations {
String itemId, title;
boolean borrowed = false;
LocalDate dueDate;
public Book(String id, String title) throws InvalidItemException {
if () {
throw new InvalidItemException("Invalid ID!");
}
[Link] = id;
[Link] = title;
}
public void borrowItem(LocalDate borrowDate) {
borrowed = true;
dueDate = [Link](7);
[Link]("Borrowed! Due Date: " + dueDate);
}
public void returnItem(LocalDate returnDate) {
double fine = calculateLateFee(returnDate);
if (fine > 0)
[Link]("Late! Fine = ₹" + fine);
else
[Link]("Returned on time");
borrowed = false;
}
public double calculateLateFee(LocalDate returnDate) {
if ([Link](dueDate)) {
long days = [Link](dueDate, returnDate);
return days * 10;
}
return 0;
}
}
class Magazine implements LibraryOperations {
String itemId, title;
LocalDate dueDate;
public Magazine(String id, String title) throws InvalidItemException {
if () {
throw new InvalidItemException("Invalid ID!");
}
[Link] = id;
[Link] = title;
}
public void borrowItem(LocalDate borrowDate) {
dueDate = [Link](5);
[Link]("Magazine Borrowed! Due: " + dueDate);
}
public void returnItem(LocalDate returnDate) {
double fine = calculateLateFee(returnDate);
[Link](fine > 0 ? "Fine = ₹" + fine : "Returned on time");
}
public double calculateLateFee(LocalDate returnDate) {
if ([Link](dueDate)) {
long days = [Link](dueDate, returnDate);
return days * 5;
}
return 0;
}
}
class EBook implements LibraryOperations {
String itemId, title;
public EBook(String id, String title) throws InvalidItemException {
if () {
throw new InvalidItemException("Invalid ID!");
}
[Link] = id;
[Link] = title;
}
public void borrowItem(LocalDate borrowDate) {
[Link]("EBook Downloaded!");
}
public void returnItem(LocalDate returnDate) {
[Link]("No return needed");
}
public double calculateLateFee(LocalDate returnDate) {
return 0;
}}
public class fourteen{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {[Link]("Enter Item ID (ITEM1234): ");
String id = [Link]();
[Link]("Enter Title: ");
String title = [Link]();
[Link]("[Link] [Link] [Link]");
int type = [Link]();
LibraryOperations item;
if (type == 1)
item = new Book(id, title);
else if (type == 2)
item = new Magazine(id, title);
else
item = new EBook(id, title);
int choice;
do {
[Link]("\[Link] [Link] [Link]");
choice = [Link]();
switch (choice) {
case 1: [Link]("Enter Borrow Date (YYYY-MM-DD): ");
LocalDate bDate = [Link]([Link]());
[Link](bDate);
break;
case 2: [Link]("Enter Return Date (YYYY-MM-DD): ");
LocalDate rDate = [Link]([Link]());
[Link](rDate);
break;}
} while (choice != 3);
} catch (InvalidItemException e) {
[Link]("Error: " + [Link]());
}
[Link]();}}
OUTPUT
C:\Users\VIVEK\OneDrive\Desktop>javac [Link]
C:\Users\VIVEK\OneDrive\Desktop>java fourteen
Enter Item ID (ITEM1234): ITEM2024
Enter Title: CSE
[Link] [Link] [Link]
1
[Link] [Link] [Link]
1
Enter Borrow Date (YYYY-MM-DD): 2026-03-05
Borrowed! Due Date: 2026-03-12
[Link] [Link] [Link]
2
Enter Return Date (YYYY-MM-DD): 2026-03-25
Late Fine = 130.0
[Link] [Link] [Link]
3
NAME: VIVEK MAINALI
UNIVERSITY ROLL NO: 2025592
SECTION: B
QUES15 Design a Java application to validate student admission details using custom
exceptions. The system checks that:
• Name is not empty
• Age is between 17–25
• Email contains '@' and a valid domain
• Marks are between 0–100
• Marks ≥ 60 are required for admission
CODE:
import [Link];
class InvalidNameException extends Exception {
public InvalidNameException(String msg) { super(msg); }
}
class InvalidAgeException extends Exception {
public InvalidAgeException(String msg) { super(msg); }
}
class InvalidEmailException extends Exception {
public InvalidEmailException(String msg) { super(msg); }
}
class InvalidMarksException extends Exception {
public InvalidMarksException(String msg) { super(msg); }
}
class NotEligibleException extends Exception {
public NotEligibleException(String msg) { super(msg); }
}
class Student {
String name, email;
int age;
double marks;
public Student(String name, int age, String email, double marks)
throws InvalidNameException, InvalidAgeException,
InvalidEmailException, InvalidMarksException, NotEligibleException {
if (name == null || [Link]().isEmpty())
throw new InvalidNameException("Name cannot be empty");
if (age < 17 || age > 25)
throw new InvalidAgeException("Age must be 17–25");
if ( || )
throw new InvalidEmailException("Invalid Email");
if (marks < 0 || marks > 100)
throw new InvalidMarksException("Marks must be 0–100");
if (marks < 60)
throw new NotEligibleException("Marks < 60 → Not Eligible");
[Link] = name;
[Link] = age;
[Link] = email;
[Link] = marks;
}
public void display() {
[Link]("Name: " + name + ", Age: " + age +
", Email: " + email + ", Marks: " + marks);
}
}
public class fifteen {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Student[] students = new Student[5]; // fixed size array
int count = 0;
while (count < [Link]) {
try {
[Link]("\nEnter Student Details:");
[Link]("Name: ");
String name = [Link]();
[Link]("Age: ");
int age = [Link]([Link]());
[Link]("Email: ");
String email = [Link]();
[Link]("Marks: ");
double marks = [Link]([Link]());
students[count] = new Student(name, age, email, marks);
count++;
[Link]("Admission Successful!");
} catch (Exception e) {
[Link]("Error: " + [Link]());
[Link]("Please re-enter details.\n");
}
}
[Link]("\n--- Admitted Students ---");
for (int i = 0; i < count; i++) {
students[i].display();
}
[Link]();
}
OUTPUT
C:\Users\VIVEK\OneDrive\Desktop>javac [Link]
C:\Users\VIVEK\OneDrive\Desktop>java fi een
Enter Student Details:
Name: Vivek Mainali
Age: 19
Email: Vm8111@[Link]
Marks: 98
Admission Successful!
Enter Student Details:
Name: Ansh
Age: 19
Email: sdsd
Marks: 87
Error: Invalid Email
Please re-enter details.
Enter Student Details:
Name: Riya
Age: 16
Email: riya@[Link]
Marks: 75
Error: Age must be 17–25
Please re-enter details.
Enter Student Details:
Name: Riya Sharma
Age: 20
Email: riya@[Link]
Marks: 55
Error: Marks < 60 → Not Eligible
Please re-enter details.
--- Admitted Students ---
Name: Vivek Mainali, Age: 19, Email: Vm8111@[Link], Marks: 98.0
NAME: VIVEK MAINALI
UNIVERSITY ROLL NO: 2025592
SECTION: B
QUES16 Develop a Java application that reads a text file ([Link]), processes each line by
performing string operations, and writes the results to an output file ([Link]).
Operations include: • Converting text to uppercase.
• Counting words and vowels (A, E, I, O, U).
• Replacing multiple spaces with single spaces
• Reversing each word (but keeping word order)
CODE:
import [Link].*;
import [Link].*;
public class SimpleTextProcessor {
public static void main(String[] args) {
int totalLines = 0;
int totalWord = 0;
int a = 0, e = 0, i = 0, o = 0, u = 0;
try {
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
BufferedWriter bw = new BufferedWriter(new FileWriter("[Link]"));
String line;
int lineNo = 1;
while ((line = [Link]()) != null) {
totalLines++;
line = [Link]().replaceAll("\\s+", " ");
String upper = [Link]();
String words[] = [Link](" ");
int wordCount = ([Link]()) ? 0 : [Link];
totalWords += wordCount;
for (char ch : [Link]()) {
if (ch == 'A') a++;
else if (ch == 'E') e++;
else if (ch == 'I') i++;
else if (ch == 'O') o++;
else if (ch == 'U') u++;
}
String reversedLine = "";
for (String w : words) {
String rev = "";
for (int j = [Link]() - 1; j >= 0; j--) {
rev += [Link](j);
}
reversedLine += rev + " ";
}
[Link]("Line " + lineNo + "\n");
[Link]("Uppercase: " + upper + "\n");
[Link]("Words: " + wordCount + "\n");
[Link]("Reversed: " + [Link]() + "\n");
[Link]("----------------------\n");
lineNo++;
}
[Link]();
[Link]();
} catch (Exception
e1) {
[Link]("Error: " + [Link]());
}
[Link]("Total Lines: " + totalLines);
[Link]("Total Words: " + totalWords);
[Link]("Vowels Count:");
[Link]("A: " + a);
[Link]("E: " + e);
[Link]("I: " + i);
[Link]("O: " + o);
[Link]("U: " + u);
}
}
OUTPUT
C:\Users\VIVEK\OneDrive\Desktop>javac [Link]
C:\Users\VIVEK\OneDrive\Desktop>java SimpleTextProcessor
Total Lines: 6
Total Words: 14
Vowels Count:
A: 8
E: 4
I: 5
O: 4
U: 1