首先,根据题目要求,我们需要创建一个Person类,包含属性name、gender和birthdate,并实现构造方法和getter/setter方法。接着,我们需要定义一个接口Judgeable,并实现其中的judge方法。最后,在main方法中完成对Person对象的创建和集合的操作。

下面是完整的程序代码:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Person {
    private String name;
    private boolean gender;
    private String birthdate;

    public Person() {
        this.name = "";
        this.gender = false;
    }

    public Person(String name, boolean gender) {
        this.name = name;
        this.gender = gender;
    }

    public Person(String name, boolean gender, String birthdate) {
        this.name = name;
        this.gender = gender;
        this.birthdate = birthdate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isGender() {
        return gender;
    }

    public void setGender(boolean gender) {
        this.gender = gender;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }
}

interface Judgeable {
    boolean judge(String str);
}

public class Main {
    public static int countPerson(List<Person> personList, Judgeable judgeable) {
        int count = 0;
        for (Person person : personList) {
            if (judgeable.judge(person.getName())) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        List<Person> personList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String name = scanner.nextLine();
            boolean gender = scanner.nextBoolean();
            scanner.nextLine();
            String birthdate = scanner.nextLine();
            personList.add(new Person(name, gender, birthdate));
        }
        scanner.close();

        int count5 = countPerson(personList, str -> str.length() == 5);
        int count7 = countPerson(personList, str -> str.length() == 7);
        int countEmpty = countPerson(personList, String::isEmpty);

        System.out.printf("name长度为5的个数:%d\n", count5);
        System.out.printf("name长度为7的个数:%d\n", count7);
        System.out.printf("name为空的个数:%d\n", countEmpty);
    }
}

输入格式:

  • 输入一个整数n,表示集合中Person对象的个数。
  • 根据n的值,依次输入n行的name、gender和birthDate值。

输出格式:

  • 输出name长度为5的个数。
  • 输出name长度为7的个数。
  • 输出name为空的个数。

输入样例:

5
Alice
true
1990-01-01
Bob
false
1995-05-05
Charlie
false
1987-07-07
David
true
1992-12-12
Eve
true
1985-03-03

输出样例:

name长度为5的个数:5
name长度为7的个数:0
name为空的个数:0
``
该程序包含Person类属性:String nameboolean gender String birthdate。构造方法: 无参构造方法初始化名字为空字符串gender为false 两个参数的构造方法namegender 三个参数的构造方法namegenderbirthdate方法:gettersetter方法。自定义接口Judgeable方法:boolean judgeSt

原文地址: http://www.cveoy.top/t/topic/hFyB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录