Final Exam Part 2- Programming
Final Exam Part 2 - Programming
Welcome to the Final Exam, Part 2!
Please carefully read the instructions below and ensure that you submit your project on time.
Question Types:
There will be 5 programming questions, covering the following topics:
Exception Handling
GUI - JavaFX
Submission Deadline:
This part is due on Saturday, May 14th, by 11:59 PM.
Submission Format:
All code and explanations should be documented in a Word document. Each question should
have its own section, including the code and screenshots of your program's output.
Screenshot Requirements:
Be sure to include screenshots of the output for each program in your document. This will aid in
better understanding and evaluation.
Important Notes:
Plagiarism will not be tolerated. Ensure your work is original.
We wish you the best of luck with your programming exam! If you have any questions or need
clarification on any aspect of the project, feel free to reach out to me.
Happy coding!
Sincerely,
Xiaojin Ye
Question 1: Write a Java program that:
1. Accepts two integers from the user: a numerator and a denominator.
2. Divides the numerator by the denominator and prints the result.
3. Handles the following exceptions:
o Division by zero (ArithmeticException).
o Invalid input format (NumberFormatException).
Requirements
1. Use a try-catch block to handle exceptions gracefully.
2. Catch ArithmeticException and print: "Error: Division by zero is
not allowed."
3. Catch NumberFormatException and print: "Error: Please enter valid
integers."
4. Include a finally block to display "Program completed."
Example Outputs
Example 1:
Input:
Enter numerator: 10
Enter denominator: 2
Output:
Result: 5
Program completed.
Example 2:
Input:
Enter numerator: 10
Enter denominator: 0
Output:
Error: Division by zero is not allowed.
Program completed.
Example 3:
Input:
Enter numerator: ten
Enter denominator: two
Output:
Error: Please enter valid integers.
Program completed.
Your Task: Write and test the Java program to meet the above requirements.
Code template: (You can edit the code for your answer)
import [Link];
public class BasicExceptionHandlingLab {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
try {
[Link]("Enter numerator: ");
int numerator = [Link]([Link]());
[Link]("Enter denominator: ");
int denominator = [Link]([Link]());
// Write your code here to perform division and print the result
} catch (ArithmeticException e) {
// Handle division by zero
// Write your code here to handle ArithmeticException
} catch (NumberFormatException e) {
// Handle invalid number format
// Write your code here to handle NumberFormatException
} finally {
// Write your Final message
}
Question 2: Write a Java program to demonstrate the use of multiple catch blocks for handling
exceptions.
1. Create an integer array A with 6 elements.
2. In the try block, do the following:
o Divide the first element of the array (A[0]) by the third element (A[2]), and
print the result.
o Attempt to access the seventh element of the array (A[7]), which will throw an
exception.
3. Use the following catch blocks to handle exceptions:
o ArithmeticException: Catch division by zero errors and print the message:
"Denominator cannot be zero."
o ArrayIndexOutOfBoundsException: Catch invalid array index errors and
print the message:
"Index out of bounds."
4. Use a finally block to display the message:
"End of program."
Requirements
Only one exception will occur during execution:
either ArithmeticException or ArrayIndexOutOfBoundsException,
depending on the input values.
The program must demonstrate the concept of handling multiple exceptions gracefully.
Example Outputs
Example 1:
Array: {30, 20, 10, 40, 0, 50}
Output:
Division is 3
Index out of bounds.
End of program.
Example 2:
Array: {30, 20, 0, 40, 0, 50}
Output:
Denominator cannot be zero.
End of program.
Your Task:
Write two Java programs to produce the two different outputs.
Code Template:
public class MultipleCatch {
public static void main(String[] args) {
// Define an array with 6 elements
try {
// Perform division and access array elements here
} catch (ArithmeticException e) {
// Handle division by zero exception
} catch (ArrayIndexOutOfBoundsException e) {
// Handle invalid index exception
} finally {
// Print final message
}
}
}
Question 3: Write a JavaFX application that meets the following requirements:
1. A label with the text "This is CSC211 class."
2. A button with the text "This is our class Button."
3. Place the Label and Button inside a VBox layout with a spacing of 10 pixels between
elements.
4. Align the contents of the VBox to the center of the scene.
5. Create a Scene that is 300 pixels wide and 100 pixels high, and set the VBox as the root
node of the scene.
6. The title of the stage is your full name
The output should be like below:
Question 4: Create a JavaFX application that displays a Border Pane to demonstrate the
following:
Question 5: (Study the Lecture note of GUI page 86 to 91 to do the following question)
Create a Java GUI application using JavaFX to demonstrate the following:
Hint: Using the formula: inches = meters * 39.37