java - Get sum and average of Array -


i'm making a program class. far have 1-3 done cant figure how implement 4 , 5. have been stuck on awhile. there 2 classes must used.

  1. how many bank accounts in bank

2) make new array hold specified number of bankaccounts

3) in loop, ask user bank account number , balance, construct bankaccount object account number , balance, , put new bankaccount object in array

4) after user has entered bank accounts, use loop (or each loop) compute total balance of accounts in array

5) print computed total balance of accounts , average balance.

package hw2; import java.util.scanner; import java.util.arraylist;  public class bankarraytester { public static void main(string[] args){       scanner in = new scanner(system.in);       system.out.println("please enter number bank accounts:");       int accounts= in.nextint();       bankaccount [] accountinfo = new bankaccount [accounts];       int c=0;       while(c<accounts){           c++;          system.out.println("enter account number account "+c);         int number=in.nextint();         system.out.println("enter balance account "+c);         double balance=in.nextdouble();         int a=0;         bankaccount numberbalance = new bankaccount(number,balance);         accountinfo [a]=numberbalance;         double test1;         (int = 0; < accountinfo.length; i++) {             test1 = accountinfo[a].getbalance();              system.out.println(test1);         }       }     } } 

other class

    package hw2;      /**      bank account has balance can changed     deposits , withdrawals.   */   public class bankaccount   {     private double balance;     private int accountnumber;  /**    constructs bank account 0 balance. */ public bankaccount(int _accountnumber) {       balance = 0; }  /**    constructs bank account given balance.    @param initialbalance initial balance */ public bankaccount(int _accountnumber, double initialbalance) {       accountnumber = _accountnumber;    balance = initialbalance; }  /**    deposits money bank account.    @param amount amount deposit */ public void deposit(double amount) {      double newbalance = balance + amount;    balance = newbalance; }  /**    withdraws money bank account.    @param amount amount withdraw */ public void withdraw(double amount) {       double newbalance = balance - amount;    balance = newbalance; }  /**    gets current balance of bank account.    @return current balance */ public double getbalance() {       return balance; } }  /**    gets account number of bank account.    @return account number */ 

use following finding total sum of balances

double sum=0; (int = 0; < accountinfo.length; i++) {         test1 = accountinfo[i].getbalance();          sum+=test1;         system.out.println(test1);     } 

to print sum , average

system.out.println("total ::"+sum); system.out.println("average ::"+sum/accounts); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -