1. Write a Java program to perform the following operations.
a. Find the string index.
b. Compare two strings.
c. Retrieve the single character formatting.
d. Find the substring of the given string.
e. Split the string.
Program
import [Link];
public class StringOperationsDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Input strings
[Link]("Enter first string: ");
String str1 = [Link]();
[Link]("Enter second string: ");
String str2 = [Link]();
// a. Find the string index
[Link]("Enter a character to find index in first string: ");
char ch = [Link]().charAt(0);
int index = [Link](ch);
[Link]("Index of '" + ch + "' in first string: " + index);
// b. Compare two strings
if ([Link](str2)) {
[Link]("Both strings are equal.");
} else {
[Link]("Strings are not equal.");
}
// c. Retrieve single character
[Link]("Enter position to retrieve character from first string: ");
int pos = [Link]();
if (pos >= 0 && pos < [Link]()) {
char character = [Link](pos);
[Link]("Character at position " + pos + " is: " + character);
} else {
[Link]("Invalid position!");
}
[Link](); // clear buffer
// d. Find substring
[Link]("Enter start index for substring: ");
int start = [Link]();
[Link]("Enter end index for substring: ");
int end = [Link]();
if (start >= 0 && end <= [Link]() && start < end) {
String sub = [Link](start, end);
[Link]("Substring: " + sub);
} else {
[Link]("Invalid substring indexes!");
}
[Link](); // clear buffer
// e. Split the string
[Link]("Enter a sentence to split: ");
String sentence = [Link]();
String[] words = [Link](" ");
[Link]("After splitting:");
for (String word : words) {
[Link](word);
}
[Link]();
}
}
Sample Input & Output
Enter first string: Hello CSBS
Enter second string: Hello CSBS Family
Enter a character to find index in first string: o
Index of 'o' in first string: 4
Strings are not equal.
Enter position to retrieve character from first string: 8
Character at position 8 is: B
Enter start index for substring: 3
Enter end index for substring: 7
Substring: lo C
Enter a sentence to split: welcome to Sairam Engineering College
After splitting:
welcome
to
Sairam
Engineering
College
...Program finished with exit code 0
Press ENTER to exit console.
2. Design a Java interface for ADT Stack. Implement this interface using an array. Provide
necessary exception handling in both the implementations.
Program
import [Link];
// Stack Interface
interface StackADT {
void push(int element) throws StackOverflowException;
int pop() throws StackUnderflowException;
int peek() throws StackUnderflowException;
boolean isEmpty();
boolean isFull();
}
// Custom Exception for Overflow
class StackOverflowException extends Exception {
public StackOverflowException(String message) {
super(message);
}
}
// Custom Exception for Underflow
class StackUnderflowException extends Exception {
public StackUnderflowException(String message) {
super(message);
}
}
// Array Implementation of Stack
class ArrayStack implements StackADT {
private int[] stack;
private int top;
private int capacity;
public ArrayStack(int size) {
capacity = size;
stack = new int[capacity];
top = -1;
}
public void push(int element) throws StackOverflowException {
if (isFull()) {
throw new StackOverflowException("Stack Overflow! Cannot push element.");
}
stack[++top] = element;
}
public int pop() throws StackUnderflowException {
if (isEmpty()) {
throw new StackUnderflowException("Stack Underflow! Cannot pop element.");
}
return stack[top--];
}
public int peek() throws StackUnderflowException {
if (isEmpty()) {
throw new StackUnderflowException("Stack is Empty!");
}
return stack[top];
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == capacity - 1;
}
}
// Main Class (Menu Driven)
public class StackMenuUserInput {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the size of the stack: ");
int size = [Link]();
ArrayStack stack = new ArrayStack(size);
int choice;
do {
[Link]("\n===== STACK MENU =====");
[Link]("1. Push");
[Link]("2. Pop");
[Link]("3. Peek");
[Link]("4. Check if Empty");
[Link]("5. Check if Full");
[Link]("6. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
try {
switch (choice) {
case 1:
[Link]("Enter element to push: ");
int element = [Link]();
[Link](element);
[Link]("Element pushed successfully.");
break;
case 2:
int popped = [Link]();
[Link]("Popped element: " + popped);
break;
case 3:
int topElement = [Link]();
[Link]("Top element: " + topElement);
break;
case 4:
if ([Link]())
[Link]("Stack is Empty.");
else
[Link]("Stack is NOT Empty.");
break;
case 5:
if ([Link]())
[Link]("Stack is Full.");
else
[Link]("Stack is NOT Full.");
break;
case 6:
[Link]("Exiting program...");
break;
default:
[Link]("Invalid choice! Please try again.");
}
} catch (StackOverflowException | StackUnderflowException e) {
[Link]("Exception: " + [Link]());
}
} while (choice != 6);
[Link]();
}
}
Sample Input & Output
Enter the size of the stack: 5
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 1
Enter element to push: 56
Element pushed successfully.
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 1
Enter element to push: 57
Element pushed successfully.
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 1
Enter element to push: 87
Element pushed successfully.
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 190
Invalid choice! Please try again.
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 1
Enter element to push: 45
Element pushed successfully.
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 1
Enter element to push: 34
Element pushed successfully.
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 3
Top element: 34
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 4
Stack is NOT Empty.
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 2
Popped element: 34
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 2
Popped element: 45
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 2
Popped element: 87
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 2
Popped element: 57
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 4
Stack is NOT Empty.
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 2
Popped element: 56
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 2
Exception: Stack Underflow! Cannot pop element.
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 4
Stack is Empty.
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 3
Exception: Stack is Empty!
===== STACK MENU =====
1. Push
2. Pop
3. Peek
4. Check if Empty
5. Check if Full
6. Exit
Enter your choice: 6
Exiting program...
...Program finished with exit code 0
Press ENTER to exit console.
3. Write a Java Program that opens a file using File Reader and handles File Not
Found Exception and IO Exception.
Program
import [Link];
import [Link];
import [Link];
import [Link];
public class FileReadUserInput {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
FileReader fr = null;
try {
// Get file name from user
[Link]("Enter the file name to open: ");
String fileName = [Link]();
// Open the file
fr = new FileReader(fileName);
int data;
[Link]("\nFile contents:");
// Read file character by character
while ((data = [Link]()) != -1) {
[Link]((char) data);
}
} catch (FileNotFoundException e) {
[Link]("Error: File not found.");
} catch (IOException e) {
[Link]("Error: Problem while reading the file.");
} finally {
// Close FileReader
try {
if (fr != null) {
[Link]();
}
[Link]();
} catch (IOException e) {
[Link]("Error while closing the file.");
}
}
}
}
Sample Input & Output
Enter the file name to open: sam
Error: File not found.
...Program finished with exit code 0
Press ENTER to exit console.
Enter the file name to open: [Link]
File contents:
welcome to CSBS department
hi everyone
...Program finished with exit code 0
Press ENTER to exit console