Java习题3:简单定义一个团队类

这篇博客展示了如何使用Java实现一个名为`Team`的类,该类包含了教师、研究生和本科生对象。`Team`类具有构造方法、成员变量以及用于添加指导老师和学生的方法。此外,还提供了输出团队信息和指导项目的功能。在测试类`TeamTest`中,创建了一个团队实例,并进行了成员添加和信息输出的演示。
摘要由CSDN通过智能技术生成

问题描述:定义一个类:团队(Team),该类中包含一些其它类的对象(教师,研究生,本科生等),为该类定义一个有参的构造方法及一些方法来描述团队的行为。

代码实现如下:

Team类(团队,团队中包括教师,研究生,本科生):
package experiment2;

public class Team {
	private String name;//团队名称
	private int max;//团队人数
	private String pName;//项目名称
	private Teacher teacher;//指导老师
	private GraduateStudent[] gStudents;//研究生数组
	private UnderGStudent[] uGStudents; //本科生数组
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setMax(int max) {
		this.max = max;
	}
	public int getMax() {
		return max;
	}
	public String getpName() {
		return pName;
	}
	public void setpName(String pName) {
		this.pName = pName;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	public GraduateStudent[] getgStudents() {
		return gStudents;
	}
	public void setgStudents(GraduateStudent[] gStudents) {
		this.gStudents = gStudents;
	}
	public UnderGStudent[] getuGStudents() {
		return uGStudents;
	}
	public void setuGStudenst(UnderGStudent[] uGStudents) {
		this.uGStudents = uGStudents;
	}
	public Team(String name,int max,String pName) {
		this.setpName(pName);
		this.setName(name);
		this.setMax(max);
		gStudents = new GraduateStudent[max/2]; 
		uGStudents = new UnderGStudent[max-max/2];
	}
	//指导老师加入团队
	public void tInTeam(Teacher tTeacher){
		teacher = tTeacher;
        System.out.println("指导老师" + teacher.getName() + "成功加入" + getName()+"团队" );
    }
	//研究生加入团队
	public void gSInTeam(GraduateStudent gStudent){
    	for(int i=0;i<gStudents.length;i++) {
    		if(gStudents[i]==null) {
    			gStudents[i]=gStudent;
                System.out.println("研究生" + gStudent.getName() + "成功加入" + getName()+"团队" );
                break;
    		}
    		if(i==gStudents.length-1) {
    			System.out.println( "研究生" + gStudent.getName() + "加入失败," +getName()+"团队研究生组已满员");
    		}
    	}
    }
	//本科生加入团队
	public void uGSInTeam(UnderGStudent uGStudent) {
    	for(int i=0;i<uGStudents.length;i++) {
    		if(uGStudents[i]==null) {
    			uGStudents[i]=uGStudent;
                System.out.println("本科生" + uGStudent.getName() + "成功加入" + getName()+"团队" );
                break;
    		}
    		if(i==uGStudents.length-1) {
    			System.out.println( "本科生" + uGStudent.getName()+"加入失败," + getName()+"团队本科生组已满员");
    		}
    	}
	}
	//输出团队信息
	public void outputInfo() {
		System.out.println("团队名称:" + getName() + " 指导老师:" + getTeacher().getName() + " 项目名称:" + getpName());
		System.out.println("研究生成员:");
		for(int i = 0;i < gStudents.length;i++){
		    if(gStudents[i] != null)
		    System.out.printf(gStudents[i].getName()+" ");
		}
		System.out.println();
		System.out.println("本科生成员:");
		for(int i = 0;i < uGStudents.length;i++)
		{
		    if(uGStudents[i] != null)
		        System.out.printf(uGStudents[i].getName()+" ");
		}
		System.out.println();
	}
	//指导老师指导项目
	public void guideProject() {
		System.out.println("指导老师" + getTeacher().getName() + "正在指导" + getpName()+ "团队项目。");
	}
}

Teacher类(指导老师):
package experiment2;

public class Teacher {
	private String name;
	private String sex;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Teacher(String name,String sex,int age) {
		this.setName(name);
		this.setSex(sex);
		this.setAge(age);
	}
}

GraduateStudent类(研究生):
package experiment2;

public class GraduateStudent {
	private String name;
	private String sex;
	private int age;
	private String eduBack;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getEduBack() {
		return eduBack;
	}
	public void setEduBack(String eduBack) {
		this.eduBack = eduBack;
	}
	public GraduateStudent(String name,String sex,int age,String eduBack) {
		this.setName(name);
		this.setSex(sex);
		this.setAge(age);
		this.setEduBack(eduBack);
	}
}

UnderGStudent类(本科生):
package experiment2;

public class UnderGStudent {
	private String name;
	private String sex;
	private int age;
	private String eduBack;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getEduBack() {
		return eduBack;
	}
	public void setEduBack(String eduBack) {
		this.eduBack = eduBack;
	}
	public UnderGStudent(String name,String sex,int age,String eduBack) {
		this.setName(name);
		this.setSex(sex);
		this.setAge(age);
		this.setEduBack(eduBack);
	}
}

TeamTest类(测试类):
package experiment2;

public class TeamTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Teacher teacher = new Teacher("杨斌","男",30);
		GraduateStudent gStudent1 = new GraduateStudent("李超","男",23,"研究生");
		GraduateStudent gStudent2 = new GraduateStudent("刘丽","女",22,"研究生");
		UnderGStudent uGStudent1 = new UnderGStudent("张三","男",21,"本科生");
		UnderGStudent uGStudent2 = new UnderGStudent("李颖","女",20,"本科生");
		UnderGStudent uGStudent3 = new UnderGStudent("姚磊","男",20,"本科生");
		UnderGStudent uGStudent4 = new UnderGStudent("刘芳","女",21,"本科生");
		Team team = new Team("题都做不队",5,"不知道是什么");
		team.tInTeam(teacher);
		team.gSInTeam(gStudent1);
		team.gSInTeam(gStudent2);
		team.uGSInTeam(uGStudent1);
		team.uGSInTeam(uGStudent2);
		team.uGSInTeam(uGStudent3);
		team.uGSInTeam(uGStudent4);
		team.outputInfo();
		team.guideProject();
	}
}

查看输出结果:

在这里插入图片描述

到此,符合题目条件的一个简单的 类就写好了。

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值