【java】教师类及查找等 经典考题

package javatutorial.chapter3.lab.teacher;

public class Teacher implements Comparable<Teacher>{
	private int no;
	private String name;
	private int age;
	private String seminary;
	
	public Teacher(){
		
	}
	
	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;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Teacher other = (Teacher) obj;
		
		if (no != other.no){
			return false;
		}
		return true;
	}

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

	@Override
	public int compareTo(Teacher t) {
		int res;
		if(this.no<t.no){
			res = -1;
		}else if(this.no>t.no){
			res = 1;
		}else{
			res = 0;
		}
		return res;
	}

}
 
package javatutorial.chapter3.lab.teacher;

import java.util.Comparator;

public class TeacherComparator implements Comparator<Teacher>{

	@Override
	public int compare(Teacher t1, Teacher t2) {
		int res;
		if(t1.getNo()<t2.getNo()){
			res = -1;
		}else if(t1.getNo()>t2.getNo()){
			res = 1;
		}else{
			res = 0;
		}
		return res;
	}

}

package javatutorial.chapter3.lab.teacher;

public class TeacherManagement {
	public static String search(Teacher[] teachers, String name){
		Teacher t = null;
		for(int i=0;i<teachers.length;i++){
			if(teachers[i]!=null && teachers[i].getName().equals(name)){
				t = teachers[i];
				break;
			}
		}
		
		if(t!=null){
			return t.toString();
		}else{
			return "没有符合条件的教师";
		}
		
	}
	
	public static String search(Teacher[] teachers, int age){
		Teacher t = null;
		for(int i=0;i<teachers.length;i++){
			if(teachers[i]!=null && teachers[i].getAge()==age){
				t = teachers[i];
				break;
			}
		}
		
		if(t!=null){
			return t.toString();
		}else{
			return "没有符合条件的教师";
		}
	}

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值