I'm trying to do an exercise from my book involving interfaces and it seems really complicated to me because I am new to said topic. I was wondering if anyone could tell me what I am doing wrong in my code.

The exercise consists of modifying a class called DataSet that has already been given to me and it has to use objects of two interfaces: Measurer and Filter. The measurer interface was already done. Here are the entire exercise instructions:

P 9.12
Declare an interface Filter as follows:
package Problem12Filter;
 
public interface Filter 
{
	boolean accept(Object x);
}

Modify the implementation of the DataSet class in section 9.4 to use both a Measurer and a Filter Object. Only objects that the filter accepts should be processed. Demonstrate your modification by having a data set process a collection of BankAccounts, filtering out all accounts with balance less than $1000.

How do I implement that Filter interface in a way that it filters out all accounts with less than $1000?

Here are the original classes I have to start off with:

DataSet:

/**
 * Computes the average of a set of data values.
 * @author Jean
 *
 */
 
public class DataSet 
{
	private double sum;
	private Object maximum;
	private int count;
	private Measurer measurer;
 
	/**
	 * Constructs an empty data set with a given measurer.
	 * @param aMeasurer the measurer that is used to measure data values.
	 */
 
	public DataSet(Measurer aMeasurer)
	{
		sum = 0;
		count = 0;
		maximum = null;
		measurer = aMeasurer;
	}
 
	/**
	 * Adds a data value to the data set.
	 * @param x a data value.
	 */
 
	public void add(Object x)
	{
		sum = sum + measurer.measure(x);
		if(count == 0 || measurer.measure(maximum) < measurer.measure(x))
		{
			maximum = x;
		}
		count++;
	}
 
	/**
	 * Gets the average of the added data.
	 * @return the average or 0 if no data has been added.
	 */
	public double getAverage()
	{
		if(count == 0)
		{
			return 0;
		}
		else 
		{
			return sum/count;
		}
	}
 
	/**
	 * Gets the larger of the added data.
	 * @return the maximum or 0 if no data has been added.
	 */
	public Object getMaximum()
	{
		return maximum;
	}
}


Measurer:

/*
 * 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);
 
}

Bank Account:
/** 
 A bank account class 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;
	}
}



---------------------------------------------------------

Here are my modified classes:


BankAccount:

package Problem12Filter;
 
/** 
A bank account class has a balance that can be changed by
deposits and withdrawals. 
*/
public class BankAccount implements Measurer, Filter
{
	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;
	}
 
	public boolean accept(Object x) 
	{
		if (balance > 1000)
		{
		return true;
		}
 
		else
		{
			return false;
		}
	}
 
	@Override
	public double getMeasure(Object x) 
	{
		return balance;
	}
 
 
}


DataSet:

package Problem12Filter;
 
/**
 * Computes the average of a set of data values.
 * @author Jean
 *
 */
 
public class DataSet 
{
	private double sum;
	private Object maximum;
	private Object maximum2;
	private int count;
	private Measurer measurer;
	private Filter filter;
 
	/**
	 * Constructs an empty data set with a given measurer.
	 * @param aMeasurer the measurer that is used to measure data values.
	 */
 
	public DataSet(Measurer aMeasurer)
	{
		sum = 0;
		count = 0;
		maximum = null;
		measurer = aMeasurer;
 
	}
 
	/**
	 * Adds a data value to the data set.
	 * @param x a data value.
	 */
 
	public void add(Object x)
	{
		sum = sum + measurer.getMeasure(x);
 
		if(count == 0 || measurer.getMeasure(maximum) < measurer.getMeasure(x))
		{
			maximum = x;
		}
 
		if(filter.accept(maximum) == true)
		{
			maximum2 = maximum; 
		}
 
		else
		{
			System.out.println("Does not apply");
		}
 
		count++;
	}
 
	/**
	 * Gets the average of the added data.
	 * @return the average or 0 if no data has been added.
	 */
	public double getAverage()
	{
		if(count == 0)
		{
			return 0;
		}
		else 
		{
			return sum/count;
		}
	}
 
	/**
	 * Gets the larger of the added data.
	 * @return the maximum or 0 if no data has been added.
	 */
	public Object getMaximum()
	{
		return maximum2;
	}
}

Filter:

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

Measurer:

package Problem12Filter;
 
/*
 * 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 getMeasure(Object x);
 
}


Tester:

package Problem12Filter;
 
public class Tester
{
	public static void main(String[] args)
	{
		Measurer m = new BankAccount(1001);
 
		DataSet data = new DataSet(m);
 
		data.add(new BankAccount(500));
		data.add(new BankAccount(3000));
 
		System.out.println(data.toString());
 
	}
 
 
}



Any help at all would be appreciated. If you can't help, then could anyone explain to me why I need those instance vars Measurer and Filter inside the DataSet class? I still don't understand why I need those very well.

Thanks.