Created
March 13, 2012 05:18
-
-
Save jimmykurian/2026963 to your computer and use it in GitHub Desktop.
A modified implementation of the DataSet class to use both a Measurer and a Filter object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//BankAccount.java - Jimmy Kurian | |
public class BankAccount | |
{ | |
private double balance; | |
public BankAccount() | |
{ | |
balance = 0; | |
} | |
public BankAccount(double initialBalance) | |
{ | |
balance = initialBalance; | |
} | |
public void deposit(double amount) | |
{ | |
double newBalance = balance + amount; | |
balance = newBalance; | |
} | |
public void withdraw(double amount) | |
{ | |
double newBalance = balance - amount; | |
balance = newBalance; | |
} | |
public double getBalance() | |
{ | |
return balance; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//DataSet.java - Jimmy Kurian | |
public class DataSet | |
{ | |
private double sum; | |
private Object maximum; | |
private int count; | |
private Measurer measurer; | |
private Filter filter; | |
public DataSet(Measurer aMeasurer, Filter aFilter) | |
{ | |
sum = 0; | |
count = 0; | |
maximum = null; | |
measurer = aMeasurer; | |
filter = aFilter; | |
} | |
public void add(Object x) | |
{ | |
if (filter.accept(x)) | |
{ | |
sum = sum + measurer.measure(x); | |
if (count == 0 | |
|| measurer.measure(maximum) < measurer.measure(x)) | |
maximum = x; | |
count++; | |
} | |
} | |
public double getAverage() | |
{ | |
if (count == 0) return 0; | |
else return sum / count; | |
} | |
public Object getMaximum() | |
{ | |
return maximum; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Filter.java - Jimmy Kurian | |
public interface Filter | |
{ | |
boolean accept(Object x); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Measurer.java - Jimmy Kurian | |
public interface Measurer | |
{ | |
double measure(Object anObject); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//MeasurerFilterTester.java - Jimmy Kurian | |
public class MeasurerFilterTester | |
{ | |
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(5000)); | |
System.out.println("Average balance= " + data.getAverage()); | |
BankAccount b = (BankAccount) data.getMaximum(); | |
double balance = b.getBalance(); | |
System.out.println("Highest balance= " + balance); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment