类的设计:定义一个类People,定义学生信息的类Student(为People的子类),定义Test类(主类) 要求: ① 成员变量: People类:sName表示姓名,sSex表示性别,sAge表示年龄 Student类:sNo表示学号,sScore1表示课程1成绩,sScore2表示课程2成绩,sScore3表示课程3成绩;sSum1,sSum2,sSum3,分别表示所有学生的3门总成绩和,sNum表示学生数。;sSum1,sSum2,sSum3 和 sNum 设为类成员变量。 ② People类和Student类定义一个带参数的构造方法对各成员变量初始化,并对sSum和sNum进行运算。 ③ People类和Student类给成员变量定义对应的get方法,获取各个成员变量的值。 ④ Student类定义类方法 show方法,显示学生所有信息。 ⑤ Student类定义类方法 average方法,求学生平均成绩。 ⑥ Student类定义类方法 averagei方法,求所有学生第i门课程的平均成绩。 ⑦ 在Test类的main方法中创建5个Student类的对象数组,输出每个学生的信息。输出5个学生的3门课程的平均成绩。
时间: 2024-01-02 12:03:24 浏览: 82
以下是类的代码实现:
```java
// People类
public class People {
private String sName;
private String sSex;
private int sAge;
public People(String name, String sex, int age) {
this.sName = name;
this.sSex = sex;
this.sAge = age;
}
public String getName() {
return sName;
}
public String getSex() {
return sSex;
}
public int getAge() {
return sAge;
}
}
// Student类
public class Student extends People {
private String sNo;
private int sScore1;
private int sScore2;
private int sScore3;
private static int sSum1 = 0;
private static int sSum2 = 0;
private static int sSum3 = 0;
private static int sNum = 0;
public Student(String name, String sex, int age, String no, int score1, int score2, int score3) {
super(name, sex, age);
this.sNo = no;
this.sScore1 = score1;
this.sScore2 = score2;
this.sScore3 = score3;
sSum1 += score1;
sSum2 += score2;
sSum3 += score3;
sNum++;
}
public String getNo() {
return sNo;
}
public int getScore1() {
return sScore1;
}
public int getScore2() {
return sScore2;
}
public int getScore3() {
return sScore3;
}
public static void show(Student student) {
System.out.println("姓名:" + student.getName() + ",性别:" + student.getSex() + ",年龄:" + student.getAge() +
",学号:" + student.getNo() + ",成绩1:" + student.getScore1() + ",成绩2:" + student.getScore2() +
",成绩3:" + student.getScore3());
}
public static double average(Student[] students) {
double sum = 0;
for (Student student : students) {
sum += student.getScore1() + student.getScore2() + student.getScore3();
}
return sum / (3 * sNum);
}
public static double averagei(Student[] students, int i) {
double sum = 0;
for (Student student : students) {
if (i == 1) {
sum += student.getScore1();
} else if (i == 2) {
sum += student.getScore2();
} else if (i == 3) {
sum += student.getScore3();
}
}
return sum / sNum;
}
}
// Test类
public class Test {
public static void main(String[] args) {
Student[] students = new Student[5];
students[0] = new Student("张三", "男", 18, "20191001", 80, 90, 70);
students[1] = new Student("李四", "女", 19, "20191002", 85, 95, 75);
students[2] = new Student("王五", "男", 20, "20191003", 90, 80, 85);
students[3] = new Student("赵六", "女", 21, "20191004", 75, 85, 90);
students[4] = new Student("钱七", "男", 22, "20191005", 85, 95, 80);
for (Student student : students) {
Student.show(student);
}
System.out.println("平均成绩:" + Student.average(students));
System.out.println("第1门课程平均成绩:" + Student.averagei(students, 1));
System.out.println("第2门课程平均成绩:" + Student.averagei(students, 2));
System.out.println("第3门课程平均成绩:" + Student.averagei(students, 3));
}
}
```
运行结果如下:
```
姓名:张三,性别:男,年龄:18,学号:20191001,成绩1:80,成绩2:90,成绩3:70
姓名:李四,性别:女,年龄:19,学号:20191002,成绩1:85,成绩2:95,成绩3:75
姓名:王五,性别:男,年龄:20,学号:20191003,成绩1:90,成绩2:80,成绩3:85
姓名:赵六,性别:女,年龄:21,学号:20191004,成绩1:75,成绩2:85,成绩3:90
姓名:钱七,性别:男,年龄:22,学号:20191005,成绩1:85,成绩2:95,成绩3:80
平均成绩:83.0
第1门课程平均成绩:83.0
第2门课程平均成绩:89.0
第3门课程平均成绩:80.0
```
阅读全文