Consider the following BankAccount class. This class has a three- argument const
ID: 3702722 • Letter: C
Question
Consider the following BankAccount class. This class has a three- argument constructor method, as well as a method for computing the “simple compound interest,” and a couple of accessor methods for returning the bank account owner’s name and for returning the current balance.
/** BankAccount.java
* Trivial class for creating and managing bank accounts */
public class BankAccount
{ // first, 3 instance variables
}
}
Suppose Geek Bank wants to store a lot of bank accounts in a two- dimensional array named geekBank such that accounts[0] stores all the accounts set up in Geek Bank’s first year in business, accounts[1] will store all the accounts set up in Geek Bank’s second year in business, and so on. As an example, consider the following declaration and initialization of geekBank:
}}
Note that variable geekBank contains a reference to an array. Each element of the array (e.g., geekBank[0], geekBank[1], etc.) contains a reference to an array of BankAccount objects. For example, geekBank[0] is a reference to an array containing 3 BankAccounts. Note that the above statement only initialized geekBank[0] and not the other elements of the array.
QUESTION:
Suppose that geekBank has been completely declared and initialized, not necessarily with the values shown above. In one or two simple, clear, unambiguous English sentences, what does the following loop accomplish?
{
total=0.0; n=0;
if (geekBank[k] != null)
{
n++;
}
PART B
Write some Java code to print out just the name of the person who has the BankAccount with the smallest balance in all of geekBank. You may assume that all balances have different values, so that there’s a single person you will have to locate.
Explanation / Answer
QUESTION 1 : Suppose that geekBank has been completely declared and initialized, not necessarily with the values shown above. In one or two simple, clear, unambiguous English sentences, what does the following loop accomplish?
Ans : code print the average balance for each year of bussiness.
------------
there are two loops first to get each year bussinness ie geekBank[k] second loop get the total balance geekBank[k][j].getBalance(); then divide by number of account to get average System.out.println( total / n );
-----------------------
Question 2 :
Write some Java code to print out just the name of the person who has the BankAccount with the smallest balance in all of geekBank. You may assume that all balances have different values, so that there’s a single person you will have to locate.
Ans :
//==================
//=== Question 2
// Since we don't know in which year we need to get the minimum balance
// we find find the user who minimum balance in all the years
//==================
String name = ""; //store the user name
double minBal = 0.0; //store the minimum balance
minBal = geekBank[0][0].getBalance(); //initalise the minbalance with value of first user
//^^ since you cannot init minBal with zero we need to set with balance of user
name = geekBank[0][0].getName(); //set the name of user
for (int k = 0; k < geekBank.length; k++) {
if (geekBank[k] != null) {
for (int j = 0; j < geekBank[k].length; j++) {
//IF some user has balance lower then user selected above
if( geekBank[k][j].getBalance() < minBal)
{
minBal = geekBank[k][j].getBalance(); //update the minBalance
name = geekBank[k][j].getName(); //update the user name
}
}
}
}
//print the person name
System.out.println("person who has minium balance is " +name);
//=======================
//=======================
// Below is the complete code of my class for your reference
public class bankTest {
public static void main(String[] args) {
BankAccount[][] geekBank = { { new BankAccount("fred", 345.00, 0.02), new BankAccount("mary", 1234.00, 0.02),
new BankAccount("karl", 1.79, 0.025) } };
for (int i = 0; i < 3; i++) {
System.out.println("" + geekBank[0][i].getBalance());
//System.out.println("lenght " + geekBank.length);
}
//==================
//=== Question 1
double total;
int n;
for (int k = 0; k < geekBank.length; k++) {
total = 0.0;
n = 0;
if (geekBank[k] != null) {
for (int j = 0; j < geekBank[k].length; j++) {
n++;
total += geekBank[k][j].getBalance();
}
System.out.println("average "+(total / n));
}
}
//==================
//=== Question 2
// Since we don't know in which year we need to get the minimum balance
// we find find the user who minimum balance in all the years
//==================
String name = ""; //store the user name
double minBal = 0.0; //store the minimum balance
minBal = geekBank[0][0].getBalance(); //initalise the minbalance with value of first user
//^^ since you cannot init minBal with zero we need to set with balance of user
name = geekBank[0][0].getName(); //set the name of user
for (int k = 0; k < geekBank.length; k++) {
if (geekBank[k] != null) {
for (int j = 0; j < geekBank[k].length; j++) {
//IF some user has balance lower then user selected above
if( geekBank[k][j].getBalance() < minBal)
{
minBal = geekBank[k][j].getBalance(); //update the minBalance
name = geekBank[k][j].getName(); //update the user name
}
}
}
}
//print the person name
System.out.println("person who has minium balance is " +name);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.