已知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 void setSno(int sno){
this.sno = sno;
}
public int getSno(){
return sno;
}
public void setMajor(String major){
this.major = major;
}
public String getMajor(){
return major;
}
public Student(int sno,String name,String gender,int age,String major){
super(name,gender,age);
this.sno = sno;
this.major = major;
}
public int compareTo(Object a){
Student o = (Student)a;
return o.sno-this.sno;
}
}
class Teacher extends Person{
private int tno;
private String subject;
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;
}
public int compareTo(Object a){
Teacher o=(Teacher)a;
return this.getAge()-o.getAge();
}
}
class MyTool{
public static void separateStu_T(List persons, List teachers, List students){
for(int i = 0;i<persons.size();i++){
if(persons.get(i)instanceof Student){
students.add(persons.get(i));
}
else
teachers.add(persons.get(i));
}
}
}
感受
做这道题用了好几个小时,其实对代码中的很多地方都没有了解过,比如collection.sort是干啥的、List是什么都不懂
此题中最重要的知识点就是compareTo()
方法的重写以及它和sort()
的结合使用
原本,传给sort的值如果是1或0,则升序输出,否则降序输出
但是在和sort结合使用的时候需要注意
这个compareTo方法是被collection.sort自动调用的(我说怎么也找不到compareTo的调用方法,涨知识了)
而且你的输出的内容决定了sort究竟是升序还是降序
举个例子
(你在含age的Student类里定义了一个compareTo方法)
降序输出
public int compareTo(Object a){
Student stu = (Student) a;
return stu.age - this.age; //降序
}
升序输出
public int compareTo(Object a){
Student stu = (Student) a;
return this.age - stu.age; //升序
}
不难看出,当你使用一个外来对象的属性减-本对象的属性时,会是降序
反之,升序