Department of Computer Science
OBJECT ORIENTED PROGRAMMING USING JAVA
Program No: 3.1 Date: 17/09/2025
Name: Ashwin Sam Arun Reg No: 24117009
Program Title: File Handling & Text Processing
//Design a text analyzer program that reads content from a file, copies it to another
file, removes unwanted words,
//and supports word search and replacement while handling invalid inputs safely.
//Name : Ashwin Sam Arun
//Roll No.: 09
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
public class TextAnalyzer {
public static String readFile(String filePath) {
try {
return new String([Link]([Link](filePath)));
} catch (IOException e) {
[Link]("Error reading file: " + [Link]());
return "";
}
}
public static void writeFile(String filePath, String content) {
try {
[Link]([Link](filePath), [Link]());
[Link]("Content written to " + filePath);
} catch (IOException e) {
[Link]("Error writing file: " + [Link]());
}
}
public static String removeWords(String content, List<String> unwantedWords) {
for (String word : unwantedWords) {
content = [Link]("\\b" + [Link](word) + "\\b", "");
}
return [Link]("\\s+", " ").trim();
}
public static void searchWord(String content, String word) {
int count = 0;
String[] words = [Link]("\\s+");
for (String w : words) {
if ([Link](word)) {
count++;
}
}
[Link]("The word \"" + word + "\" appears " + count + " times.");
}
public static String replaceWord(String content, String oldWord, String newWord) {
// Case-insensitive replacement
return [Link]("(?i)\\b" + [Link](oldWord) + "\\b", newWord);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Name: Ashwin Sam Arun\nRoll No: 09\n");
try {
[Link]("Enter source file path: ");
String sourcePath = [Link]().trim();
if ()) {
[Link]("Source file does not exist. Exiting.");
return;
}
[Link]("Enter destination file path: ");
String destinationPath = [Link]().trim();
String content = readFile(sourcePath);
if ([Link]()) {
[Link]("No content to process. Exiting.");
return;
}
writeFile(destinationPath, content);
[Link]("Enter unwanted words separated by space: ");
String[] wordArray = [Link]().split("\\s+");
List<String> unwantedWords = [Link](wordArray);
String cleanedContent = removeWords(content, unwantedWords);
[Link]("\nContent after removing unwanted words: ");
[Link](cleanedContent);
[Link]("\nEnter a word to search: ");
String wordToSearch = [Link]();
searchWord(cleanedContent, wordToSearch);
[Link]("\nEnter word to replace: ");
String oldWord = [Link]();
[Link]("Enter new word: ");
String newWord = [Link]();
String replacedContent = replaceWord(cleanedContent, oldWord, newWord);
[Link]("\nContent after replacing:");
[Link](replacedContent);
writeFile(destinationPath, replacedContent);
} catch (Exception e) {
[Link]("An unexpected error occurred: " + [Link]());
} finally {
[Link]();
}
}
}
Output Screenshot/Text
Remarks Faculty Name and Signature with date
Department of Computer Science
OBJECT ORIENTED PROGRAMMING USING JAVA
Program No: 3.2 Date: 17/09/2025
Name: Ashwin Sam Arun Reg No: 24117009
Program Title: File Handling with Collections
Program :
//Create a file merging program that alternates lines from two text files while
merging. Implement word frequency analysis and word replacement, ensuring
efficient processing for large files using appropriate collections.
//Name : Ashwin Sam Arun
//Roll No.: 09
import [Link].*;
import [Link].*;
public class FileMergeAnalyzer02 {
public static void mergeFiles(String file1, String file2, String mergedFile) {
try (BufferedReader br1 = new BufferedReader(new FileReader(file1));
BufferedReader br2 = new BufferedReader(new FileReader(file2));
BufferedWriter bw = new BufferedWriter(new FileWriter(mergedFile))) {
String line1, line2;
while (true) {
line1 = [Link]();
line2 = [Link]();
if (line1 == null && line2 == null) break;
if (line1 != null) {
[Link](line1);
[Link]();
}
if (line2 != null) {
[Link](line2);
[Link]();
}
}
[Link]("Files merged into: " + mergedFile);
} catch (IOException e) {
[Link]("Error merging files: " + [Link]());
}
}
public static Map<String, Integer> wordFrequency(String filePath) {
Map<String, Integer> frequencyMap = new HashMap<>();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = [Link]()) != null) {
String[] words = [Link]().split("\\W+");
for (String word : words) {
if (![Link]()) {
[Link](word, [Link](word, 0) + 1);
}
}
}
} catch (IOException e) {
[Link]("Error reading file for word frequency: " + [Link]());
}
return frequencyMap;
}
public static void replaceWord(String sourceFile, String destinationFile, String
oldWord, String newWord) {
try (BufferedReader br = new BufferedReader(new FileReader(sourceFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(destinationFile))) {
String line;
while ((line = [Link]()) != null) {
line = [Link]("\\b" + oldWord + "\\b", newWord);
[Link](line);
[Link]();
}
[Link]("Word replacement complete. Updated file: " +
destinationFile);
} catch (IOException e) {
[Link]("Error replacing words: " + [Link]());
}
}
public static void main(String[] args) {
[Link]("Name: Ashwin Sam Arun\nRoll No: 09\n");
Scanner sc = new Scanner([Link]);
[Link]("Enter path for first file: ");
String file1 = [Link]();
[Link]("Enter path for second file: ");
String file2 = [Link]();
[Link]("Enter path for merged file: ");
String mergedFile = [Link]();
mergeFiles(file1, file2, mergedFile);
[Link]("\nWord Frequency Analysis:");
Map<String, Integer> freqMap = wordFrequency(mergedFile);
for ([Link]<String, Integer> entry : [Link]()) {
[Link]([Link]() + " → " + [Link]());
}
[Link]("\nEnter word to replace: ");
String oldWord = [Link]();
[Link]("Enter new word: ");
String newWord = [Link]();
String replacedFile = "replaced_output.txt";
replaceWord(mergedFile, replacedFile, oldWord, newWord);
[Link]();
}
}
Output Screenshot/Text
[Link]
[Link]
[Link]
replaced_output.txt
[Link]
Remarks Faculty Name and Signature with date
Department of Computer Science
OBJECT ORIENTED PROGRAMMING USING JAVA
Program No: 3.3 Date: 17/09/2025
Name: Ashwin Sam Arun Reg No: 24117009
Program Title: Collections – List Implementations
Program :
//Collections – List Implementations
Implement a shopping cart system where users can add, remove, and view
items. Choose between Vector, ArrayList, or LinkedList, and explain why one
collection is preferable.
//Name : Ashwin Sam Arun
//Roll No.: 09
import [Link].*;
class ShoppingCart {
private List<String> items;
public ShoppingCart() {
items = new ArrayList<>();
}
public void addItem(String item) {
[Link](item);
[Link](item + " added to cart. ");
}
public void removeItem(String item) {
if([Link](item)) {
[Link](item + " removed from cart. ");
} else {
[Link](item + " not found in cart. ");
}
}
public void viewCart() {
if([Link]()) {
[Link]("Your cart is Empty.");
} else {
[Link]("Items in your cart:");
for(String item : items) {
[Link]("- " + item);
}
}
}
}
public class ShoppingCartDemo3 {
public static void main(String[] args) {
[Link]("Name: Ashwin Sam Arun\nRoll No: 09\n");
Scanner sc = new Scanner([Link]);
ShoppingCart cart = new ShoppingCart();
int choice;
do {
[Link]("\n--- Shopping Cart Menu ---");
[Link]("1. Add Item");
[Link]("2. Remove Item");
[Link]("3. View Cart");
[Link]("4. Exit");
[Link]("Enter your Choice: ");
choice = [Link]();
[Link]();
switch(choice) {
case 1:
[Link]("Enter item to add: ");
String addItem = [Link]();
[Link](addItem);
break;
case 2:
[Link]("Enter item to remove: ");
String removeItem = [Link]();
[Link](removeItem);
break;
case 3:
[Link]();
break;
case 4:
[Link]("Exiting... Thank you!");
break;
default:
[Link]("Invalid Choice! Try again");
}
} while(choice != 4);
[Link]();
}
}
Output Screenshot/Text
Remarks Faculty Name and Signature with date
Department of Computer Science
OBJECT ORIENTED PROGRAMMING USING JAVA
Program No: 3.4 Date: 17/09/2025
Name: Ashwin Sam Arun Reg No: 24117009
Program Title: Collections – Queue Implementation
Program :
//Simulate a printer queue using a collection that processes jobs in arrival order and
supports job cancellation. Explain how the chosen collection structure supports this
behavior.
//Name : Ashwin Sam Arun
//Roll No.: 09
import [Link].*;
class PrinterQueue {
private Queue<String> queue;
public PrinterQueue() {
queue = new LinkedList<>();
}
public void addJob(String job) {
[Link](job);
[Link]("Job added: " + job);
}
public void processJob() {
String job = [Link]();
if(job != null) {
[Link]("Processing job: " + job);
} else {
[Link]("No jobs in the Queue.");
}
}
public void cancelJob(String job) {
if([Link](job)) {
[Link]("Cancelled job: " + job);
} else {
[Link]("Job not found in queue.");
}
}
public void viewQueue() {
if([Link]()) {
[Link]("Queue is Empty.");
} else {
[Link]("Current job in queue: " + queue);
}
}
}
public class PrinterQueueDemo4 {
public static void main(String[] args) {
[Link]("Name: Ashwin Sam Arun\nRoll No: 09\n");
Scanner sc = new Scanner([Link]);
PrinterQueue pq = new PrinterQueue();
int choice;
do {
[Link]("\n--- Printer Queue Menu ---");
[Link]("1. Add job");
[Link]("2. Process job");
[Link]("3. Cancel job");
[Link]("4. View Queue");
[Link]("5. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
[Link]();
switch(choice) {
case 1:
[Link]("Enter job name: ");
String job = [Link]();
[Link](job);
break;
case 2:
[Link]();
break;
case 3:
[Link]("Enter job name to cancel: ");
String cancelJob = [Link]();
[Link](cancelJob);
break;
case 4:
[Link]();
break;
case 5:
[Link]("Exiting...");
break;
default:
[Link]("Invalid choice! Try again.");
}
} while(choice != 5);
[Link]();
}
}
Output Screenshot/Text
Remarks Faculty Name and Signature with date
Department of Computer Science
OBJECT ORIENTED PROGRAMMING USING JAVA
Program No: 3.5 Date: 17/09/2025
Name: Ashwin Sam Arun Reg No: 24117009
Program Title: Collections & Sorting Utilities
Program :
//Write a program to sort student marks in ascending and descending order.
Demonstrate sorting techniques or collection utilities used in Java.
//Name : Ashwin Sam Arun
//Roll No.: 09
import [Link].*;
public class StudentMarksSorting {
public static void main(String[] args) {
[Link]("Name: Ashwin Sam Arun\nRoll No: 09\n");
Scanner sc = new Scanner([Link]);
[Link]("Enter number of students: ");
int n = [Link]();
List<Integer> marks = new ArrayList<>();
[Link]("Enter marks of students:");
for (int i = 0; i < n; i++) {
[Link]([Link]());
}
[Link](marks);
[Link]("Marks in Ascending Order: " + marks);
[Link](marks, [Link]());
[Link]("Marks in Descending Order: " + marks);
[Link]([Link]());
[Link]("Ascending (using [Link]()): " + marks);
[Link]([Link]());
[Link]("Descending (using [Link]()): " + marks);
[Link]();
}
}
Output Screenshot/Text
Remarks Faculty Name and Signature with date
Department of Computer Science
OBJECT ORIENTED PROGRAMMING USING JAVA
Program No: 3.6 Date: 17/09/2025
Name: Ashwin Sam Arun Reg No: 24117009
Program Title: Interfaces & Polymorphism
Program :
//Design an online payment system with multiple methods like Credit Card, UPI, and
PayPal. Create a common interface and explain how this design improves scalability
and maintainability.
//Name : Ashwin Sam Arun
//Roll No.: 09
import [Link];
interface Payment {
void pay(double amount);
}
class CreditCardPayment implements Payment {
private String cardNumber;
public CreditCardPayment(String cardNumber) {
[Link] = cardNumber;
}
@Override
public void pay(double amount) {
[Link]("Paid " + amount + " using Credit Card: " + cardNumber);
}
}
class UpiPayment implements Payment {
private String upiId;
public UpiPayment(String upiId) {
[Link] = upiId;
}
@Override
public void pay(double amount) {
[Link]("Paid " + amount + " using UPI ID: " + upiId);
}
}
class PayPalPayment implements Payment {
private String email;
public PayPalPayment(String email) {
[Link] = email;
}
@Override
public void pay(double amount) {
[Link]("Paid " + amount + " using PayPal account: " + email);
}
}
public class OnlinePaymentSystem {
public static void main(String[] args) {
[Link]("Name: Ashwin Sam Arun\nRoll No: 09\n");
Scanner sc = new Scanner([Link]);
int choice;
do {
[Link]("=== Online Payment System ===");
[Link]("1. Credit Card");
[Link]("2. UPI");
[Link]("3. PayPal");
[Link]("4. Exit");
[Link]("Enter choice (1-4): ");
choice = [Link]();
[Link]();
if (choice == 4) {
[Link]("Exiting... Thank you!");
break;
}
[Link]("Enter amount to pay: ");
double amount = [Link]();
[Link]();
Payment payment = null;
switch (choice) {
case 1:
[Link]("Enter Credit Card Number: ");
String card = [Link]();
payment = new CreditCardPayment(card);
break;
case 2:
[Link]("Enter UPI ID: ");
String upi = [Link]();
payment = new UpiPayment(upi);
break;
case 3:
[Link]("Enter PayPal Email: ");
String email = [Link]();
payment = new PayPalPayment(email);
break;
default:
[Link]("Invalid choice! Try again.");
continue;
}
if (payment != null) {
[Link](amount);
}
} while (choice != 4);
[Link]();
}
}
Output Screenshot/Text
Remarks Faculty Name and Signature with date
Department of Computer Science
OBJECT ORIENTED PROGRAMMING USING JAVA
Program No: 3.8 Date: 17/09/2025
Name: Ashwin Sam Arun Reg No: 24117009
Program Title: User-Defined Exceptions
Program :
//Implement a validation mechanism where student ages must be between 15 and 21.
Throw and handle a user-defined exception for invalid ages instead of using built-in
exceptions.
//Name : Ashwin Sam Arun
//Roll No.: 09
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) throws InvalidAgeException {
if (age < 15 || age > 21) {
throw new InvalidAgeException("Invalid Age! Age must be between 15 and
21.");
}
[Link] = name;
[Link] = age;
}
public void display() {
[Link]("Student Name: " + name + ", Age: " + age);
}
}
public class AgeValidation {
public static void main(String[] args) {
[Link]("=== User-Defined Exception: Student Age Validation ===\n");
try {
Student s1 = new Student("Ashwin", 19);
[Link]();
Student s2 = new Student("Sahil", 18);
[Link]();
} catch (InvalidAgeException e) {
[Link]("Exception Caught: " + [Link]());
}
[Link]("\nProgram continues after exception handling...");
}
}
Output Screenshot/Text
Remarks Faculty Name and Signature with date
Department of Computer Science
OBJECT ORIENTED PROGRAMMING USING JAVA
Program No: 3.7 Date: 17/09/2025
Name: Ashwin Sam Arun Reg No: 24117009
Program Title: Packages & Code Organization
Program :
//Structure an e-learning platform into packages for students, courses, exams, and
results. Explain how the chosen package structure enhances organization and code
clarity.
//Name : Ashwin Sam Arun
//Roll No.: 09
[Link]
package students;
public class Student {
private String id;
private String name;
private String enrolledCourse;
public Student(String id, String name) {
[Link] = id;
[Link] = name;
}
public void enrollCourse(String course) {
[Link] = course;
[Link](name + " enrolled in " + course);
}
public String getDetails() {
return "Student ID: " + id + ", Name: " + name + ", Course: " + enrolledCourse;
}
}
[Link]
package exams;
public class Exam {
private String examName;
private String courseName;
public Exam(String examName, String courseName) {
[Link] = examName;
[Link] = courseName;
}
public void scheduleExam() {
[Link]("Exam '" + examName + "' scheduled for course: " +
courseName);
}
}
[Link]
package courses;
public class Course {
private String courseId;
private String courseName;
public Course(String courseId, String courseName) {
[Link] = courseId;
[Link] = courseName;
}
public void showCourse() {
[Link]("Course ID: " + courseId + ", Name: " + courseName);
}
public String getCourseName() {
return courseName;
}
}
[Link]
package results;
public class Result {
private String studentId;
private String courseName;
private int marks;
public Result(String studentId, String courseName, int marks) {
[Link] = studentId;
[Link] = courseName;
[Link] = marks;
}
public void showResult() {
[Link]("Result -> Student ID: " + studentId + ", Course: " +
courseName + ", Marks: " + marks);
}
}
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainApp {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Student ID: ");
String studentId = [Link]();
[Link]("Enter Student Name: ");
String studentName = [Link]();
Student s1 = new Student(studentId, studentName);
[Link]("Enter Course ID: ");
String courseId = [Link]();
[Link]("Enter Course Name: ");
String courseName = [Link]();
Course c1 = new Course(courseId, courseName);
[Link]([Link]());
[Link]("Enter Exam Name: ");
String examName = [Link]();
Exam e1 = new Exam(examName, [Link]());
[Link]();
[Link]("Enter marks obtained by student: ");
int marks = [Link]();
Result r1 = new Result(studentId, [Link](), marks);
[Link]();
[Link]("\n--- Student Summary ---");
[Link]([Link]());
[Link]();
}
}
Output Screenshot/Text
Enter Student ID: BCA2409
Enter Student Name: Ashwin Sam Arun
Enter Course ID: cs01
Enter Course Name: BCA
Ashwin Sam Arun enrolled in BCA
Enter Exam Name: Java
Exam 'Java' scheduled for course: BCA
Enter marks obtained by student: 80
Result -> Student ID: BCA2409, Course: BCA, Marks: 80
--- Student Summary ---
Student ID: BCA2409, Name: Ashwin Sam Arun, Course: BCA
Remarks Faculty Name and Signature with date
Department of Computer Science
OBJECT ORIENTED PROGRAMMING USING JAVA
Program No: 3.9 Date: 17/09/2025
Name: Ashwin Sam Arun Reg No: 24117009
Program Title: Exception Handling in Banking
Program:
//Write a Java program to demonstrate different exception handling techniques:
a. Banking Example: Handle a withdrawal request where the withdrawal amount
is greater than the balance. Throw and catch a custom exception and display the
correct balance.
b. Division Example: Divide two numbers and handle division by zero using try-
catch-finally, ensuring the finally block always executes.
c. Age Validation Example: Create a method that throws an exception if a
student’s age is less than 18. Handle the exception in the main method using throw
and throws.
//Name : Ashwin Sam Arun
//Roll No.: 09
import [Link];
class InsufficientBalanceException extends Exception {
public InsufficientBalanceException(String message) {
super(message);
}
}
class BankAccount {
private double balance;
public BankAccount(double balance) {
[Link] = balance;
}
public void withdraw(double amount) throws InsufficientBalanceException {
if (amount > balance) {
throw new InsufficientBalanceException("Withdrawal failed! Amount exceeds
balance.");
}
balance -= amount;
[Link]("Withdrawal successful. New Balance: " + balance);
}
public double getBalance() {
return balance;
}
}
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age must be 18 or above to register.");
}
[Link] = name;
[Link] = age;
}
public void showDetails() {
[Link]("Student Name: " + name + ", Age: " + age);
}
}
public class ExceptionHandling{
public static void main(String[] args) {
[Link]("Name: Ashwin Sam Arun\nRoll No: 09\n");
Scanner sc = new Scanner([Link]);
[Link]("\n--- Banking Example ---");
BankAccount account = new BankAccount(5000);
[Link]("Enter amount to withdraw: ");
double amount = [Link]();
try {
[Link](amount);
} catch (InsufficientBalanceException e) {
[Link]("Error: " + [Link]());
[Link]("Available Balance: " + [Link]());
}
[Link]("\n--- Division Example ---");
try {
[Link]("Enter numerator: ");
int num1 = [Link]();
[Link]("Enter denominator: ");
int num2 = [Link]();
int result = num1 / num2;
[Link]("Result: " + result);
} catch (ArithmeticException e) {
[Link]("Error: Cannot divide by zero.");
} finally {
[Link]("Division operation completed (finally block executed).");
}
[Link]("\n--- Age Validation Example ---");
[Link]();
[Link]("Enter student name: ");
String name = [Link]();
[Link]("Enter student age: ");
int age = [Link]();
try {
Student s1 = new Student(name, age);
[Link]();
} catch (InvalidAgeException e) {
[Link]("Error: " + [Link]());
}
[Link]();
}
}
Output Screenshot/Text
Remarks Faculty Name and Signature with date
Department of Computer Science
OBJECT ORIENTED PROGRAMMING USING JAVA
Program No: 3.10 Date: 17/09/2025
Name: Ashwin Sam Arun Reg No: 24117009
Program Title: String Operations in Java – Concatenation, Replacement, and Character
Frequency Analysis
Program :
//Write a Java program to perform the following String operations:
a. Concatenate two strings using StringBuilder.
Example: Input → "Hello" and "World" → Output → "HelloWorld".
b. Replace a substring inside a string using replace().
Example: "I like C" → Replace "C" with "Java" → "I like Java".
c. Count the frequency of each character in a string.
Example: Input → "apple" → Output → a=1, p=2, l=1, e=1.
//Name : Ashwin Sam Arun
//Roll No.: 09
import [Link];
import [Link];
public class StringOperations {
public static void main(String[] args) {
[Link]("Name: Ashwin Sam Arun\nRoll No: 09\n");
Scanner sc = new Scanner([Link]);
[Link]("\n--- String Concatenation ---");
[Link]("Enter first string: ");
String str1 = [Link]();
[Link]("Enter second string: ");
String str2 = [Link]();
StringBuilder sb = new StringBuilder(str1);
[Link](str2);
[Link]("Concatenated String: " + [Link]());
[Link]("\n--- String Replacement ---");
[Link]("Enter a string: ");
String mainStr = [Link]();
[Link]("Enter substring to replace: ");
String oldSub = [Link]();
[Link]("Enter new substring: ");
String newSub = [Link]();
String replacedStr = [Link](oldSub, newSub);
[Link]("Replaced String: " + replacedStr);
[Link]("\n--- Character Frequency ---");
[Link]("Enter a string: ");
String freqStr = [Link]();
HashMap<Character, Integer> freqMap = new HashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
[Link]("Character frequencies:");
for (char c : [Link]()) {
[Link](c + " = " + [Link](c));
}
[Link]();
}
}
Output Screenshot/Text
Remarks Faculty Name and Signature with date