设计一个教师类Teacher(java)

设计一个教师类Teacher(属于cn.net.sdkd包),要求:

  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方法进行测试。
package cn.net.sdkd;

public class Teacher implements Comparable {
	int no;
	String name;
	int age;
	String seminary;

	public Teacher(int no, String name, int age, String seminary) {
		this.no = no;
		this.name = name;
		this.age = age;
		this.seminary = seminary;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSeminary() {
		return seminary;
	}

	public void setSeminary(String seminary) {
		this.seminary = seminary;
	}

	public boolean equals(Teacher t) {
		if (this.no == t.no)
			return true;
		return false;
	}

	public String toString() {
		return "编号为" + this.no + "、姓名为" + this.name + "、年龄为" + this.age + "的" + this.seminary + "学院的老师 ";
	}

	public int compareTo(Object o) {
		Teacher t = (Teacher) o;
		if (no < t.no)
			return -1;
		else if (no > t.no)
			return 1;
		return 0;
	}

}

package cn.sd;

import cn.net.sdkd.Teacher;

public class TeacherManagement {

	public void search(Teacher[] t, int age) {
		int len = t.length;
		for (int i = 0; i < len; i++) {
			if (age == t[i].getAge()) {
				System.out.println(t[i].toString());
				break;
			}
			if (i == len - 1) {
				System.out.println("没有符合条件的老师");
			}
		}
	}

	public void search(Teacher[] t, String name) {
		int len = t.length;
		for (int i = 0; i < len; i++) {
			if (name == t[i].getName()) {
				System.out.println(t[i].toString());
				break;
			}
			if (i == len - 1) {
				System.out.println("没有符合条件的老师");
			}
		}
	}
}

package cn.net.sdkd;

import java.util.*;

import cn.sd.*;

public class main {

	public static void main(String[] args) {
		Teacher[] teachers = new Teacher[5];
		teachers[0] = new Teacher(1, "wa", 18, "jisuanji");
		teachers[1] = new Teacher(2, "sh", 19, "dianzi");
		teachers[2] = new Teacher(3, "ta", 20, "wulianwang");
		teachers[3] = new Teacher(4, "so", 26, "xinan");
		teachers[4] = new Teacher(5, "su", 24, "ruanjian");
		if (teachers[1].equals(teachers[2])) {
			System.out.println("same");
		} else {
			System.out.println("diffirent");
		}
		System.out.println(teachers[3].toString());
		TeacherManagement q = new TeacherManagement();
		q.search(teachers, 19);
		q.search(teachers, "su");
		System.out.println("以升序排列:");
		Arrays.sort(teachers);
		for (int i = 0; i < teachers.length; i++) {
			System.out.println(teachers[i].toString());
		}
	}

}

你可能感兴趣的:(java,基础算法)