Reputation: 57
public void deposit(double amount)
{
balance += amount;
}
This is what I'm calling in another class. I want to be able to deposit 100$ into this account.
Account acct1;
acct1 = new Account(500, "Joe", 1112);
What would I need to do in order to deposit into this account? I've tried different variations of this (below), but I'm confused as to what to do.
initBal = new deposit(100);
Help?
Upvotes: 1
Views: 9400
Reputation: 330
Make sure your Account
object stores your initial balance and that your deposit
method increase it:
Example:
public class Account{
private Double balance;
public Account(Double initBalance, String name, int number){
this.balance = initBalance;
}
public void deposit(double amount)
{
balance += amount;
}
}
Then when you create an instance of Account acct1 = new Account(500, "Joe", 1112);
Then, to increase the balance of your account, you have to call the deposit method that is inside your instance of Account
acct1.deposit(amount)
Upvotes: 1
Reputation: 7042
The syntax for what it appears you want to do is:
Account acct1; //Creating a reference of type Account
acct1 = new Account(500, "Joe", 1112); //Instantiating a new Account object,
//giving a reference to that object to acct1
acct1.deposit(100); //Calling the deposit method in class Account
//On the object referred to by acct1
More generally, to call a method on an object (of a type that has that method):
<object_reference>.<method_name>(<parameter 1>, <parameter 2>, ...);
Upvotes: 1