5-2 jmu-java-m05-自定义Judgeable接口 (10分)
该程序包含
Person类:
属性:String name,boolean gender String birthdate。
构造方法:无参构造方法(初始化名字为空字符串,gender为false)
(name,gender)
(name,gender,birthdate)
方法:getter/setter方法。
自定义接口Judgeable
方法:boolean judge(String)
静态方法:int countPerson(List,Judgeable):实现判断List集合中符合Judge方法的集合元素,并返回其个数
main方法:
定义一个Person类集合,输入个数n,在下面依次输入n行的name,gender,biryhDate值生成person对象并添加进集合。
判断集合中name长度为5的数的个数,并输出,具体格式见输出样例。
判断集合中name长度为7的数的个数,并输出,具体格式见输出样例。
判断集合中name为空的个数,并输出,具体格式见输出样例。
输入格式:
输入一个整数number
根据number,输入number行的name,gender,birthDate
参考代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Person{
//在这里按要求定义person类
}
//在这里给出Judgeable接口的定义,入参为String,返回boolean。
public class Main {
public static int countPerson(List<Person> personList, Judgeable judger) {
int n = 0;
for (Person person : personList) {
if (judger.judge(person.getName()))
n++;
}
return n;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Person> personList = new ArrayList<Person>();
String name;
boolean gender;
String birthDate;
//在这里输入个数
//循环创建person对象存入personList最后
//注意:当name输入为null时,将其置为空
int nameLength5 = countPerson(personList, new Judgeable() {
//Judgeable的实现代码
});
System.out.println("Number of person with nameLength == 5:"+nameLength5);
int nameLength7 = countPerson(personList, new Judgeable() {
//Judgeable的实现代码
});
System.out.println("Number of person with nameLength == 7:"+nameLength7);
int nameisnull = countPerson(personList, new Judgeable() {
//Judgeable的实现代码
});
System.out.println("Number of person with null name:"+nameisnull);
}
}
输出格式:
name长度为5的名字的个数
name长度为7的名字的个数
name为null的个数
输入样例:
9
Aaron false 1999-12-19
Abigale true 1998-09-01
null false 1992-11-21
Bonnie true 1990-11-11
null false 1994-07-08
Carol false 1982-12-04
Celeste true 1987-03-01
null false 1996-04-01
Chloe true 1993-02-16
输出样例:
Number of person with nameLength == 5:3
Number of person with nameLength == 7:2
Number of person with null name:3
注意:样例输出不包含检验代码输出,提交时请务必添加检验代码。
作者
郑如滨
单位
集美大学
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Person{
//在这里按要求定义person类
private String name;
private boolean gender;
private String birthDate;
public Person()
{
}
public Person(String name,boolean gender)
{
this.name=name;
this.gender=gender