0% found this document useful (0 votes)
3 views4 pages

LABTASK04

The document contains two Java programs for file management and mathematical operations. The first program allows users to create, write to, and read from files while handling exceptions for file operations. The second program demonstrates basic and advanced division operations, including error handling for division by zero.

Uploaded by

alliyafatima78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

LABTASK04

The document contains two Java programs for file management and mathematical operations. The first program allows users to create, write to, and read from files while handling exceptions for file operations. The second program demonstrates basic and advanced division operations, including error handling for division by zero.

Uploaded by

alliyafatima78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Q1

package yum;
import java.io.*;
import java.util.Scanner;

class FileAlreadyExistsException extends Exception {


public FileAlreadyExistsException(String message) {
super(message);
}
}

class InvalidFileContentException extends Exception {


public InvalidFileContentException(String message) {
super(message);
}
}

public class Main {

public static void createFile(String fileName) throws


FileAlreadyExistsException, IOException {
File file = new File(fileName);
if (file.exists()) throw new FileAlreadyExistsException("File already
exists.");
file.createNewFile();
System.out.println("File created successfully.");
}

public static void writeFile(String fileName, String content) throws


FileNotFoundException, InvalidFileContentException {
File file = new File(fileName);
if (!file.exists()) throw new FileNotFoundException("File not found.");
if (content == null || content.trim().isEmpty()) throw new
InvalidFileContentException("Content cannot be empty.");

try (FileWriter writer = new FileWriter(file, true)) {


writer.write(content + System.lineSeparator());
} catch (IOException e) {
System.out.println("Failed to write to file: " + e.getMessage());
}
System.out.println("Content written to file.");
}

public static void readFile(String fileName) throws


FileNotFoundException {
File file = new File(fileName);
if (!file.exists()) throw new FileNotFoundException("File not found.");
try (BufferedReader reader = new BufferedReader(new
FileReader(file))) {
System.out.println("File Content:");
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Failed to read file: " + e.getMessage());
}
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

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.");
}
}
}

class AdvancedMathOperations extends MathOperations {


@Override
public void divide(int a, int b) {
System.out.println("Advanced Division in progress...");
super.divide(a, b);
}
}
public class mainn {
public static void main(String[] args) {
MathOperations base = new MathOperations();
AdvancedMathOperations advanced = new
AdvancedMathOperations();
base.divide(10, 2);
base.divide(10, 0);

advanced.divide(20, 5);
advanced.divide(7, 0);
}
}

OUTPUT

You might also like