【问题标题】:Two different implementations两种不同的实现
【发布时间】: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


    【解决方案1】:

    要实现两种类型的过滤行,策略设计模式将帮助你这是一篇好文章http://www.oodesign.com/strategy-pattern.html 我认为你必须像

      public ArrayList filterRaw(ArrayList groups)
    {
        ArrayList pass = new ArrayList();
        for (int i = 0; i < groups.size(); i++)
        {
            if ((Integer)groups.get(i) >= 50) pass.add(groups.get(i));
    
        }
        return pass;
    }
    

    【讨论】:

      【解决方案2】:

      您的项目的核心是一个 Main.class,其中包含将要执行的所有内容。其他类仅被声明,但从未使用过。当您运行程序时,它以 main 方法中的命令开始行。目前,您唯一拥有的就是打印到带有文本的控制台字符串:“-- Array-Listen und Generics --”

      现在您需要准备一些您将过滤的数据,例如

      Student student1 = new Student("John", 1);
      student1.setPoints(51);
      Student student2 = new Student("Alice", 2);
      student1.setPoints(48);
      
      List<Student> studentList = new ArrayList<>();
      studentList.add(student1, student2);
      

      假设您有测试数据,现在您需要使用过滤器 要使用您的过滤器,我们需要使用它的实现

      IFilter myFilter = FilterFactory.create();
      

      现在 myFilter 是您的实现的一个实例,要使用它,您需要调用带有声明数据的方法

      List<Student> result = myFilter.filterRaw(studentList);
      

      该方法的示例实现是

      public List<Student> filteredStudents(List<Student> input) {
          return input.stream().filter(s -> s.getPoints() > 50).collect(Collectors.toList());
      }
      

      但你可能会注意到,它需要更改接口的声明才能具有相同的类型。

      【讨论】:

        最近更新 更多