Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that performs these steps: Asks the user how many bank accounts

ID: 668617 • Letter: W

Question

Write a program that performs these steps:

Asks the user how many bank accounts are in the bank

Make a new array to hold the specified number of BankAccounts

In a loop, ask the user for the bank account number and balance, construct a BankAccount object with the account number and balance, and put the new BankAccount object in the array

After the user has entered all the bank accounts, use a for loop (or a for each loop) to compute the total balance of the accounts in the array

Print the computed total balance of the accounts and the average balance.

Use the provided BankAccount.java class for your BankAccount objects. Please read the BankAccount.java class and understand how to use its constructor and getBalance() methods. Change the provided BankArrayTester.java main() method to do the five steps outlined above.

Sample run:

Please enter the number of bank accounts: 3
Enter account number for account 1: 10001
Enter balance for account 1: 10000.00
Enter account number for account 2: 10015
Enter balance for account 2: 5000.00
Enter account number for account 3: 10031
Enter balance for account 3: 15000.00
The total balance is 30000.00.
The average balance is 10000.00.

___________

/**

A bank account has a balance that can be changed by

deposits and withdrawals.

*/

public class BankAccount

{

   private double balance;

   private int accountNumber;

   /**

   Constructs a bank account with a zero balance.

   */

   public BankAccount(int _accountNumber)

   {

   balance = 0;

   }

   /**

   Constructs a bank account with a given balance.

   @param initialBalance the initial balance

   */

   public BankAccount(int _accountNumber, double initialBalance)

   {

   accountNumber = _accountNumber;

   balance = initialBalance;

   }

   /**

   Deposits money into the bank account.

   @param amount the amount to deposit

   */

   public void deposit(double amount)

   {

   double newBalance = balance + amount;

   balance = newBalance;

   }

   /**

   Withdraws money from the bank account.

   @param amount the amount to withdraw

   */

   public void withdraw(double amount)

   {

   double newBalance = balance - amount;

   balance = newBalance;

   }

   /**

   Gets the current balance of the bank account.

   @return the current balance

   */

   public double getBalance()

   {

   return balance;

   }

   /**

   Gets the account number of the bank account.

   @return the account number

   */

   public double getAccountNumber()

   {

   return accountNumber;

   }

}

_______

import java.util.Scanner;

/**

   A class to test an array of BankAccounts.

   @author Your Name

*/

public class BankArrayTester {

   /**

   * Test building an array of BankAccounts

   * and then print the total and average balances.

   * @param args not used

   */

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       System.out.print("Please enter the number of bank accounts: ");

       int numAccounts = int.nextInt();

       BankAccount[] accounts = new BankAccount[numAccounts];

       // Write the loop to ask for each bank account

       // number and balance, create the BankAccount

       // object, and store the account in the accounts

       // array.

       double totalBalance = 0;

       // Write the loop to get the balance of each account

       // (hint: use the getBalance() method) and add it

       // to the total.

       System.out.printf("The total balance is %.2f ", totalBalance);

       System.out.printf("The average balance is %.2f ", totalBalance / numAccounts);

   }

}

Explanation / Answer

BankAccount.java


public class BankAccount
{
   private double balance;
   private int accountNumber;

   /**
       Constructs a bank account with a zero balance.
   */
   public BankAccount(int _accountNumber)
   {
       balance = 0;
   }
  
   /**
       Constructs a bank account with a given balance.
       @param initialBalance the initial balance
   */
   public BankAccount(int _accountNumber, double

initialBalance)
   {
       accountNumber = _accountNumber;
       balance = initialBalance;
   }
  
   /**
       Deposits money into the bank account.
       @param amount the amount to deposit
   */
   public void deposit(double amount)
   {
       double newBalance = balance + amount;
       balance = newBalance;
   }
  
   /**
       Withdraws money from the bank account.
       @param amount the amount to withdraw
   */
   public void withdraw(double amount)
   {
       double newBalance = balance - amount;
       balance = newBalance;
   }
  
   /**
       Gets the current balance of the bank account.
       @return the current balance
   */
   public double getBalance()
   {
       return balance;
   }

   /**
       Gets the account number of the bank account.
       @return the account number
   */
   public double getAccountNumber()
   {
       return accountNumber;
   }
}

BankArray.java


import java.util.*;

public class BankArray {

   public static void main(String[] args) {
       Scanner stdin = new Scanner(System.in);
       System.out.print("How many bank accounts are

in the bank?");
       int accNumber = stdin.nextInt();
      
      

   }

}


BankArrayTester.java

import java.util.Scanner;

public class BankArrayTester {

   /**
   * Test building an array of BankAccounts
   * and then print the total and average balances.
   * @param args not used
   */
   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       System.out.print("Please enter the number of

bank accounts: ");
       int numAccounts = in.nextInt();

       BankAccount[] accounts = new BankAccount

[numAccounts];
      
       //start loop
       for (int i = 0; i < numAccounts; i++)
       {
           Scanner stdin1 = new Scanner

(System.in); //prompts for number of accounts
           System.out.print("Enter account number

for account " + (i+1) + ": "); //account has to start from 1,

hence i+1
           int accountNum = stdin1.nextInt();
           //1st attribute of BankAccount is

stored
          
           Scanner stdin2 = new Scanner

(System.in);
           System.out.print("Enter balance for

account " + (i+1) + ": ");
           Double accountBal = stdin2.nextDouble

(); //input in the .pdf is a Double type
           //2nd attribute of BankAccount is

stored
          
           accounts[i] = new BankAccount

(accountNum, accountBal);
           //added entities with BankAccount

attribute to the array
       }
       //end loop

       double totalBalance = 0;
      
       for (int j = 0; j < numAccounts; j++)
       {
           totalBalance += accounts

[j].getBalance(); //added the balance from getBalance() to the

total
       }

       System.out.printf("The total balance is %.2f

", totalBalance);
       System.out.printf("The average balance is

%.2f ", totalBalance / numAccounts);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote