该程序包含
Person类
属性:String name,boolean gender String birthdate。
构造方法:
-
无参构造方法(初始化名字为空字符串,gender为false)
-
两个参数的构造方法(name,gender)
-
三个参数的构造方法(name,gender,birthdate)
方法:getter/setter方法。
自定义接口Judgeable
方法:boolean judge(String)
main类
静态方法:int countPerson(List,Judgeable),功能为调用List集合中所有元素的judge方法,并统计其中返回true的元素个数。
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
注意:输入中的null表示该行所代表的对象的name属性为null。
输出样例:
Number of person with nameLength == 5:3
Number of person with nameLength == 7:2
Number of person with null name:3
**注意:**样例输出不包含检验代码输出,提交时请务必添加检验代码。
代码:
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() {
}
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;
}
}
//在这里给出Judgeable接口的定义,入参为String,返回boolean。
interface Judgeable {
boolean judge(String s);
static int countPerson(List<Person> personList, Judgeable judger) {
return 0;
}
}
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;
//在这里输入个数
int n = sc.nextInt();
//循环创建person对象存入personList最后
for (int i = 0; i < n; i ++ ) {
name = sc.next();
if (name.equals("null")) {
name = "";
}
gender = sc.nextBoolean();
birthDate = sc.next();
Person person = new Person(name, gender, birthDate);
personList.add(person);
}
//注意:当name输入为null时,将其置为空
int nameLength5 = countPerson(personList, new Judgeable() {
@Override
public boolean judge(String s) {
return s.length() == 5;
}
//Judgeable的实现代码
});
System.out.println("Number of person with nameLength == 5:"+nameLength5);
int nameLength7 = countPerson(personList, new Judgeable() {
@Override
public boolean judge(String s) {
return s.length() == 7;
}
//Judgeable的实现代码
});
System.out.println("Number of person with nameLength == 7:"+nameLength7);
int nameisnull = countPerson(personList, new Judgeable() {
@Override
public boolean judge(String s) {
return s.isEmpty();
}
//Judgeable的实现代码
});
System.out.println("Number of person with null name:"+nameisnull);
}
}