Question

Declare an interface Filter as follows: public interface Filter {boolean accept (Object x); } Modify the...

Declare an interface Filter as follows:

public interface Filter {boolean accept (Object x); }

Modify the implementation of the DataSet class in Section 10.4 to use both a Measurer and a Filter object. Only objects that the filter accepts should be processed. Demonstrate your modification by processing a collection of bank accounts, filtering out all accounts with balances less than $1,000. Please use the code provided and fill in what is needed.

BankAccount.java

/**
   A bank account has a balance that can be changed by
   deposits and withdrawals.
*/
public class BankAccount
{
   private double balance;

   /**
Constructs a bank account with a zero balance.
   */
   public BankAccount()
   {
balance = 0;
   }

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

   /**
Deposits money into the bank account.
@param amount the amount to deposit
   */
   public void deposit(double amount)
   {
balance = balance + amount;
   }

   /**
Withdraws money from the bank account.
@param amount the amount to withdraw
   */
   public void withdraw(double amount)
   {
balance = balance - amount;
   }

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

   /**
Adds interest to the bank account.
@param rate The percentage rate of interest gained.
   */
   public void addInterest(double rate)
   {
balance = balance + balance * rate / 100;
   }
}

BalanceFilter.java

/**
   Filter out balance less than $1,000.
*/
public class BalanceFilter implements Filter
{
   /**
Accept balances no less than $1,000
@param object bank account
@return true if balance is no less than $1,000
   */
   public boolean accept(Object object)
   {
...
   }
}

BalanceMeasurer.java

/**
   Measure bank account balance
*/
public class BalanceMeasurer implements Measurer
{
   /**
Measure bank account balance
@param object bank account
@return balance
   */
   public double measure(Object object)
   {
return ((BankAccount) object).getBalance();
   }
}

Filter.java

/**
   Describes any class whose objects can filter other objects.
*/
public interface Filter
{
   /**
Filters or accepts an object.
@param anObject the object to potentially be filtered
@return accept or reject
   */
   boolean accept(Object anObject);
}

Measurer.java

/**
   Describes any class whose objects can measure other objects.
*/
public interface Measurer
{
   /**
Computes the measure of an object.
@param anObject the object to be measured
@return the measure
   */
   double measure(Object anObject);
}

DataSet.java

