External Practical_BCSC0186 Java Programming Lab Even Sem. 2024-25
External Practical_BCSC0186 Java Programming Lab Even Sem. 2024-25
Q1: Product Price Analysis: A store wants to analyze the prices of `n` products. Q1. What is the difference between == and .equals() in Java? Explain with an example
Write a Java program that: involving strings.
- Takes `n` as input (number of products).
- Uses an array to store the prices of `n` products. Q2. Multilevel Inheritance – Person → Student → GraduateStudent
- Calculates and displays the following: The average price. Create a base class Person with:
Example Input: Variable: name
Number of products: 8 Method: displayName()
Enter prices: 120 150 200 180 250 300 100 220 Extend Person in a class Student with:
Example Output: Variable: rollNo
Average Price: 190.0 Method: displayRollNo()
Extend Student in a class GraduateStudent with:
Q2: Working with Packages in Java Variable: degree
Create a package named bankPackage that contains two classes: Method: displayDegree()
1. Account: Methods: deposit(double amount),
withdraw(double amount),displayBalance() Task:
2. Loan: Methods: calculateInterest(double principal, In a main class TestPerson, create an object of GraduateStudent, assign values, and call all
double rate, int years),calculateEMI(double three display methods.
principal, double rate, int years)
Now, create another class called TestBank (outside the package):
Import the package.
Create objects of Account and Loan.
Call the methods using test values.
Display the account balance and loan details.
Q2. Nested Try-Catch Blocks Q2. Exception Hierarchy and Catch Execution
Analyze the following code and explain the output for each of the given scenarios: Analyze the following code and explain the output for each of the given scenarios:
try { try {
System.out.println("Outer Try"); System.out.println("Start of Try");
try { Statement-A;
System.out.println("Inner Try"); Statement-B;
Statement-1; Statement-C;
} catch (ArithmeticException e) { } catch (NullPointerException e) {
System.out.println("Inner Catch: AE"); System.out.println("Caught NPE");
} } catch (RuntimeException e) {
} catch (Exception e) { System.out.println("Caught RuntimeException");
System.out.println("Outer Catch: Exception"); } catch (Exception e) {
} finally { System.out.println("Caught General Exception");
System.out.println("Outer Finally"); } finally {
} System.out.println("Finally Block Executed");
System.out.println("End"); }
Scenarios: System.out.println("End of Code");
(i) If Statement-1 throws ArithmeticException, which catch block handles it? Scenarios:
(iii) What happens if there’s no exception? (i) If Statement-B throws a NullPointerException, which catch block will be executed?
(ii) If Statement-C throws an IllegalArgumentException, what will be the output?