0% found this document useful (0 votes)
33 views5 pages

FilteringDataSummary PDF

This document discusses Java interfaces and how they can be used to filter data. It defines a Filter interface with a satisfies method and a MinMagFilter class that implements the Filter interface. The filter method takes an ArrayList of data and Filter as parameters and returns a filtered ArrayList by checking each element against the Filter's satisfies method. This allows different Filter implementations to be used to filter data in a standardized way.

Uploaded by

Mouloud HAOUAS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

FilteringDataSummary PDF

This document discusses Java interfaces and how they can be used to filter data. It defines a Filter interface with a satisfies method and a MinMagFilter class that implements the Filter interface. The filter method takes an ArrayList of data and Filter as parameters and returns a filtered ArrayList by checking each element against the Filter's satisfies method. This allows different Filter implementations to be used to filter data in a standardized way.

Uploaded by

Mouloud HAOUAS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Filtering Data

Summary
Interface Summary
public interface Filter {

public boolean satisfies(QuakeEntry qe);

• Defining Interfaces :
• Specify methods classes must have
Interface Summary
public class MinMagFilter implements Filter {
private double magMin;
public MinMagFilter(double min) {
magMin = min;
}
public boolean satisfies(QuakeEntry qe) {
return qe.getMagnitude() >= magMin;
}
}
• Implementing Interfaces:
• Write class with “implements InterfaceName”
• Define all required methods
Interface Summary
public ArrayList<QuakeEntry>
filter(ArrayList<QuakeEntry> quakeData,
Filter f) {
ArrayList<QuakeEntry> answer
= new ArrayList<QuakeEntry>();
for(QuakeEntry qe : quakeData) {
if (f.satisfies(qe)) {
answer.add(qe);
}
}
return answer;
}
• Using Interface Types:
• Can use interface name as type
• Can call methods in the interface
Interface Summary

Filter f = new MinMagFilter(4.0);


ArrayList<QuakeEntry> largeQuakes
= filter(list, f);

• Type compatibility: use class as interface


• Will still use methods in class: dynamic dispatch

You might also like