用java创建一个学生类,属性包括:学号、姓名,和Java、C语言、数据结构三门课程成绩 //使用ArrayList<Student>保存10个学生对象, //分别对这些学生按照Java、C语言、数据结构和总成绩进行排序 //lamda //Comparable compare(Object o1,Object o2)。最后要求写到一个.java中
时间: 2023-12-07 10:03:50 浏览: 96
好的,下面是代码实现:
```
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Student implements Comparable<Student>{
private String id;
private String name;
private int javaScore;
private int cScore;
private int dataStructureScore;
public Student(String id, String name, int javaScore, int cScore, int dataStructureScore) {
this.id = id;
this.name = name;
this.javaScore = javaScore;
this.cScore = cScore;
this.dataStructureScore = dataStructureScore;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getJavaScore() {
return javaScore;
}
public void setJavaScore(int javaScore) {
this.javaScore = javaScore;
}
public int getCScore() {
return cScore;
}
public void setCScore(int cScore) {
this.cScore = cScore;
}
public int getDataStructureScore() {
return dataStructureScore;
}
public void setDataStructureScore(int dataStructureScore) {
this.dataStructureScore = dataStructureScore;
}
@Override
public int compareTo(Student o) {
return this.id.compareTo(o.getId());
}
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("001", "Tom", 80, 90, 70));
students.add(new Student("002", "Jerry", 70, 85, 90));
students.add(new Student("003", "Mike", 90, 60, 80));
students.add(new Student("004", "Lily", 60, 80, 85));
students.add(new Student("005", "Lucy", 85, 70, 75));
students.add(new Student("006", "John", 75, 90, 65));
students.add(new Student("007", "Amy", 95, 75, 80));
students.add(new Student("008", "Bob", 70, 85, 95));
students.add(new Student("009", "Kate", 80, 75, 90));
students.add(new Student("010", "Sam", 85, 80, 70));
// 按照Java成绩排序
Collections.sort(students, (o1, o2) -> o2.getJavaScore() - o1.getJavaScore());
System.out.println("按照Java成绩从高到低排序:");
for (Student student : students) {
System.out.println(student.getId() + " " + student.getName() + " " + student.getJavaScore());
}
// 按照C语言成绩排序
Collections.sort(students, Comparator.comparingInt(Student::getCScore).reversed());
System.out.println("按照C语言成绩从高到低排序:");
for (Student student : students) {
System.out.println(student.getId() + " " + student.getName() + " " + student.getCScore());
}
// 按照数据结构成绩排序
Collections.sort(students, Comparator.comparingInt(Student::getDataStructureScore).reversed());
System.out.println("按照数据结构成绩从高到低排序:");
for (Student student : students) {
System.out.println(student.getId() + " " + student.getName() + " " + student.getDataStructureScore());
}
// 按照总成绩排序
Collections.sort(students, (o1, o2) -> {
int total1 = o1.getJavaScore() + o1.getCScore() + o1.getDataStructureScore();
int total2 = o2.getJavaScore() + o2.getCScore() + o2.getDataStructureScore();
return total2 - total1;
});
System.out.println("按照总成绩从高到低排序:");
for (Student student : students) {
System.out.println(student.getId() + " " + student.getName() + " " + (student.getJavaScore() + student.getCScore() + student.getDataStructureScore()));
}
}
}
```
以上代码实现了一个学生类,包含学号、姓名、Java成绩、C语言成绩、数据结构成绩等属性,使用ArrayList保存了10个学生对象,并分别按照Java、C语言、数据结构和总成绩进行排序。其中,使用lamda表达式和Comparator接口实现了按照Java、C语言、数据结构成绩排序,使用Comparable接口实现了按照学号排序,最后使用lamda表达式实现了按照总成绩排序。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)