2编写一个程序,定义一个表示学生的类Student和一个TestStudent类。Student类包括学号、姓名、性别、年龄和三门课程成绩的信息数据,以及用来获得和设置学号、姓名、性别、年龄和三门成绩的方法。在TestStudnet类中生成五个学生对象,计算三门课程的平均成绩,以及某门课程的最高分和最低分。
时间: 2024-02-04 14:03:52 浏览: 72
好的,下面是符合题目要求的源代码:
```java
// 定义学生类
class Student {
private String id;
private String name;
private String gender;
private int age;
private int score1;
private int score2;
private int score3;
// 设置学号的方法
public void setId(String id) {
this.id = id;
}
// 设置姓名的方法
public void setName(String name) {
this.name = name;
}
// 设置性别的方法
public void setGender(String gender) {
this.gender = gender;
}
// 设置年龄的方法
public void setAge(int age) {
this.age = age;
}
// 设置三门课程成绩的方法
public void setScores(int score1, int score2, int score3) {
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
}
// 获取学号的方法
public String getId() {
return id;
}
// 获取姓名的方法
public String getName() {
return name;
}
// 获取性别的方法
public String getGender() {
return gender;
}
// 获取年龄的方法
public int getAge() {
return age;
}
// 获取三门课程成绩的方法
public int getScore1() {
return score1;
}
public int getScore2() {
return score2;
}
public int getScore3() {
return score3;
}
}
// 定义TestStudent类,用于测试学生类
public class TestStudent {
public static void main(String[] args) {
// 生成五个学生对象
Student stu1 = new Student();
stu1.setId("001");
stu1.setName("Tom");
stu1.setGender("Male");
stu1.setAge(20);
stu1.setScores(80, 90, 70);
Student stu2 = new Student();
stu2.setId("002");
stu2.setName("Jane");
stu2.setGender("Female");
stu2.setAge(19);
stu2.setScores(70, 80, 90);
Student stu3 = new Student();
stu3.setId("003");
stu3.setName("Jack");
stu3.setGender("Male");
stu3.setAge(22);
stu3.setScores(90, 80, 85);
Student stu4 = new Student();
stu4.setId("004");
stu4.setName("Lily");
stu4.setGender("Female");
stu4.setAge(21);
stu4.setScores(85, 90, 80);
Student stu5 = new Student();
stu5.setId("005");
stu5.setName("Tim");
stu5.setGender("Male");
stu5.setAge(20);
stu5.setScores(80, 85, 75);
// 计算三门课程的平均成绩
double avg1 = (stu1.getScore1() + stu2.getScore1() + stu3.getScore1() + stu4.getScore1() + stu5.getScore1()) / 5.0;
double avg2 = (stu1.getScore2() + stu2.getScore2() + stu3.getScore2() + stu4.getScore2() + stu5.getScore2()) / 5.0;
double avg3 = (stu1.getScore3() + stu2.getScore3() + stu3.getScore3() + stu4.getScore3() + stu5.getScore3()) / 5.0;
System.out.println("Average score of course 1: " + avg1);
System.out.println("Average score of course 2: " + avg2);
System.out.println("Average score of course 3: " + avg3);
// 计算某门课程的最高分和最低分
int max1 = Math.max(stu1.getScore1(), Math.max(stu2.getScore1(), Math.max(stu3.getScore1(), Math.max(stu4.getScore1(), stu5.getScore1()))));
int min1 = Math.min(stu1.getScore1(), Math.min(stu2.getScore1(), Math.min(stu3.getScore1(), Math.min(stu4.getScore1(), stu5.getScore1()))));
System.out.println("Max score of course 1: " + max1);
System.out.println("Min score of course 1: " + min1);
int max2 = Math.max(stu1.getScore2(), Math.max(stu2.getScore2(), Math.max(stu3.getScore2(), Math.max(stu4.getScore2(), stu5.getScore2()))));
int min2 = Math.min(stu1.getScore2(), Math.min(stu2.getScore2(), Math.min(stu3.getScore2(), Math.min(stu4.getScore2(), stu5.getScore2()))));
System.out.println("Max score of course 2: " + max2);
System.out.println("Min score of course 2: " + min2);
int max3 = Math.max(stu1.getScore3(), Math.max(stu2.getScore3(), Math.max(stu3.getScore3(), Math.max(stu4.getScore3(), stu5.getScore3()))));
int min3 = Math.min(stu1.getScore3(), Math.min(stu2.getScore3(), Math.min(stu3.getScore3(), Math.min(stu4.getScore3(), stu5.getScore3()))));
System.out.println("Max score of course 3: " + max3);
System.out.println("Min score of course 3: " + min3);
}
}
```
你可以将这段代码保存为TestStudent.java文件,并同样使用`javac`编译器进行编译,然后使用`java`命令运行TestStudent类即可。
阅读全文