HELP ME PLEASE. I AM A 1ST YR COLLEGE STUDENT :/

Define an interface Filter as follows:
public interface Filter
{
boolean accept(Object x);
}
Modify the implementation of the DataSet class 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.
Refer to the source code below:

``````/**
Computes the average of a set of data values.
*/
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 xa data value
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 largest of the added data.
//@return the maximum or 0 if no data has been added
public Object getMaximum(){
return maximum;
}
}``````

not sure if I understand you correctly, but it looks to me like your DataSet class must implement your Filter interface and it's methods.
as for polymorphism, if your DataSet class implements the Filter interface, it will always pas an 'is a Filter' test.

By analogy with interfaces like Comparator, I would interpret this as follows:
Create a class (eg "BalanceFilter") that implements Filter, and in its accept(...) method only accepts accounts with balances > some arbitrary threshold value. Then in the DataSet class instantiate a BalanceFilter with threshold value of 1000, and use …

let me guess ... it says it can't find the geMeasure() method?
well, as far as I know, the Object class doesn't contain such a method, and your maximum is an instance of Object.

## All 12 Replies

not sure if I understand you correctly, but it looks to me like your DataSet class must implement your Filter interface and it's methods.
as for polymorphism, if your DataSet class implements the Filter interface, it will always pas an 'is a Filter' test.

By analogy with interfaces like Comparator, I would interpret this as follows:
Create a class (eg "BalanceFilter") that implements Filter, and in its accept(...) method only accepts accounts with balances > some arbitrary threshold value. Then in the DataSet class instantiate a BalanceFilter with threshold value of 1000, and use that instance to filter out the low-balance accounts.

Oh sorry, I forgot to include this one.

``````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);
}``````

I have already figured it out.

``````package pkginterface;

public class DataSet {

private double sum;
private Object maximum;
private int count;
private Measurer measurer;

if (m.accept(m)) {
this.sum += m.getMeasure();
if (maximum == null) {
this.maximum = m;
}
if (m.getMeasure() > this.maximum.getMax()){
this.maximum = m;
}
count++;
}
}

public int getCount() {
return count;
}

public Filter getMax() {
return (Filter) maximum;
}

public double getSum() {
return sum;
}
}``````

But I got a problem with this line.

if (m.getMeasure() > this.maximum.getMeasure()){

let me guess ... it says it can't find the geMeasure() method?
well, as far as I know, the Object class doesn't contain such a method, and your maximum is an instance of Object.

let me guess ... it says it can't find the geMeasure() method?
well, as far as I know, the Object class doesn't contain such a method, and your maximum is an instance of Object.

Yah :/ What should I do? :3

your maximum should not be an instance of Object, but of one of your own classes (the same as m, I think .. haven't looked at your entire code, just looks to me that way based on the last code you posted)

Here's my code :3

``````public interface Filter {

boolean accept(Object x);
double getMeasure();

}``````
``````public class BankAccount implements Filter {
private double balance;

public BankAccount(double init) {
this.balance = init;
}

public double getMeasure(){
return balance;
}

public boolean accept(Object x){
BankAccount temp = (BankAccount)x;

if(temp.balance > 1000){
return true;
}

return false;
}
}``````
``````public class DataSet {

private double sum;
private Object maximum;
private int count;
private Measurer measurer;

if (m.accept(m)) {
this.sum += m.getMeasure();
if (maximum == null) {
this.maximum = m;
}
if (m.getMeasure() > this.maximum.getMax()){
this.maximum = m;
}
count++;
}
}

public int getCount() {
return count;
}

public Filter getMax() {
return (Filter) maximum;
}

public double getSum() {
return sum;
}
}``````
``````public class DataSetTest {

public static void main(String[] args){

DataSet ds = new DataSet();
System.out.println("Count = " + ds.getCount());
System.out.println("Max Amount = " + ((BankAccount)ds.getMax()).getMeasure());
System.out.println("Sum = " + ds.getSum());

}

}``````
``````public interface Measurer{
//Computes the measure of an object.
//@param anObject the object to be measured
//return the measure
double measure(Object anObject);
}``````

I'll check it at home, but I'm almost leaving here.

I think your problem is the getMax() and such.
you (almost) never should use instances of Object, always classes that extend from Object (which means any class :) )

Thank you. I'll try to work it out @.@

``````package pkginterface;

public class DataSet {

private double sum;
private Filter maximum;
private int count;
private Measurer measurer;

if (x.accept(x)) {
this.sum += x.getMeasure();
if (maximum == null) {
this.maximum = x;
}
if (x.getMeasure() > this.maximum.getMeasure()){
this.maximum = x;
}
count++;
}
}
public int getCount(){
return count;
}
public double getAverage() {
if (count == 0) {
return 0;
} else {
return sum / count;
}
}

public Filter getMaximum() {
return maximum;
}

public double getSum() {
return sum;
}
}``````

I guess I'm done. Thank you so much for your help. I really appreciate it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.