This assignment demonstrates the use of custom exception handling in Java by applying it to a practical, real-world example: Heisenberg's Uncertainty Principle. The program allows users to calculate either the uncertainty in position or the uncertainty in momentum based on user input and the principle's formula. Custom exceptions are used to handle invalid inputs for these uncertainties.
The principle states:
ΔX × ΔP ≥ h / (4π)
Where:
- ΔX = Uncertainty in position
- ΔP = Uncertainty in momentum
- h = Planck's constant (6.62607015 × 10⁻³⁴ J·s)
-
Custom Exception Handling:
InvalidUncertaintyOfMomentumExceptionfor invalid momentum values.InvalidUncertaintyOfPositionExceptionfor invalid position values.
-
User-Friendly Input:
- Choose to calculate either ΔX or ΔP.
- Input validation ensures correctness.
-
Precise Calculation:
- Uses Java's
Math.PIfor accuracy. - Results displayed in scientific notation.
- Uses Java's
-
InvalidUncertaintyOfMomentumException:
Thrown when the uncertainty of momentum (ΔP) is invalid (≤ 0). -
InvalidUncertaintyOfPositionException:
Thrown when the uncertainty of position (ΔX) is invalid (≤ 0).
-
UncertaintyInPosition(double delta_P):
Calculates ΔX when given ΔP. ThrowsInvalidUncertaintyOfMomentumExceptionfor invalid ΔP. -
UncertaintyInMomentum(double delta_x):
Calculates ΔP when given ΔX. ThrowsInvalidUncertaintyOfPositionExceptionfor invalid ΔX.
-
Compile the program:
javac Assignment2_2222756042.java
-
Run the program:
java Assignment2_2222756042
-
Follow the on-screen prompts to calculate:
- Uncertainty in position (ΔX)
- Uncertainty in momentum (ΔP)
What do you want to calculate?
1. Uncertainty Of Position(delta_X)
2. Uncertainty Of Momentum(delta_P)
Enter input: 1
Enter uncertainty of momentum(delta_P) in kgm/s: -5
Here is an Exception: InvalidUncertaintyOfMomentumException: -5.0
What do you want to calculate?
1. Uncertainty Of Position(delta_X)
2. Uncertainty Of Momentum(delta_P)
Enter input: 2
Enter uncertainty of position(delta_X) in kgm/s: 2.637E-24
Uncertainty in momentum (delta_P) is: 1.999567344797414E-11 kgm/s
.
├── Assignment2_2222756042.java
└── README.md
- Creating custom exceptions by extending the
Exceptionclass. - Utilizing constructors and the
toStringmethod for meaningful exception messages. - Practical application of exception handling in scientific calculations.
- Java Development Kit (JDK 8 or higher)
- Add support for additional error handling (e.g., non-numeric input).
- Extend the program to handle multiple scientific formulas.
This assignment is licensed under the MIT License. Feel free to use and modify.
- ID: 2222756042
- Course: CSE215 || Faculty: CFA || Spring'23