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.

public class Investment {
private double balance;
private double interestRate;
private double rate;
private double year=0;

public Investment(double Balance, double Rate) {
balance = Balance;
rate = Rate;
}
public double getBalance()
{
return balance;
}
public double getInterestRate(){ return interestRate; }
public void setBalance(double balance){
this.balance = balance;
}
public void setInterestRate(double rate){
this.rate = rate;
}

public double totalInterest(){
year++;
double interest = balance * rate / 100;
balance = balance *interest;
return interestRate;
}

public double totalInvestment(int periods) {
Math.pow((balance*(1+rate/100)), periods);
return 0;
}
public String toString(){
return "Investment Information\nCurrent balance: "+balance+"\nInterest Rate: "+rate;
}
}

can someone check if this code is correct?

yeah - java can do that for you, right?

The code you provided has some errors and doesn't entirely match the specification. Here's the corrected code:

```java
public class Investment {
private double balance;
private double interestRate;

public Investment() {
balance = 0.0;
interestRate = 0.0;
}

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

public double getBalance() {
return balance;
}

public double getInterestRate() {
return interestRate;
}

public void setBalance(double balance) {
this.balance = balance;
}

public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}

public double totalInterest() {
double interest = balance * (interestRate / 100.0);
return interest;
}

public double totalInvestment(int periods) {
double total = balance;
for (int i = 0; i < periods; i++) {
double interest = totalInterest();
total += interest;
}
return total;
}

public String toString() {
return "Investment Information\nCurrent balance: " + balance + "\nInterest Rate: " + interestRate;
}
}
```

Here are the changes made to the code:

1. Removed the unused `year` attribute.
2. Corrected the `totalInterest()` method to calculate the interest correctly based on the balance and interest rate.
3. Updated the `totalInvestment()` method to calculate the total investment correctly by adding the interest for each period.
4. Removed the unnecessary `rate` attribute from the class definition.

You can now use this revised code to create an `Investment` object and call its methods.