Question: For this problem, I want us to practice creating an array of object. We must first have a class to add to our array and
For this problem, I want us to practice creating an array of object. We must first have a class to add to our array and for this problem, we are going to use what we already have, BankAccount. (BankAccount in bold at the bottom)
a, Open the BankAccount class, you can use the basic one.
b. Create a BankAccountArrayTester class.
c. Create an array of type BankAccount that holds 10 BankAccount object. You create this by typing: BankAccount[] bankArray=new BankAccount[10].
d. Create a loop.
e. Inside the loop create a BankAccount. Put a different balance in each.
f. Add the BankAccount object to your array. Simply add the variable name to the array.
g. Once you have created the array we need to print it. Create another loop.
h. Inside the loop, we will print the balance for each element of the BankAccount array. Because it is an object, you cant do this directly. You must use the getBalance method. Your code should look something like: System.out.println(Balance is: + bankArray[i].getBalance());
public class BankAccount { private double balance; /** * Constructs a bank account with a zero balance. */ public BankAccount() { balance=0; } /** * Constructs a bank account with a given balance. * @param intialBalance the initial balance. */ public BankAccount(double initialBalance) { balance = initialBalance; } /** * Deposits money into the bank account. * @param amount the amount to deposit. */ public void deposit(double amount) { balance= balance + amount; } /** * Withdraws money from the bank account. * @param amount the amount to withdraw. */ public void withdraw(double amount) { balance = balance - amount; } /** * Gets the current balance of the bank account. * @return the current balance. */ public double getBalance() { return balance; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
