In: Computer Science
from Big Java Early Objects 7th edition
Question
Declare an interface Filter as follows:
public interface Filter { boolean accept(Object x); }
Modify the implementation of the Data 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.
Solve Exercise •• P10.6, using a lambda expression for the
filter.
How would you use a use a lambda expression for the filter
class?????
below is working code for the first part of the question,
thanks.
*******************************************************************************
Filter.java <- needs to be lambda expression
public interface Filter
{
// this is what sifts through the -1000 and +1000 accounts
boolean accept(Object anObject);
}
below classes for
*******************************************************************************
BalanceFilter.java
public class BalanceFilter implements Filter
{
@Override
public boolean accept(Object object)
{
return ((BankAccount) object).getBalance() >= 1000;
}
}
*******************************************************************************
Measurer .java
public interface Measurer
{
double measure(Object anObject);
}
*******************************************************************************
public class BankAccount
{
private double balance;
public BankAccount()
{
balance = 0;
}
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
public void deposit(double amount)
{
balance = balance + amount;
}
public void withdraw(double amount)
{
balance = balance - amount;
}
public double getBalance()
{
return balance;
}
public void addIntrest(double rate)
{
balance = balance + balance * rate/100;
}
}
*******************************************************************************
DataSet.java
public class DataSet
{
public static double average(Object[] objects, Measurer meas,
Filter filt)
{
double sum = 0;
double avg = 0;
int count = 0;
for (Object ob : objects)
{
if (filt.accept(ob))
{
sum += meas.measure(ob);
count++;
}
}
if (count > 0)
{
avg = (double)sum/count;
}
return avg;
}
public static void main (String[] args)
{
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),
};
BalanceMeasurer measurer = new BalanceMeasurer();
BalanceFilter filter = new BalanceFilter();
double avg = average(accounts, measurer, filter);
System.out.println("Average for bank accounts with 1000.00 in
collateral is "+avg);
}
}
*******************************************************************************
BalanceMeasurer.java
public class BalanceMeasurer implements Measurer
{
public double measure(Object object)
{
return ((BankAccount) object).getBalance();
}
}
In the below code i have put commnets for the code for the lambda expression for filter
(all the remaining code remains same). Only DataSet.java, Filter.java has the changes.
========================
CODE
======================
DataSet.java
public class DataSet {
public static double average(Object[] objects,
Measurer meas, Filter filt) {
double sum = 0;
double avg = 0;
int count = 0;
for (Object ob : objects)
{
if
(filt.accept(ob)) {
sum += meas.measure(ob);
count++;
}
}
if (count > 0) {
avg = (double)
sum / count;
}
return avg;
}
// main method
public static void main(String[] args) {
// Bank accounts creation
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), };
BalanceMeasurer measurer = new
BalanceMeasurer();
// previously we use the
BalanceFilter class which implements the filter
// Now below is the lambda
expression which implements for the single method from filter
interface
Filter filter = (object) ->
{
// returns the
boolean if the balance is greater than the 1000
return
((BankAccount) object).getBalance() >= 1000;
};
// calling the average method with
filter
double avg = average(accounts,
measurer, filter);
System.out.println("Average for
bank accounts with 1000.00 in collateral is " + avg);
}
}
=================================
Filter.java
// Annotate the interface to be functional interface
@FunctionalInterface
public interface Filter {
// this is what sifts through the -1000 and +1000 accounts
boolean accept(Object anObject);
}
===================================
Measurer.java
public interface Measurer
{
double measure(Object anObject);
}
===================================
BankAccount.java
public class BankAccount {
private double balance;
public BankAccount() {
balance = 0;
}
public BankAccount(double initialBalance) {
balance = initialBalance;
}
public void deposit(double amount) {
balance = balance + amount;
}
public void withdraw(double amount) {
balance = balance - amount;
}
public double getBalance() {
return balance;
}
public void addIntrest(double rate) {
balance = balance + balance * rate
/ 100;
}
}
========================================
BalanceMeasurer.java
public class BalanceMeasurer implements Measurer {
public double measure(Object object) {
return ((BankAccount)
object).getBalance();
}
}
========================================
output
========================================