150 - 教师类

150 - 教师类

Time Limit: 1000   Memory Limit: 65535
Submit: 501  Solved: 286

Description

设计一个教师类Teacher,要求:
属性有编号(int no)、姓名(String name)、年龄(int age)、所属学院(String seminary),为这些属性设置相应的get和set方法。
为Teacher类重写equals方法,要求:当两个教师对象的no相同时返回true。
重写Teacher类的toString方法,通过该方法可以返回“no: **, name:**, age: **, seminary: **”形式的字符串。

Input

两个教师对象的编号,姓名,年龄,学院

Output

教师的信息
两个教师是否相等

Sample Input

1 Linda 38 SoftwareEngineering
2 Mindy 27 ComputerScience

Sample Output

no: 1, name:Linda, age: 38, seminary: SoftwareEngineering
no: 2, name:Mindy, age: 27, seminary: ComputerScience
false

 

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		int n1 = scan.nextInt();
		String na1 = scan.next();
		int a1 = scan.nextInt();
		String s1 = scan.next();
		Teacher t1 = new Teacher(n1,na1,a1,s1);
		int n2 = scan.nextInt();
        String na2 = scan.next();
        int a2 = scan.nextInt();
        String s2 = scan.next();
        Teacher t2 = new Teacher(n2,na2,a2,s2);

        System.out.println(t1.toString());
        System.out.println(t2.toString());
        System.out.println(t1.equals(t2));
	}
}
class Teacher
{
	int no;
	String name;
	int age;
	String seminary;
	Teacher(int no, String name, int age, String seminary)
	{
		this.no = no;
		this.name =  name;
		this.age = age;
		this.seminary = 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)//o是否是一个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();
    }
}

 

你可能感兴趣的:(Java)