1. 设计一个教师类Teacher(属于cn包),要求:
1) 属性有编号(int no)、姓名(String name)、年龄(int age)、所属学院(String seminary),为这些属性设置相应的get和set方法。
2) 为Teacher类重写equals方法,要求:当两个教师对象的no相同时返回true。
3) 重写Teacher类的toString方法,通过该方法可以返回“编号为**、姓名为**、年龄为**的**学院老师”形式的字符串。
4) 由多个Teacher对象所形成的数组可以以两种方法排序(编号由低到高排序):1)使用Arrays.sort(Object[] a)方法;2)使用Arrays.sort(Object[] a, Comparator c)方法。
5) 再定义一个类TeacherManagement(属于cn.sd包),提供方法search,方法可以在一组给定的教师中,根据姓名(或年龄)返回等于指定姓名(或年龄)的教师的字符串信息,信息格式为:“编号为**、姓名为**、年龄为**的**学院老师”。如果没有满足条件的教师,则返回“没有符合条件的教师”。
6) 构造main方法进行测试。
Teacher
package cn.net.sdkd;
public class Teacher implements Comparable {
private int no, age;
private String name, seminary;
public Teacher(int no, String name, int age, String seminary) {
this.no = no;
this.age = age;
this.name = name;
this.seminary = seminary;
}
public void setNo(int no) {
this.no = no;
}
public int getNo() {
return no;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setSeminary(String seminary) {
this.seminary = seminary;
}
public String getSeminary() {
return seminary;
}
public boolean equals(Teacher t) {
return (this.getNo() == t.getNo()) ? true : false;
}
public String toString() {
return "编号为" + this.no + "、姓名为" + this.name + "、年龄为" + this.age + "的" + this.seminary + "学院的老师 ";
}
public int compareTo(Object o) {
Teacher otherTeacher = (Teacher) o;
if (no < otherTeacher.no)
return -1;
if (no > otherTeacher.no)
return 1;
return 0;
}
}
TeacherManagement
package cn.sd;
import cn.net.sdkd.Teacher;
public class TeacherManagement {
public void search(Teacher[] teachers, int age) {
int l = teachers.length, i;
for (i = 0; i < l; i++) {
if (age == teachers[i].getAge()) {
System.out.println(teachers[i].toString());
break;
} else if (i == l - 1)
System.out.println("没有符合条件的老师");
}
}
public void search(Teacher[] teachers, String name) {
int l = teachers.length, i;
for (i = 0; i < l; i++) {
if (name == teachers[i].getName()) {
System.out.println(teachers[i].toString());
break;
} else if (i == l - 1)
System.out.println("没有符合条件的老师");
}
}
}
main
package cn.net.sdkd;
import cn.sd.*;
import java.util.*;
public class mian {
public static void main(String[] args) {
Teacher[] teachers = new Teacher[4];
teachers[0] = new Teacher(001, "MA", 32, "计院");
teachers[1] = new Teacher(004, "SHAO", 31, "地科");
teachers[2] = new Teacher(003, "SONG", 30, "机电");
teachers[3] = new Teacher(002, "WU", 29, "艺术");
if (teachers[1].equals(teachers[2])) {
System.out.println(teachers[1].getName() + "老师和" + teachers[2].getName() + "老师学工号相同");
} else {
System.out.println(teachers[1].getName() + "老师和" + teachers[2].getName() + "老师学工号不相同");
}
if (teachers[2].equals(teachers[3])) {
System.out.println(teachers[2].getName() + "老师和" + teachers[3].getName() + "老师学工号相同");
} else {
System.out.println(teachers[2].getName() + "老师和" + teachers[3].getName() + "老师学工号不相同");
}
System.out.println(teachers[3].toString());
TeacherManagement a = new TeacherManagement();
a.search(teachers, 30);
a.search(teachers, "MA");
a.search(teachers, "mama");
System.out.println("按编号升序排列:");
Arrays.sort(teachers);
for (int i = 0; i < teachers.length; i++) {
System.out.println(teachers[i].toString());
}
}
}