0

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);

2

1 Answer 1

3

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); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.