【发布时间】:2025-12-27 20:00:06
【问题描述】:
我想做两个不同的过滤器实现,它只返回那些得分至少为 50% 的学生。我的问题是如何正确实现这两种方法,首先我不明白为什么它不起作用,因此我需要你的帮助。
具有两种实现的过滤器类:
package u7a1;
import java.util.ArrayList;
class Filter implements IFilter {
public ArrayList filterRaw(ArrayList groups)
{
ArrayList<Boolean> allstudents = new ArrayList();
ArrayList<Student> students = new ArrayList();
ArrayList pass = new ArrayList();
for (int i = 0; i < groups.size(); i++)
{
if ((Integer)groups.get(i) >= 50) allstudents.add(true);
else allstudents.add(false);
}
for (int i = 0; i < groups.size(); ++i)
{
if ((Boolean)allstudents.get(i) == true) pass.add(groups.get(i));
}
return pass;
}
public ArrayList<Student> filterGeneric(ArrayList<ArrayList<Student>> groups)
{
ArrayList<Boolean> allstudents = new ArrayList();
ArrayList<Student> students = new ArrayList();
ArrayList pass = new ArrayList();
/*for (int i = 0; i < groups.size(); i++)
{
Integer mystudentpoints = new Integer(0);
mystudentpoints = Student.getPoints();
if (Student.getPoints() >= 50) allstudents.add(true);
else allstudents.add(false);
}*/
for (int i = 0; i < groups.size(); ++i)
{
if ((Boolean)allstudents.get(i) == true) pass.add(groups.get(i));
}
return pass;
}
}
过滤器工厂已经正确:
package u7a1;
/**
* Factory for "Testat" filters
*/
public class FilterFactory {
/**
* Create a "Testat" filter
* @return a "Testat" filter
*/
public static IFilter create()
{
// TODO
return new Filter();
}
}
这是由方法无法正常工作的类实现的接口:
package u7a1;
import java.util.ArrayList;
/**
* Filters for students who obtain the "Testat".
*
* The requirements for the testat are to have at least {@link IFilter#criteria} percent
* of {@link IFilter#maxNumberofPoints} points.
*/
public interface IFilter {
public final int maxNumberofPoints = 80;
public final double criteria = 50;
/**
* Filter all students that achieved enough points for the "Testat".
*
* @param groups an ArrayList of groups, where a group is an ArrayList of students
* @return the ArrayList of all students who achieved enough points for the "Testat".
*/
public ArrayList filterRaw(ArrayList groups);
/**
* Filter all students that achieved enough points for the "Testat".
*
* @param groups an ArrayList of groups, where a group is an ArrayList of students
* @return the ArrayList of all students who achieved enough points for the "Testat".
*/
public ArrayList<Student> filterGeneric(ArrayList<ArrayList<Student>> groups);
}
我不会进一步编辑主类:
/**
* Main class of the Java program.
*
*/
public class Main {
public static void main(String[] args) {
System.out.println("-- Array-Listen und Generics --");
/* you can make function calls from here*/
}
}
最后我有了不需要任何更改的 Student 类:
package u7a1;
public class Student {
private String name;
private int legi;
private int points;
public Student(String name, int legi)
{
this.name = name;
this.legi = legi;
points = 0;
}
public int getLegi()
{
return this.legi;
}
public String getName()
{
return this.name;
}
public Student setPoints(int points)
{
this.points = points;
return this;
}
public int getPoints()
{
return points;
}
public String toString()
{
return String.format("%s (%d)", name, legi);
}
}
【问题讨论】:
标签: java class filter main implementation