An Object-Oriented Java library designed to represent and perform arithmetic operations on fractions. This project ensures data integrity through strict validation and automatic mathematical simplification.
- Automatic Simplification: Every fraction is automatically reduced to its simplest form using the Greatest Common Divisor (GCD) algorithm during construction.
- Robust Validation: Enforces rules to prevent division by zero and handles negative denominators by terminating the program with clear error messages.
- Constructor Chaining: Demonstrates advanced OOP patterns using
this(1, 1)to link constructors for consistent initialization. - Arithmetic Suite: Full support for:
Add(Fraction f2)Subtract(Fraction f2)Multiply(Fraction f2)Divide(Fraction f2)
The reduce() method is marked as private final to protect the internal logic. It utilizes a custom GCD function:
Implemented to allow safe duplication of objects without affecting the original instance, a core concept in effective memory management and object integrity.
Enter first fraction's numerator: 4
Enter first fraction's denominator: 16
Enter second fraction's numerator: 6
Enter second fraction's denominator: 10
x + y = 17/20
x - y = -7/20
x * y = 3/20
x / y = 5/12