1) Student类: 属性id, neme,specialty,avggrade,方法outPut ()输出所有属性的信息。 方法outMaxAvgstu()输出专业第一名学生的信息。 (2) StudentTest类:创建数组,数组的类型为 Student类,创建5个的元素,调用上述的两个方法
时间: 2023-05-21 19:05:24 浏览: 80
更方便快捷的外部操作数据库的方法(另类玩法)
答案:
Sure, I can help you with this question! Here is the code for the Student class:
```
public class Student {
private int id;
private String name;
private String specialty;
private double avgGrade;
public Student(int id, String name, String specialty, double avgGrade) {
this.id = id;
this.name = name;
this.specialty = specialty;
this.avgGrade = avgGrade;
}
public void outPut() {
System.out.println("Student Information:");
System.out.println("id: " + id);
System.out.println("name: " + name);
System.out.println("specialty: " + specialty);
System.out.println("avgGrade: " + avgGrade);
}
public static void outMaxAvgstu(Student[] students) {
double maxAvgGrade = -1;
int maxAvgGradeIndex = -1;
for (int i = 0; i < students.length; i++) {
if (students[i].avgGrade > maxAvgGrade) {
maxAvgGrade = students[i].avgGrade;
maxAvgGradeIndex = i;
}
}
System.out.println("The student with the highest average grade is:");
students[maxAvgGradeIndex].outPut();
}
}
```
And here is the code for the StudentTest class:
```
public class StudentTest {
public static void main(String[] args) {
Student[] students = new Student[5];
students[0] = new Student(1, "Alice", "Computer Science", 3.9);
students[1] = new Student(2, "Bob", "Electrical Engineering", 3.8);
students[2] = new Student(3, "Charlie", "Mechanical Engineering", 3.7);
students[3] = new Student(4, "David", "Civil Engineering", 3.6);
students[4] = new Student(5, "Emily", "Chemical Engineering", 3.5);
System.out.println("All students:");
for (Student student : students) {
student.outPut();
System.out.println();
}
Student.outMaxAvgstu(students);
}
}
```
Hope this helps!
阅读全文