现要求你使用java编程实现以下内容:编写一个学生类student,包含的属性有学号、姓名、性别、年龄等,将所有学生存储在一个数组中,编写学生管理程序实现如下操作:(1)增加一个学生;(2)根据学号删除某个学生;(3)将所有学生年龄增加一岁;(4)按数组中顺序显示所有学生信息;(5)将所有学生按姓名排序输出。
时间: 2023-05-28 10:07:16 浏览: 89
以下是java代码实现:
```
import java.util.Arrays;
public class StudentManagement {
private Student[] students;
private int size;
public StudentManagement(int capacity) {
students = new Student[capacity];
size = 0;
}
public void addStudent(Student s) {
if (size >= students.length) {
System.out.println("Error: No more space to add a new student.");
return;
}
students[size++] = s;
}
public void removeStudent(int id) {
int index = findStudentIndex(id);
if (index == -1) {
System.out.println("Error: Cannot find student with ID " + id);
return;
}
for (int i = index; i < size - 1; i++) {
students[i] = students[i+1];
}
students[--size] = null;
}
public void increaseAge() {
for (int i = 0; i < size; i++) {
students[i].setAge(students[i].getAge() + 1);
}
}
public void displayAllStudents() {
for (int i = 0; i < size; i++) {
System.out.println(students[i]);
}
}
public void sortByName() {
Arrays.sort(students, 0, size, (s1, s2) -> s1.getName().compareTo(s2.getName()));
}
private int findStudentIndex(int id) {
for (int i = 0; i < size; i++) {
if (students[i].getId() == id) {
return i;
}
}
return -1;
}
}
```
其中,Student类的实现如下:
```
public class Student {
private int id;
private String name;
private String gender;
private int age;
public Student(int id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Gender: " + gender + ", Age: " + age;
}
}
```
以下是测试代码:
```
public class TestStudentManagement {
public static void main(String[] args) {
StudentManagement sm = new StudentManagement(5);
sm.addStudent(new Student(1, "Alice", "female", 20));
sm.addStudent(new Student(2, "Bob", "male", 21));
sm.addStudent(new Student(3, "Charlie", "male", 22));
sm.addStudent(new Student(4, "David", "male", 23));
sm.addStudent(new Student(5, "Eve", "female", 24));
sm.addStudent(new Student(6, "Frank", "male", 25)); // will not be added due to capacity limit
sm.displayAllStudents();
System.out.println("--------");
sm.removeStudent(3);
sm.displayAllStudents();
System.out.println("--------");
sm.increaseAge();
sm.displayAllStudents();
System.out.println("--------");
sm.sortByName();
sm.displayAllStudents();
}
}
```
输出结果如下:
```
ID: 1, Name: Alice, Gender: female, Age: 20
ID: 2, Name: Bob, Gender: male, Age: 21
ID: 3, Name: Charlie, Gender: male, Age: 22
ID: 4, Name: David, Gender: male, Age: 23
ID: 5, Name: Eve, Gender: female, Age: 24
--------
ID: 1, Name: Alice, Gender: female, Age: 20
ID: 2, Name: Bob, Gender: male, Age: 21
ID: 4, Name: David, Gender: male, Age: 23
ID: 5, Name: Eve, Gender: female, Age: 24
--------
ID: 1, Name: Alice, Gender: female, Age: 21
ID: 2, Name: Bob, Gender: male, Age: 22
ID: 4, Name: David, Gender: male, Age: 24
ID: 5, Name: Eve, Gender: female, Age: 25
--------
ID: 1, Name: Alice, Gender: female, Age: 21
ID: 2, Name: Bob, Gender: male, Age: 22
ID: 5, Name: Eve, Gender: female, Age: 25
ID: 4, Name: David, Gender: male, Age: 24
```
阅读全文