Question

In: Computer Science

Exercise 4: Perform the following on the Account class and test it in the AccountArrayTest: ...

Exercise 4: Perform the following on the Account class and test it in the AccountArrayTest:  In the AccountArrayTest class, add a static method called getAverageBalance (Account accounts[]) that returns the average balance of all accounts. Use the enhanced for loop.  In the main method get and print the average accounts balance.  Add an instance method called deposit() that takes a double amount as parameter and if it is positive it adds it to the current balance. If the amount is less than or equal to zero it does nothing.  Add a static method called addInterest(Account accounts[]) that calculates the interest for each account in the accounts array (based on the interestRate value), and then deposits it into the relevant account in that array. (Hint: interest = interestRate * balance/100).  Use the above methods to apply the addInterest() for all the accounts in your AccountArrayTest. Print the accountNo and balance after the update.

Solutions

Expert Solution

/****************************************Account.java**********************/


// TODO: Auto-generated Javadoc
/**
* The Class Account.
*/
public class Account {

   /** The owner. */
   private String owner;

   /** The account number. */
   private String accountNumber;

   /** The balance. */
   private double balance;

   /** The interest rate. */
   private double interestRate;

   /**
   * Instantiates a new account.
   *
   * @param owner the owner
   * @param accountNumber the account number
   * @param balance the balance
   */
   public Account(String owner, String accountNumber, double balance) {
       super();
       this.owner = owner;
       this.accountNumber = accountNumber;
       this.balance = balance;
   }

   /**
   * Gets the owner.
   *
   * @return the owner
   */
   public String getOwner() {
       return owner;
   }

   /**
   * Sets the owner.
   *
   * @param owner the new owner
   */
   public void setOwner(String owner) {
       this.owner = owner;
   }

   /**
   * Gets the interest rate.
   *
   * @return the interest rate
   */
   public double getInterestRate() {
       return interestRate;
   }

   /**
   * Sets the interest rate.
   *
   * @param interestRate the new interest rate
   */
   public void setInterestRate(double interestRate) {
       this.interestRate = interestRate;
   }

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

   /**
   * Sets the account number.
   *
   * @param accountNumber the new account number
   */
   public void setAccountNumber(String accountNumber) {
       this.accountNumber = accountNumber;
   }

   /**
   * Gets the balance.
   *
   * @return the balance
   */
   public double getBalance() {
       return balance;
   }

   /**
   * Sets the balance.
   *
   * @param balance the new balance
   */
   public void setBalance(double balance) {
       this.balance = balance;
   }

   /**
   * Deposit.
   *
   * @param amount the amount
   */
   public void deposit(double amount) {

       this.balance += amount;
   }

   /**
   * Withdraw.
   *
   * @param amount the amount
   */
   public void withdraw(double amount) {

       if (balance > amount) {

           this.balance -= amount;
       }
   }

   /**
   * Adds the interest.
   */
   public void addInterest() {

       this.balance += balance * interestRate / 100;
   }

}
/**********************************AccountArrayTest.java**********************/


/**
* The Class AccountArrayTest.
*/
public class AccountArrayTest {

   /**
   * Gets the average balance.
   *
   * @param accounts the accounts
   * @return the average balance
   */
   public static double getAverageBalance(Account[] accounts) {

       double totalBalance = 0;
       for (Account account : accounts) {

           totalBalance += account.getBalance();
       }

       return totalBalance / accounts.length;

   }

   /**
   * Adds the interest.
   *
   * @param accounts the accounts
   */
   public static void addInterest(Account[] accounts) {

       for (Account account : accounts) {

           account.addInterest();
       }
   }

   /**
   * The main method.
   *
   * @param args the arguments
   */
   public static void main(String[] args) {

       Account[] accounts = new Account[3];

       accounts[0] = new Account("Virat", "12478252", 20000);
       accounts[1] = new Account("Kohli", "12478252", 20000);
       accounts[2] = new Account("MS", "12478252", 20000);
       accounts[0].setInterestRate(10);
       accounts[1].setInterestRate(9);
       accounts[2].setInterestRate(12);
       System.out.println("Average Balance: " + getAverageBalance(accounts));

       addInterest(accounts);

       for (Account account : accounts) {

           System.out.println(
                   "Account number: " + account.getAccountNumber() + " has balance: $" + account.getBalance());
       }

   }
}
/*****************************output******************************/

Average Balance: 20000.0
Account number: 12478252 has balance: $22000.0
Account number: 12478252 has balance: $21800.0
Account number: 12478252 has balance: $22400.0

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

The Account class Create a class named Account, which has the following private properties:
in java The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber (), getBalance (), setBalan newBalance). There is no setNumber () once an account is created, its account number cannot change. Now implement these methods: void deposit (double amount) and void withdraw (double amount). For both these methods, if the amount is less than...
The Account class Create a class named Account , which has the following private properties:
 The Account class Create a class named Account , which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNunber(), getBalance(), setBalance (double newBalance) . There is no setNunber() - once an account is created, its account number cannot change. Now implement these methods: void deposit (double anount) and void withdraw(double anount). For both these methods, if the amount is less than zero,...
If a researcher wanted to test whether attending class influences how students perform on an exam,...
If a researcher wanted to test whether attending class influences how students perform on an exam, he collected needed data and decided that the alpha level is 0.05, so the critical value is 3.841 (4 mark) Pass Fail Total attended 25 6 31 skipped 8 15 23 Total 33 21 54 The researcher calculated the expected frequency for each cell and came up with these numbers in parenthesis Pass Fail Total attended 25 (18.94) 6 (12.05) 31 skipped 8 (14.05)...
4.Explain one way you can perform the oxidase test. a.Name a bacterial species that will test...
4.Explain one way you can perform the oxidase test. a.Name a bacterial species that will test negative for this test.
You will perform an ANOVA test. For the hypothesis test make sure to report the following...
You will perform an ANOVA test. For the hypothesis test make sure to report the following steps: Identify the null hypothesis, Ho, and the alternative hypothesis, Ha. Determine whether the hypothesis test is left-tailed, right-tailed, or two-tailed. Find the critical value(s) and identify the rejection region(s). Find the appropriate standardized test statistic. If convenient, use technology. Decide whether to reject or fail to reject the null hypothesis. Interpret the decision in the context of the original claim. The grade point...
31. An instructor wants to test whether attending class influences how students perform on an exam....
31. An instructor wants to test whether attending class influences how students perform on an exam. There are 54 students in the class. There are 25 students who attended the class and passed the exam, 6 students who attended class and failed the exam, 8 students who skipped the class and passed the exam, 15 students who skipped the class and failed class. Please perform a statistical test and indicate whether attending class influence the exam performance. a) Parametric or...
The Account class Create a class named Account, which has the following private properties: number: long...
The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change. Now implement deposit(double amount) and withdraw(double amount) methods. If the amount is less than zero,...
The Account class Create a class named Account, which has the following private properties: number: long...
The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change. Now implement deposit(double amount) and withdraw(double amount) methods. If the amount is less than zero,...
The Account class Create a class named Account, which has the following private properties: number: long...
The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change. Now implement these methods: void deposit(double amount) and void withdraw(double amount). For both these methods,...
File Account.java (see 4.1. A Flexible Account Class exercise) contains a definition for a simple bank...
File Account.java (see 4.1. A Flexible Account Class exercise) contains a definition for a simple bank account class withmethods to withdraw, deposit, get the balance and account number, and return a String representation. Note that theconstructor for this class creates a random account number. Save this class to your directory and study it to see how it works.Now modify it to keep track of the total number of deposits and withdrawals (separately) for each day, and the total amountdeposited and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT