Question
public class BankAccount implements Filter { private long accountNumber; private double balance; private static int numberOfAccounts = 0; public BankAccount() { accountNumber = 0; balance
public class BankAccount implements Filter { private long accountNumber; private double balance; private static int numberOfAccounts = 0; public BankAccount() { accountNumber = 0; balance = 0; numberOfAccounts++; } public BankAccount(long accountNumber) { this.accountNumber = accountNumber; balance = 0; numberOfAccounts++; } public BankAccount(long accountNumber, double initialBalance) { this.accountNumber = accountNumber; balance = initialBalance; numberOfAccounts++; } public BankAccount(int accountNumber, double initialBalance) { this.accountNumber = accountNumber; balance = initialBalance; numberOfAccounts++; } public static int getNumberOfAccounts() { return numberOfAccounts; }
public double getBalance() { return balance; } public void deposit(double amount) { this.balance += amount; }
public void withdraw(double amount) { this.balance -= amount; }
public void transfer(double amount, BankAccount targetAccount) { this.withdraw(amount); targetAccount.deposit(amount); } public String toString() { return "Account Number: " + accountNumber + " Balance: " + balance; } //-----------Start below here. To do: approximate lines of code = 4 // Implement the necessary method such that class BankAccount implements the Filter interface // check the balance instance variable of this bank account object to see if it is in range specified by // the parameters. See Filter.java //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. }
Main program
// interface Filter filters out objects based on whether an (double) attribute of the object // is within a specified range public interface Filter { // method inRange returns true if the attribute is >= low and <= high, returns false otherwise boolean inRange(double low, double high); }
tester
import java.util.ArrayList;
class RangeFilterTester { public static void main( String[] args) { ArrayList accounts = new ArrayList(); accounts.add(new BankAccount(1398723,900)); accounts.add(new BankAccount(1432561,9900)); accounts.add(new BankAccount(1584624,52)); accounts.add(new BankAccount(1856210,2300)); accounts.add(new BankAccount(1745382,213)); accounts.add(new BankAccount(1965432,603)); accounts.add(new BankAccount(1234567,12)); for (int i = 0; i < accounts.size(); i++) { System.out.println(accounts.get(i)); } System.out.println("After Filtering:"); for (int i = 0; i < accounts.size(); i++) { if (accounts.get(i).inRange(100, 1000)) System.out.println(accounts.get(i)); } System.out.println("Expected: Account Number: 1398723 Balance: 900.0"); System.out.println("Account Number: 1745382 Balance: 213.0"); System.out.println("Account Number: 1965432 Balance: 603.0"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started