Time Limit: 1000 Memory Limit: 65535
Submit: 740 Solved: 270
Description
修改题目143
1. 修改教师类,使得由多个Teacher对象所形成的数组可以排序(编号由低到高排序),并在main函数中使用Arrays.sort(Object[] a)方法排序
2. 定义一个类TeacherManagement,包含教师数组,提供方法add(Teacher[]),使其可以添加教师,提供重载方法search,方法可以在一组给定的教师中,根据姓名或年龄返回等于指定姓名或年龄的教师的字符串信息,信息格式为:“no: **, name:**, age: **, seminary: **”。如果没有满足条件的教师,则返回“no such teacher”。
Input
教师个数
教师信息
待查找教师的姓名
待查找教师的年龄
Output
排序后的信息
按姓名查找的老师信息
按年龄查找的老师信息
Sample Input
4
3 Linda 38 SoftwareEngineering
1 Mindy 27 ComputerScience
4 Cindy 28 SoftwareEngineering
2 Melody 27 ComputerScience
Cindy
27
Sample Output
no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience
no: 3, name: Linda, age: 38, seminary: SoftwareEngineering
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by name:
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by age:
no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience
import java.util.*;
import java.lang.reflect.Array;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i;
int num = scan.nextInt();
TeacherManagement tm = new TeacherManagement(num);
Teacher[] t = new Teacher[num];
for(i=0; i{
int no;
String name;
int age;
String seminary;
Teacher(int no, String name, int age, String seminary){
setNo(no);
setName(name);
setSeminary(seminary);
setAge(age);
}
public void setNo(int no) {
this.no = no;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSeminary(String seminary) {
this.seminary = seminary;
}
public String getName() {
return name;
}
public int getNo() {
return no;
}
public int getAge() {
return age;
}
public String getSeminary() {
return seminary;
}
public boolean equals(Object o){
if(o == null)
return false;
else{
boolean res = false;
if(o instanceof Teacher){
Teacher t = (Teacher)o;
if(this.no == t.no){
res = true;
}
}
return res;
}
}
public String toString(){
return "no: "+getNo()+", name: "+getName()+", age: "+getAge()+", seminary: "+getSeminary();
}
public int compareTo(Teacher t){
if(this.no == t.no)
return 0;
else if(this.no>t.no)
return 1;
else
return -1;
}
}
class TeacherManagement{
Teacher[] t;
int num;
TeacherManagement(int num){
this.num = num;
this.t = new Teacher[num];
}
public void add(Teacher[] t){
this.t = t;
}
// 重载
public void search(int age){
int flag = 0;
System.out.println("search by age:");
for (Teacher aT : t) {
if (aT.age == age) {
flag = 1;
System.out.println(aT.toString());
}
}
if(flag==0){
System.out.println("no such teacher");
}
}
public void search(String name){
int flag = 0;
System.out.println("search by name:");
for (Teacher aT : t) {
if (aT.name.equals(name)) {
flag = 1;
System.out.println(aT.toString());
}
}
if(flag==0){
System.out.println("no such teacher");
}
}
}