0% found this document useful (0 votes)
52 views5 pages

CMSC 330 Project 1: Library & Investment Classes

The document describes two programming assignments. The first involves creating a Library class with instance variables to store a book's accession number, title, and author. The class contains methods to input data, calculate late fees, and display book details. A test program demonstrates class usage. The second assignment involves creating a FixedInvestment class to model an investment with fields for deposit amount, interest rate, and years. Methods include getting total return calculated as the initial deposit multiplied by monthly compound interest over the specified number of years. A test program demonstrates class usage by modeling a $10,000 investment at 4.5% annually for 3 years.

Uploaded by

Jaskirat Kaur
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views5 pages

CMSC 330 Project 1: Library & Investment Classes

The document describes two programming assignments. The first involves creating a Library class with instance variables to store a book's accession number, title, and author. The class contains methods to input data, calculate late fees, and display book details. A test program demonstrates class usage. The second assignment involves creating a FixedInvestment class to model an investment with fields for deposit amount, interest rate, and years. Methods include getting total return calculated as the initial deposit multiplied by monthly compound interest over the specified number of years. A test program demonstrates class usage by modeling a $10,000 investment at 4.5% annually for 3 years.

Uploaded by

Jaskirat Kaur
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment

Q1. Develop a class called Library with the following description by applying
the concept of class and object:
 Instance Variable/Data Members
int acc_num - to store the accession number of the book.
String title - to store the title of the book
String author - to store the name of the author
 Member methods
void input() - to input and store the detail of the book
void compute() - to accept the number of days late, calculate and display the
fine charged at the rate of Rs. 2 per day.
void display() - to display the all the details of book
Write a program for the above mentioned class and call the method
accordingly.
class Assignment3Question1 {
public static void main(String[] args) {
Library book = new Library();
[Link](001, "harry puttar", "joking rolling");
[Link](10);
[Link]();
}
}
class Library {
int acc_num;
String title;
String author;
void input(int acc_num, String title, String author)
{
this.acc_num = acc_num;
[Link] = title;
[Link] = author;
}
void compute(int days_late) {
[Link]("Your late fine is ₹" + 2*days_late);
}
void display()
{
[Link]("accession number:\t" + this.acc_num);
[Link]("title:\t\t\t" + [Link]);
[Link]("author:\t\t\t" + [Link]);
}
}
2. Build a class named FixedInvestment that contains:
 A double data field named depositAmount that specifies the investment
amount (default 1000).
 A double data field named annualInterestRate that specifies the fixed
interest rate (default 5.0%)
 An int data field named numberOfYears that specifies the investment
duration (default 1).
 A no-arg constructor that create a default instance.
 A constructor that creates an instance with the specified
depositAmount, annualInterestRate and numberOfYears.
 The accessor methods for depositAmount, numberOfYears and
annualInterestRate.
 A method named getTotalReturn() that return the investment return
after the specified number of years.
The total return can be computed using the following formula:
totalReturn = investmentAmount x (1 +
monthlyInterestRate)numberOfYears*12

Write a test program that creates a FixedInvestment object with a deposit


amount of 10000 and an annual interest rate of 4.5% for three years. Display
the total return after three years.
import [Link];
class Assignment3Q2 {
public static void main(String[] args) {
FixedInvestment invest = new FixedInvestment(10000, 4.5, 3);
[Link]("Returns value of default investment is: " +
[Link]());
}
}
class FixedInvestment {
double depositAmout;
double annualInterestRate;
int numberOfYears;
FixedInvestment() {
[Link] = 1000.0;
[Link] = 5.0;
[Link] = 1;
}
FixedInvestment(double depositAmout, double annualInterestRate, int
numberOfYears) {
[Link] = depositAmout;
[Link] = annualInterestRate;
[Link] = numberOfYears;
}
public double getDepositAmout() {
return depositAmout;
}
public void setDepositAmout(double depositAmout) {
[Link] = depositAmout;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
[Link] = annualInterestRate;
}

public int getNumberOfYears() {


return numberOfYears;
}
public void setNumberOfYears(int numberOfYears) {
[Link] = numberOfYears;
}
double getTotalReturn() {
return depositAmout * [Link]((1 + [Link] / 100),
[Link] * 12);
}
}

Common questions

Powered by AI

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 .

You might also like