Reputation: 1
My code does not compile. How can I resolve the errors?
import java.util.*;
public class BankAccount{
private double balance;
public BankAccount(double b) {
balance = b;
}
public double getBalance(){
return balance;
}
public void deposit(double d){
balance += d;
}
public boolean withdraw(double d){
if(d > balance){
return false;
} else {
balance -= d;
return true;
}
}
public class SavingsAccount extends BankAccount{
private double interest;
public SavingsAccount(double k, double inRate){
super(k);
interest = inRate;
}
public void gainInterest(){
super.getBalance() = super.getBalance() * interest;
}
}
public static void main(String[] args) {
SavingsAccount test = new SavingsAccount(1000, .05);
test.gainInterest();
System.out.println(test.getBalance());
}
The following errors are
I get the unexpected type error at super.getBalance() = super.getBalance() * interest; and the "non-static variable this cannot be referenced from a static context" at SavingsAccount test = new SavingsAccount(1000, .05);
Upvotes: 0
Views: 56
Reputation: 347194
You can't assign a value to a method.
You will need to assign the result to a variable or pass the result to another method, in this case you can use deposit
, something like...
public void gainInterest(){
deposit(super.getBalance() * interest);
}
Upvotes: 3