CMSC 330 Project 1: Library & Investment Classes
CMSC 330 Project 1: Library & Investment Classes
To enhance the `Library` and `FixedInvestment` classes, the use of design patterns like Factory or Dependency Injection could be beneficial. For example, a Factory pattern could standardize object creation, especially for `FixedInvestment`, managing different investment types or scenarios, enhancing scalability . The Visitor pattern might extend `Library` for additional operations like book categorization without altering its structure. Incorporating principles like Cohesion and SRP (Single Responsibility Principle) would reduce complexity and improve maintainability, by ensuring each class or method has a single focused purpose .
The `Math.pow()` function in `FixedInvestment` is used to effectively calculate compound interest by exponentiating the monthly interest rate to the total number of compounding periods (months in this case). Its role in Java-based financial applications is crucial for performing reliable, computationally efficient power calculations necessary for modeling exponential growth situations like compound interest. This utility enhances precision and speed, integral for financial software where accuracy in calculating returns over long periods influences financial decisions and outcomes .
Constructor overloading in `FixedInvestment` promotes flexibility by offering two constructors: one that initializes default values (`depositAmount` of 1000, `annualInterestRate` of 5.0, `numberOfYears` of 1) and another that allows customization with specific parameters . The default constructor might be used in scenarios where a quick, generic investment setup is needed with standard parameters. The parameterized constructor is suitable when specific investment conditions are warranted, such as a larger `depositAmount`, a different `annualInterestRate`, or longer investment terms, providing tailored investment modeling .
The `Library` class adheres to encapsulation by defining instance variables that store book details and providing methods to manipulate these variables. However, to enhance data protection, it could improve encapsulation by making instance variables `private` and providing getter and setter methods to control access. This would prevent unauthorized modification and adhere more strictly to encapsulation principles by ensuring that the internal state is not accessible directly from outside the class .
The `FixedInvestment` class demonstrates principles of object-oriented programming by encapsulating investment-related data into instance variables such as `depositAmount`, `annualInterestRate`, and `numberOfYears` . It uses constructors to initialize objects, demonstrates abstraction through methods like `getTotalReturn()`, and encapsulation by providing accessor methods for its fields. This design encapsulates data and behavior related to fixed investments within a single class .
The default constructor in the `FixedInvestment` class initializes a new object with pre-set values, facilitating quick instantiation without requiring initial parameters, which simplifies object creation when default values are acceptable . However, the drawback is that it may not suit all user requirements if different initial values are needed, as it only creates a `FixedInvestment` object with a `depositAmount` of 1000, `annualInterestRate` of 5.0%, and `numberOfYears` of 1 by default .
The `compute()` method in the `Library` class calculates the fine for overdue books by multiplying the number of days late by Rs. 2 per day, directly outputting the fine amount . Its strength lies in its simplicity and direct approach to handling fines. However, weaknesses include its lack of flexibility to change fine rates without modifying the method itself, absence of error handling for negative day values, and direct printing instead of returning a value, which reduces its utility in broader contexts .
The `getTotalReturn` method in `FixedInvestment` accurately models compound interest computation by converting the `annualInterestRate` to a monthly rate, then raising it to the power of the total number of months over the investment period using `Math.pow()` . This approach accurately reflects compounded growth over time in financial calculations and is consistent with standard methods for calculating compound interest. However, using monthly compounding might diverge slightly from annual compounding due to the difference in compounding frequency, suggesting that practical application requires matching the method with actual compounding terms .
The `FixedInvestment` class calculates the total return using the formula `totalReturn = depositAmount × (1 + monthlyInterestRate)^(numberOfYears*12)`. The `monthlyInterestRate` is derived by dividing the `annualInterestRate` by 100 and then compounded monthly over the specified number of years . The `getTotalReturn()` method employs Java's Math.pow function to execute the exponentiation part of the formula .
The `Library` class encapsulates book-related data through three instance variables: `int acc_num` for the accession number, `String title` for the book's title, and `String author` for the author's name. It provides three methods for interacting with this data: `input()`, which assigns values to these instance variables; `compute()`, which calculates a late fine based on the number of days a book is overdue, using a rate of Rs. 2 per day ; and `display()`, which prints the book's details, such as accession number, title, and author .