Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class encapsulating the concept of an investment, assuming the investmen

ID: 3548238 • Letter: W

Question

Write a class encapsulating the concept of an investment, assuming the investment has the following attributes: the amount of the investment, and the interest rate at which the investment will be compounded, include constructor, the accessor and mutators, and methods toString and equals. Also include a method returning the future value of the investment depending on how many years we hold it before selling it, which can be calculated using the formula:


future value = investment (1 + interest rate)^number of years


We will assume that the interest rate is compounded annually. Write a client class to test all the methods in your class.

Explanation / Answer

import java.util.*;

class Investment
{
private double amount;
private double interest_rate;
public Investment()
{
amount = 0;
interest_rate = 0;
}
void setAmount(double amt)
{
amount = amt;
}
void setInterest_rate(double rate)
{
interest_rate = rate;
}
double getAmount()
{
return amount;
}
double getInterest_rate()
{
return interest_rate;
}
public String toString()
{
return "Amount is "+ amount + " with annual interest rate of " + interest_rate;
}
public boolean equals(Investment IT)
{
return ((amount == IT.amount) && (interest_rate == IT.interest_rate));
}
public double future_value(int years)
{
return amount*Math.pow((1 + interest_rate),years);
}
}

public class InvestmentClient
{
public static void main(String[] args)
{
Investment new_Investment = new Investment();
double amount;
double interest_rate;
Scanner in = new Scanner(System.in);
System.out.println("Enter amount to invest ");
amount = in.nextDouble();
System.out.println("Enter interest rate per year ");
interest_rate = in.nextDouble();
new_Investment.setAmount(amount);
new_Investment.setInterest_rate(interest_rate);
System.out.println(new_Investment);
System.out.println("Enter no of years to find future value ");
int years = in.nextInt();
System.out.println("Future value of investment is given by "+ new_Investment.future_value(years));
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote