LABTASK04
LABTASK04
package yum;
import java.io.*;
import java.util.Scanner;
while (true) {
System.out.println("\n=== File Management System ===");
System.out.println("1. Create File");
System.out.println("2. Write to File");
System.out.println("3. Read from File");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
String choice = scanner.nextLine();
try {
if (choice.equals("1")) {
System.out.print("Enter file name to create: ");
String fileName = scanner.nextLine();
createFile(fileName);
} else if (choice.equals("2")) {
System.out.print("Enter file name to write to: ");
String fileName = scanner.nextLine();
System.out.print("Enter content to write: ");
String content = scanner.nextLine();
writeFile(fileName, content);
} else if (choice.equals("3")) {
System.out.print("Enter file name to read: ");
String fileName = scanner.nextLine();
readFile(fileName);
} else if (choice.equals("4")) {
System.out.println("Exiting program. Goodbye!");
break;
} else {
System.out.println("Invalid choice. Try again.");
}
} catch (FileAlreadyExistsException | FileNotFoundException |
InvalidFileContentException e) {
System.out.println("Error: " + e.getMessage());
} catch (IOException e) {
System.out.println("I/O Error: " + e.getMessage());
}
}
scanner.close();
}
}
q2
package yum;
class MathOperations {
public void divide(int a, int b) {
try {
int result = a / b;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero.");
}
}
}
advanced.divide(20, 5);
advanced.divide(7, 0);
}
}
OUTPUT