万能百科  > 所属分类  > 

阅读以下说明和java代码,将应填入(n)处的字句写在对应栏内。[说明] 有若干教师,每个教师只有姓名,

阅读以下说明和java代码,将应填入(n)处的字句写在对应栏内。

[说明]

有若干教师,每个教师只有姓名,一个教师可以指导多名研究生;每名研究生有姓名和研究方向,程序最后输出每个教师指导的所有研究生的姓名和研究方向。

[Java程序]

public class Teacher {

String name;

int top=0;

Student[] student=new Student[20];

public Teacher() {}

public Teacher(String name) { this.name=name;}

boolean add(Student stu){

int len=this.student.length;

if (top<len-1) {

this.student[top]=siu;

(1);

return true;

}

else

return (2);

}

void disp(){

System.out.println(“指导老师"”+this.name);

System.out.println(“研究生:”);

for(int i=0;i<(3);i++){

System.out.println(“姓名:”+this.student[i].name+“\t研究方向:”+this.student[i]. search);

}

}

public static void main(String[] args){

Teacher t[]={new Teacher(“李明”),new Teacher(“王华”)};

Student s1 = new Student(“孙强”,“数据库”);

Student s2 = new Student(“陈文”,“软件工程”);

Student s3 = new Student(“章锐”,“计算机网络”);

if(! t[0].add(s1)) {System.out.println(“每个老师最多只能指导20个学生!”);}

if(! t[0].add(a2)) {System.out.println(“每个老师最多只能指导20个学生!”);}

if(! t[1].add(s3)) {System.out.println(“每个老师最多只能指导20个学生!”);}

for(int i=0;i<2;i++)

(4);

}

}

class (5) {

String name;

String search;

public Student(){}

public Student(String name,String search){

this.name=name;

this.search=search;

}

String getName(){return this.name;}

String getSearch(){return this.search;}

}

正确答案:

(1) top++及其等效形式 (2) false (3) top (4) t[i].disp() (5) Student(1) top++及其等效形式 (2) false (3) top (4) t[i].disp() (5) Student 解析:(1)~(2):add函数是给Teacher类的student数组增加一个学生,每个老师最多能指导20个学生,如果增加成功则返回true;否则返回false。Top是一个表示student数组里面有多少个学生的标志。每增加一个学生,top就要加1

词条内容仅供参考,如果您需要解决具体问题
(尤其在法律、医学等领域),建议您咨询相关领域专业人士。

标签