public class DataSet
{
   /**
Computes the average of the measures of the given objects.
@param objects an array of objects
@param meas the measurer for the objects
@param filt the filter for the objects
@return the average of the measures
   */
   public static double average(Object[] objects, Measurer meas, Filter filt)
   {
...
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Only BalanceFilter.java and DataSet.java files are attached. Other files are not modified (no modifications needed).

// BalanceFilter.java

/**

* Filter out balance less than $1,000.

*/

public class BalanceFilter implements Filter {

      /**

      * Accept balances no less than $1,000

      *

      * @param object

      *            bank account

      * @return true if balance is no less than $1,000

      */

      public boolean accept(Object object) {

            return ((BankAccount) object).getBalance() >= 1000;

      }

}

// DataSet.java

public class DataSet {

      /**

      * Computes the average of the measures of the given objects.

      *

      * @param objects

      *            an array of objects

      * @param meas

      *            the measurer for the objects

      * @param filt

      *            the filter for the objects

      * @return the average of the measures

      */

      public static double average(Object[] objects, Measurer meas, Filter filt) {

            // initializing sum, average and count to 0

            double sum = 0, avg = 0;

            int count = 0;

            // looping through each object in objects array

            for (Object ob : objects) {

                  // using filter object, checking if ob can be accepted

                  if (filt.accept(ob)) {

                        // acceptable, so adding balance of ob to sum, getting balance

                        // using Measurer object

                        sum += meas.measure(ob);

                        // updating count

                        count++;

                  }

            }

            // finding average if count>0 to prevent division by zero.

            if (count > 0) {

                  avg = (double) sum / count;

            }

            return avg;

      }

      // code for testing

      public static void main(String[] args) {

            //creating a BankAccounts array containing different BankAccount objects

            //having balances less than 1000, equal to 1000 and greater than 1000

            BankAccount accounts[] = new BankAccount[] {

                        new BankAccount(200),

                        new BankAccount(1500),

                        new BankAccount(1000),

                        new BankAccount(2000),

                        new BankAccount(3000),

                        new BankAccount(0),

                        new BankAccount(50),

            };

            //creating a BalanceMeasurer and BalanceFilter

            BalanceMeasurer measurer=new BalanceMeasurer();

            BalanceFilter filter=new BalanceFilter();

            //finding average, it will be (1500+1000+2000+3000)/4 = 1875.0

            double avg=average(accounts, measurer, filter);

            //displaying average

            System.out.println("Average is "+avg);

      }

}

/*OUTPUT*/

Average is 1875.0

Add a comment
Know the answer?
Add Answer to:
Declare an interface Filter as follows: public interface Filter {boolean accept (Object x); } Modify the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...

  • java code ========= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    java code ========= public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...

  • Susceptible.java interface Susceptible { public boolean infect(Disease disease); public void forceInfection(Disease disease); public Disease getCurrentDisease(); public...

    Susceptible.java interface Susceptible { public boolean infect(Disease disease); public void forceInfection(Disease disease); public Disease getCurrentDisease(); public void setImmune(); public boolean isImmune(); public Point getPosition(); } Movable.java interface Movable { public void move(int step); } public class Point { private int xCoordinate; private int yCoordinate; /** * Creates a point at the origin (0,0) and colour set to black */ public Point(){ xCoordinate = 0; yCoordinate = 0; } /** * Creates a new point at a given coordinate and colour...

  • P9.17 (For Java, and can you please explain as well.) Declare an interface Filter as follows:...

    P9.17 (For Java, and can you please explain as well.) Declare an interface Filter as follows: public interface Filter { boolean accept(Object x); } Write a method: public static ArrayList collectAll(ArrayList objects, Filter f) that returns all objects in the objects list that are accepted by the given filter. Provide a class ShortWordFilter whose filter method accepts all strings of length < 5. Then write a program that asks the user for input and output textfile names, reads all words...

  • Implement a class Portfolio. This class has two objects, checking and savings, of the type BankAccount....

    Implement a class Portfolio. This class has two objects, checking and savings, of the type BankAccount. Implement four methods: • public void deposit(double amount, String account) • public void withdraw(double amount, String account) • public void transfer(double amount, String account) • public double getBalance(String account) Here the account string is "S" or "C". For the deposit or withdrawal, it indicates which account is affected. For a transfer, it indicates the account from which the money is taken; the money is...

  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

  • I need to update the code listed below to meet the criteria listed on the bottom....

    I need to update the code listed below to meet the criteria listed on the bottom. I worked on it a little bit but I could still use some help/corrections! import java.util.Scanner; public class BankAccount { private int accountNo; private double balance; private String lastName; private String firstName; public BankAccount(String lname, String fname, int acctNo) { lastName = lname; firstName = fname; accountNo = acctNo; balance = 0.0; } public void deposit(int acctNo, double amount) { // This method is...

  • MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture...

    MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture an output. polymorphism. Write an application that creates a vector of Account pointers to two SavingsAccount and two CheckingAccountobjects. For each Account in the vector, allow the user to specify an amount of money to withdraw from the Account using member function debit and an amount of money to deposit into the Account using member function credit. As you process each Account, determine its...

  • need basic default constructor , and shoed an example too s0. write the class Battery and...

    need basic default constructor , and shoed an example too s0. write the class Battery and it has s1. a data for the battery capacity (that is similar to myBalance of the BankAccount class.) s2. a parameterized constructor: public Battery(double amount) s3. a drain mutator s4. a change mutator s5. a getRemainingCapacity accessor Need to run this tester - public class BatteryTester{ public static void main(String[] args) { Battery autoBattery = new Battery(2000); //call default constructor System.out.println("1. battery capacity is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT