5-2 jmu-java-m05-自定义Judgeable接口 (10分)

本文介绍了一个Java程序,定义了Person类和Judgeable接口。Person类包含姓名、性别和出生日期属性,而Judgeable接口包含一个判断方法。主程序中,创建Person对象集合,然后使用Judgeable接口的静态方法countPerson来统计集合中符合特定条件(name长度为5、7或为空)的对象数量,并输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值