Explore BrainMass
Share

Java Practice

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

Declare an interface Filter as follows:

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

Modify the implementation of the DataSet class in Section 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 bank accounts, filtering out all accounts with balances less than $1,000.

You need to supply the following class in your solution:
DataSet

Use the following class as your tester class:
/**
This program tests the use of a Measurer and a Filter.
*/
public class DataSetTester
{
public static void main(String[] args)
{
class BankMeasurer implements Measurer
{
public double measure(Object anObject)
{
BankAccount ba = (BankAccount) anObject;
return ba.getBalance();
}
}

class BankFilter implements Filter
{
public boolean accept(Object x)
{
BankAccount ba = (BankAccount) x;
return ba.getBalance() > 1000;
}
}

Measurer m = new BankMeasurer();
Filter f = new BankFilter();
DataSet data = new DataSet(m, f);

data.add(new BankAccount(1));
data.add(new BankAccount(100));
data.add(new BankAccount(2000));
data.add(new BankAccount(950));
data.add(new BankAccount(4000));

System.out.println("Average balance: " + data.getAverage());
System.out.println("Expected: 3000");

BankAccount b = (BankAccount) data.getMaximum();
double balance = b.getBalance();
System.out.println("Highest balance: " + balance);
System.out.println("Expected: 4000");
}
}

DO NOT MODIFY THE FOLLOWING
/**
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)
{
double newBalance = balance + amount;
balance = newBalance;
}

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

/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
}
DO NOT MODIFY THE FOLLOWING
/**
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);
}

DO NOT MODIFY THE FOLLOWING

public interface Filter
{
/**
Determines whether to accept an object.
@param x the object to be filtered
@return true to accept an object, false otherwise
*/
boolean accept(Object x);
}
DO NOT MODIFY THE FOLLOWING

/**
This program tests the use of a Measurer and a Filter.
*/
public class DataSetTester
{
public static void main(String[] args)
{
class BankMeasurer implements Measurer
{
public double measure(Object anObject)
{
BankAccount ba = (BankAccount) anObject;
return ba.getBalance();
}
}

class BankFilter implements Filter
{
public boolean accept(Object x)
{
BankAccount ba = (BankAccount) x;
return ba.getBalance() > 1000;
}
}

Measurer m = new BankMeasurer();
Filter f = new BankFilter();
DataSet data = new DataSet(m, f);

data.add(new BankAccount(1));
data.add(new BankAccount(100));
data.add(new BankAccount(2000));
data.add(new BankAccount(950));
data.add(new BankAccount(4000));

System.out.println("Average balance: " + data.getAverage());
System.out.println("Expected: 3000.0");

BankAccount b = (BankAccount) data.getMaximum();
double balance = b.getBalance();
System.out.println("Highest balance: " + balance);
System.out.println("Expected: 4000.0");
}
}

© BrainMass Inc. brainmass.com October 25, 2018, 9:27 am ad1c9bdddf
https://brainmass.com/computer-science/java/java-practice-574849

Solution Summary

A java code for filter and measure interfaces.

$2.19
See Also This Related BrainMass Solution

Commenting on the enclosed Java Code

I need assistance in commenting on the enclosed code, consider things like code layout, use of variable names, general readability and understandability.

The enclosed program suppose to Implement a class called ShortAddress that has the following attributes: firstName, secondName, and phoneNumber. Also, implement a class called FullAddress that inherits the above attributes while adding the attributes (int) houseNumber (simply 1, 2, ... etc. - i.e. no 1a or 3b's allowed), street1Name, street2Name and cityName. We are not suppose to forget to use the keyword super in the constructor. The application class to allow the user to input an address details and then give the user the choice of viewing the short address details or the long address details. This means the final answer should have a minimum of 3 classes for the application - We may use more if we are including utility classes. Further we are suppose to use Use exception handling to make the program robust.

View Full Posting Details