一道java编程题 急!!! 50
请你为本校老师设计一个类,成员变量包括:编号,姓名,年龄,论文数量。请创建5个实例代表5名老师,为所有老师的成员变量赋任意初值,程序自己可以按论文数量从高到低排序,并按序...
请你为本校老师设计一个类,成员变量包括:编号,姓名,年龄,论文数量。请创建5个实例代表5名老师,为所有老师的成员变量赋任意初值,程序自己可以按论文数量从高到低排序,并按序排列的教师信息的正确结果。
按论文数量从高到低排列,并按序输出教师各项信息 展开
按论文数量从高到低排列,并按序输出教师各项信息 展开
6个回答
展开全部
public class Test {
public static void main(String args[]) {
Teacher t1 = new Teacher("001","张三","23","40");
Teacher t2 = new Teacher("002","李四","45","102");
Teacher t3 = new Teacher("003","王五","34","80");
Teacher t4 = new Teacher("004","赵六","19","15");
Teacher t5 = new Teacher("005","前期","24","50");
List list = new ArrayList();
list.add(t1);
list.add(t2);
list.add(t3);
list.add(t4);
list.add(t5);
Object[] techers = list.toArray();
System.out.println ("排序前。。。。");
for (int i = 0; i<teachers.length; i++){
System.out.println (teachers[i]);
}
//调用排序方法
java.util.Arrays.sort(teachers, new MyComparator());
System.out.println ("排序后。。。。");
for (int i = 0; i<teachers.length; i++){
System.out.println (teachers[i]);
}
}
}
class Teacher {
private String no;
private String name;
private int age;
private int count;
public Teacher(String no, String name, int age, int count) {
this.no = no;
this.name = name;
this.age = age;
this.count =count;
}
public void setNo(String no){
this.no = no;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setCount(int count) {
this.count = count;
}
public String getNo() {
return this.no;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public int getCount() {
return this.count;
}
}
class MyComparator implements Comparator {
public int compare(Object o1, Object o2) {
Teacher t1 = (Teacher) o1;
Teacher t2 = (Teacher) o2;
if(t1.getCount() > t2.getCount())
return 1;
else if(t1.getCount() < t2.getCount())
return -1;
else {
//论文数量相同,按年龄排序
if(t1.getAge() > t2.getAge()) {
return 1;
} else if(t1.getAge() < t2.getAge()) {
return -1;
} else {
//利用String自身的排序方法。按名字排序
return t1.getName().compareTo(t2.getName());
}
}
}
}
2015-11-24
展开全部
/**
* 请你为本校老师设计一个类,成员变量包括:编号,姓名,年龄,论文数量。
* 请创建5个实例代表5名老师,为所有老师的成员变量赋任意初值,
* 程序自己可以按论文数量从高到低排序,并按序排列的教师信息的正确结果。
* 按论文数量从高到低排列,并按序输出教师各项信息
* @author Administrator
*
*/
//教师的类
class Teacher{
private int id;
private String name;
private int age;
private int papperCount;
public Teacher(int id, String name, int age, int papperCount) {
super();
this.id = id;
this.name = name;
this.age = age;
this.papperCount = papperCount;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 int getPapperCount() {
return papperCount;
}
public void setPapperCount(int papperCount) {
this.papperCount = papperCount;
}
}
//测试类
public class TeacherTest {
public static void main(String[] args) {
Teacher[] t = new Teacher[3];
//新建三个教师
t[0] = new Teacher(1,"zhangsan",20,1);
t[1] = new Teacher(1,"lisi",25,5);
t[2] = new Teacher(1,"wangwu",99,18);
//获取老师的论文数量
int[] i = new int[]{t[0].getPapperCount(),t[1].getPapperCount(),t[2].getPapperCount()};
Arrays.sort(i);
for (int j = i.length - 1; j >= 0 ; j--) {
//按论文数量从高到底排序
System.out.println(t[j].getName());
}
}
}
望采纳!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-11-24
展开全部
package code;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;import java.util.List;
public class test {
public static void main(String[] args) {
List<teacher> teachers = new ArrayList<>();
teachers.add(new teacher(1, "Zhao", 35, 4));
teachers.add(new teacher(2, "bai", 33, 1));
teachers.add(new teacher(3, "wang", 38, 10));
teachers.add(new teacher(4, "li", 45, 16));
teachers.add(new teacher(5, "su", 32, 2));
Collections.sort(teachers, new Comparator<teacher>() {
@Override
public int compare(teacher a, teacher b) {
return b.getPaper().compareTo(a.getPaper());
} });
for (Object object : teachers) {
teacher t = (teacher) object;
System.out.println("ID:" + t.id + " Name:" + t.name + " Age: " + t.age + " Paper:" + t.paper);
} }}
class teacher {
int id;
String name;
int age;
Integer paper;
public teacher(int id, String name, int age, Integer paper) {
this.id = id;
this.name = name;
this.age = age;
this.paper = paper; }
public Integer getPaper() {
return paper;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Tutor implements Comparable<Tutor> {
private String no;
private String name;
private int age;
private int thesisCount;
public String getNo() {
return no;
}
public void setNo(String 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 int getThesisCount() {
return thesisCount;
}
public void setThesisCount(int thesisCount) {
this.thesisCount = thesisCount;
}
@Override
public int compareTo(Tutor o) {
if (this.thesisCount > o.getThesisCount()) {
return -1;
} else if (this.thesisCount < o.getThesisCount()) {
return 1;
} else {
return 0;
}
}
}
public static void main(String[] args) {
Tutor t1 = new Tutor();
t1.setName("t1");
t1.setThesisCount(3);
Tutor t2 = new Tutor();
t2.setName("t2");
t2.setThesisCount(1);
Tutor t3 = new Tutor();
t3.setName("t3");
t3.setThesisCount(5);
List<Tutor> list = new ArrayList<Tutor>();
list.add(t1);
list.add(t2);
list.add(t3);
Collections.sort(list);
for (Tutor t : list) {
System.out.println(t.getName());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Teacher(){
public Long id;
public String name;
public int age;
public int num;//论文数量
}
/**
你按这样多创建几个就行了。
**/
public class Test(){
Teacher teacher = new Teacher();
teacher.id=1;
teacher.name="高数教授";
teacher.age=55;
teacher.num=13;
}
/*至于排序,我不懂你说的是什么意思。我就按我的理解来写select吧。*/
首先新建一张表,把编号,姓名,年龄,论文数量字段建好。假设表名为t_teacher
查询的select。select * from t_teacher order by num desc
//desc是倒序的意思
//好久没写这么容易的sql了。。 (- 。 -)
public Long id;
public String name;
public int age;
public int num;//论文数量
}
/**
你按这样多创建几个就行了。
**/
public class Test(){
Teacher teacher = new Teacher();
teacher.id=1;
teacher.name="高数教授";
teacher.age=55;
teacher.num=13;
}
/*至于排序,我不懂你说的是什么意思。我就按我的理解来写select吧。*/
首先新建一张表,把编号,姓名,年龄,论文数量字段建好。假设表名为t_teacher
查询的select。select * from t_teacher order by num desc
//desc是倒序的意思
//好久没写这么容易的sql了。。 (- 。 -)
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询