You are on page 1of 3

Problem A

Objectives:
To develop applications with exception handling (§12.4.5).
To define custom exception classes (§12.9).

Task: Create a grading system for a school. The system should accept the scores of
students and calculate their grades based on predefined criteria. The application should
also handle exceptions such as invalid inputs and out-of-range scores:
1. Create a class called "GradingSystem" with the following methods:
○ A method named "calculateGrade" that accepts a student's score as a
parameter and returns the corresponding grade based on the following
criteria:
■ Score >= 90: Grade A
■ Score >= 80 and < 90: Grade B
■ Score >= 70 and < 80: Grade C
■ Score >= 60 and < 70: Grade D
■ Score < 60: Grade F
■ If the score is negative or greater than 100, throw a custom
exception named "InvalidScoreException" with an appropriate error
message.
2. Create a custom exception class named "InvalidScoreException" that extends
the Exception class. The exception should have a constructor that accepts an
error message and passes it to the superclass constructor.
3. In the main method of the "GradingSystem" class, prompt the user to enter a
score. Use exception handling to catch any invalid inputs (e.g., non-numeric
values) and display an appropriate error message.
4. Test your application by entering different scores and verifying that the correct
grade is calculated and displayed.

Example Output:

Enter the student's score: 85 Grade: B

Enter the student's score: 62 Grade: D

Enter the student's score: abcd Error: Invalid input. Please enter a numeric score.

Enter the student's score: 105 Error: Invalid score. Score should be between 0 and 100.
Problem B
Objectives:
To discover file/directory properties, to delete and rename files/ directories, and to create directories
using the File class (§12.10).

Task: Create a file and directory management application. It should allow the user to
perform various operations on files and directories, such as discovering properties,
deleting files/directories, renaming files/directories, and creating directories using the
File class.
1. Create a class called "FileManager" with the following methods: a. A method
named "getFileProperties" that accepts a file name/path as a parameter and
displays the following properties of the file:
○ File name
○ Absolute path
○ File size (in bytes)
○ Last modified date
2. b. A method named "deleteFile" that accepts a file name/path as a parameter
and deletes the file. Handle any exceptions that may occur.
c. A method named "renameFile" that accepts the current file name/path and the
new file name/path as parameters and renames the file. Handle any exceptions
that may occur.
d. A method named "createDirectory" that accepts a directory name/path as a
parameter and creates a new directory. Handle any exceptions that may occur.
3. In the main method of the "FileManager" class, prompt the user to choose an
operation to perform (e.g., discover properties, delete file, rename file, create
directory).
4. Depending on the user's choice, prompt the user to provide the necessary inputs
(e.g., file name/path, new file name/path, directory name/path) and call the
respective methods to perform the operation.
5. Test your application by performing different operations on files and directories.
Verify that the operations are performed successfully.

Example Output:

Welcome to the File Manager! Choose an operation to perform:


1. Discover file properties
2. Delete file
3. Rename file
4. Create directory
5. Exit Enter your choice: 1
Enter the file name/path: C:\Users\John\example.txt
File Properties: File Name: example.txt Absolute Path: C:\Users\John\example.txt File Size: 1024 bytes Last Modified: Tue, 25 Oct 2022 14:30:00 GMT
Choose an operation to perform:
1. Discover file properties
2. Delete file
3. Rename file
4. Create directory
5. Exit Enter your choice: 2
Enter the file name/path: C:\Users\John\example.txt
File deleted successfully.
Choose an operation to perform:
1. Discover file properties
2. Delete file
3. Rename file
4. Create directory
5. Exit Enter your choice: 3
Enter the current file name/path: C:\Users\John\old.txt Enter the new file name/path: C:\Users\John\new.txt
File renamed successfully.
Choose an operation to perform:
1. Discover file properties
2. Delete file
3. Rename file
4. Create directory
5. Exit Enter your choice: 4
Enter the directory name/path: C:\Users\John\new_directory
Directory created successfully.
Choose an operation to perform:
1. Discover file properties
2. Delete file
3. Rename file
4. Create directory
5. Exit Enter your choice: 5
Exiting...

You might also like