已知Main类、Person类的设计,完成Student类,Teacher类、MyTool类的设计。
函数接口定义:
class Student extends Person{ }
class Teacher extends Person{ }
class MyTool{ public static void separateStu_T(List persons,List teachers,List students){} }
Student类继承了Person,拥有私有属性int类型的sno和String类型的major,分别代表学号与所学专业;提供对应的set,get方法;比较方法完成按照学号比较。
Teacher类继承了Person,拥有私有属性int类型的tno和String类型的subject,分别代表教师编号与所授科目;提供对应的set,get方法;比较方法完成按年龄比较。
MyTool类中提供方法public static void separateStu_T(List persons,List teachers,List students){},方法 separateStu_T的功能是将persons线性表中的 teacher,student分别放到teachers,students两个线性表中。
裁判测试程序样例:
import java.util.*;
public class Main {
public static void main(String[] args) {
List persons=getPersons(); //得到一个所有人的线性表
List teachers=new ArrayList();
List students=new ArrayList();
MyTool.separateStu_T( persons,teachers,students); //将persons线性表中的 teacher,student分别放到teachers,students两个线性表中
Collections.sort(teachers); //对教师线性表排序
Collections.sort(students); //对学生线性表排序
showResult(teachers); //显示教师线性表排序以后的结果
showResult(students); //显示学生线性表排序以后的结果
}
public static List getPersons()
{
List persons=new ArrayList();
Scanner in=new Scanner(System.in);
Person person=null;
int num=Integer.parseInt(in.nextLine());
for(int i=0;i<num;i++)
{ String str=in.nextLine();
String []data=str.split(",");
if(data[0].equalsIgnoreCase("student"))
person=new Student(Integer.parseInt(data[1]),data[2],data[3],Integer.parseInt(data[4]),data[5]);
else if (data[0].equalsIgnoreCase("teacher"))
person=new Teacher(Integer.parseInt(data[1]),data[2],data[3],Integer.parseInt(data[4]),data[5]);
else person=null;
persons.add(person);
}
return persons;
}
public static void showResult(List persons)
{
for(int i=0;i<persons.size();i++)
{
Person per=(Person)persons.get(i);
System.out.println(per.getName()+","+per.getGender()+","+per.getAge());
}
}
}
abstract class Person implements Comparable
{ private String name;
private String gender;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, String gender, int age) {
super();
this.name = name;
this.gender = gender;
this.age = age;
}
}
/* 请在这里写你的代码 */
输入样例:
输入的第一行是总人数n,紧跟着输入n行,每一行代表一个人的信息。下面的例子中n=5,输入了5个人的信息。
5
student,1001,Tom1,female,18,computer
teacher,2001,Jake1,female,35,datastructer
student,1002,Tom2,male,19,computer
student,1003,Tom3,female,20,software
teacher,2002,Jake2,female,33,database
输出样例:
注意:本样例输出结果中两名教师是按照年龄升序排序的,三名学生是按照学号降序排序的。
Jake2,female,33
Jake1,female,35
Tom3,female,20
Tom2,male,19
Tom1,female,18
通过代码:
class Student extends Person{
private int sno;
private String major;
public Student(Integer sno, String name, String gender, int age, String major) {
super(name, gender, age);
this.sno = sno;
this.major = major;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
@Override
public int compareTo(Object arg0) {
Student other = (Student)arg0;
if(sno > other.sno) return -1;
else if(sno < other.sno) return 1;
else return 0;
}
}
class Teacher extends Person{
private int tno;
private String subject;
public Teacher(int tno, String name, String gender, int age, String subject) {
super(name, gender, age);
this.tno = tno;
this.subject = subject;
}
public int getTno() {
return tno;
}
public void setTno(int tno) {
this.tno = tno;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
@Override
public int compareTo(Object o) {
Teacher other = (Teacher)o;
if(this.getAge() > other.getAge()) return 1;
else if(this.getAge() < other.getAge()) return -1;
else return 0;
}
}
class MyTool{
public static void separateStu_T(List persons,List teachers,List students){
Iterator<Person> iterator = persons.iterator();
while(iterator.hasNext()) {
Person person = iterator.next();
if(person instanceof Teacher) {
teachers.add((Teacher)person);
}
else if(person instanceof Student) {
students.add((Student)person);
}
}
}
}