Skip to content

Latest commit

 

History

History
37 lines (28 loc) · 1.6 KB

File metadata and controls

37 lines (28 loc) · 1.6 KB

➗ Java Fraction Arithmetic Library

Java

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.

✨ Key Features

  • 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)

🧮 Mathematical Logic

1. GCD & Reduction

The reduce() method is marked as private final to protect the internal logic. It utilizes a custom GCD function: $$gcd(a, b) = \max {g \in \mathbb{Z} : g|a \text{ and } g|b}$$

2. Copy Constructor

Implemented to allow safe duplication of objects without affecting the original instance, a core concept in effective memory management and object integrity.

🚀 Example Usage

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