The Investment class: Write a class called Investment with the following specification:

Class: Investment
Attributes:
- balance : double
- interestRate : double
Methods:
+ Investment()
+ Investment(balance : double, rate : double)
+ getBalance() : double
+ getInterestRate() : double
+ setBalance(balance : double) : void
+ setInterestRate(rate : double) : void
+ totalInterest() : double
+ totalInvestment(periods : int) : double
+ toString() : String
Your class definition must contain all of the members listed in the specification exactly as they are defined.

The notes below will help you with the design of the Investment class:

balance - the current balance of the investment. Must be greater than 0. Default: 1000
interestRate - the interest rate of the investment. Is always stated as a standard percentage (for example, an interest rate of 2.3% is indicated with the value 2.3). Must be greater than 0, but no more than 12. Default: 3.5
Mutator methods should throw an IllegalArgumentException when parameters are invalid. Your Exception must have an appropriate error message.
The default constructor sets the default values of the instance variables, if they are not already set when the variables are initialized (in which case your default constructor is empty).
totalInterest() method: returns the amount of interest on the current balance. For example, if the balance is 1000 and the interest rate is 5%, this method will return 50.0.
totalInvestment(int periods): calculates what the total amount the investment will be worth after certain number of periods. This method accepts a parameter for the number of periods. The total amount of investment on a balance (B) at a certain interest rate (R) for a certain number of periods (P) is: B * (1 + R/100)P
toString() - returns the investment object's information as a string in the following format:
Investment Information
Current Balance: 1,000.00
Interest Rate: 5.0%
(your balance must be formatted to 2 decimal places with the thousands separator, and interest rate must be formatted to 1 decimal place and include the percent sign)
Your class must work with the following test program (you may not change the test program in any way):

public class TestInvestment {
public static void main(String[] args) {
try {
Investment inv1 = new Investment();
System.out.println(inv1);
inv1.setBalance(10000);
inv1.setInterestRate(10.2);
System.out.println(inv1);

// uncomment this to test it (it should throw an exception):
//Investment inv2a = new Investment(0, 3);

// uncomment these lines to test them (last 2 lines should also
// throw exceptions):
//Investment inv2b = new Investment();
//inv2b.setBalance(-3);
//inv2b.setInterestRate(-3);
//inv2b.setInterestRate(12.01);

Investment inv3 = new Investment(2500, 7.9);
System.out.println(inv3);
double interest = inv3.totalInterest();
double total = inv3.totalInvestment(10);
System.out.printf("Total interest: $%4.2f%n", interest);
System.out.printf("Total investment: $%4.2f%n", total);

} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}

the code above is giving me errors because of the code below. can someone help me please.

public class Investment {
private double balance;
private double interestRate;
private double rate;


public Investment(double Balance, double Rate)
{
balance = Balance;
rate = Rate;
}

public double getBalance()
{
return balance;
}
public double getInterestRate(){ return interestRate; }
public void setBalance(double Default){
getBalance();

}
public void setInterestRate(){
getInterestRate();
}
double totalInterest(){
return interestRate;
}

public double totalInvestment() {
double targetBalance = 0;
while (balance < targetBalance)
{
year++;
double interest = balance * rate / 100;
balance = balance + interest;
}
return 0;
}
public String toString(){
return "Investment Information"+"\nCurrent balance: "+balance+"\nInterest Rate: "+rate;
}

well, it's missing a closing }

There are several errors in your code that need to be fixed. I will explain each error and provide the corrected code.

1. Error: The class name and the constructor name do not match.

In your code, the class name is "Investment" and the constructor name is "Investment". The constructor name should be the same as the class name.

Correction: Change the constructor name to "Investment" in line 9.

```java
public Investment(double balance, double interestRate)
{
this.balance = balance;
this.interestRate = interestRate;
}
```

2. Error: The variable name in the constructor does not match the attribute name.

In the constructor, the attribute names should be used instead of local variable names.

Correction: Replace the local variable names with the attribute names in lines 10 and 11.

```java
public Investment(double balance, double interestRate)
{
this.balance = balance;
this.interestRate = interestRate;
}
```

3. Error: The getter and setter methods are not implemented correctly.

The getter and setter methods should access the corresponding attributes. In your code, the getter and setter methods are empty.

Correction: Implement the getter and setter methods correctly.

```java
public double getBalance()
{
return balance;
}

public double getInterestRate()
{
return interestRate;
}

public void setBalance(double balance)
{
if (balance > 0) {
this.balance = balance;
} else {
throw new IllegalArgumentException("Balance must be greater than 0");
}
}

public void setInterestRate(double interestRate)
{
if (interestRate > 0 && interestRate <= 12) {
this.interestRate = interestRate;
} else {
throw new IllegalArgumentException("Interest rate must be greater than 0 and less than or equal to 12");
}
}
```

4. Error: The totalInterest() method does not calculate the correct interest.

In your code, the totalInterest() method returns the interest rate instead of calculating the actual interest.

Correction: Calculate and return the total interest using the formula: balance * interestRate / 100.

```java
public double totalInterest()
{
return balance * interestRate / 100;
}
```

5. Error: The totalInvestment() method is not implemented correctly.

In your code, the totalInvestment() method is incomplete. The targetBalance variable is not set and there is no loop to calculate the investment over multiple periods.

Correction: Initialize the targetBalance variable with the initial balance and implement a loop to calculate the investment over multiple periods.

```java
public double totalInvestment(int periods)
{
double targetBalance = balance;
for (int i = 0; i < periods; i++) {
double interest = targetBalance * interestRate / 100;
targetBalance += interest;
}
return targetBalance;
}
```

6. Error: The toString() method does not format the balance and interest rate correctly.

In your code, the balance and interest rate are not formatted with decimal places and separators.

Correction: Use the DecimalFormat class to format the balance and interest rate.

```java
public String toString()
{
DecimalFormat df = new DecimalFormat("#,##0.00");
return "Investment Information"
+ "\nCurrent balance: $" + df.format(balance)
+ "\nInterest rate: " + String.format("%.1f", interestRate) + "%";
}
```

After making these corrections, your Investment class should work correctly.