(5) 定义一个表示学生信息的类Student,要求如下: ⚫ 类Student的成员变量: sNO 表示学号;sName表示姓名; sSex表示性别;sAge表示年龄; sJava:表示Java课程成绩。 ⚫ 类Student带参数的构造方法: 在构造方法中通过形参完成对成员变量的赋值操作。 ⚫ 类Student的方法成员: getNo():获得学号; getName():获得姓名; getSex():获得性别; getAge()获得年龄; getJava():获得Java 课程成绩; ⚫ 根据类Student的定义,创建五个该类的对象,输出每个学生的信息, 计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java 语言成绩的最大值和最小值
时间: 2024-02-20 13:59:08 浏览: 84
好的,我可以为您解答这个问题。以下是实现该类和要求的代码:
```java
public class Student {
private String sNO;
private String sName;
private String sSex;
private int sAge;
private double sJava;
public Student(String sNO, String sName, String sSex, int sAge, double sJava) {
this.sNO = sNO;
this.sName = sName;
this.sSex = sSex;
this.sAge = sAge;
this.sJava = sJava;
}
public String getNo() {
return sNO;
}
public String getName() {
return sName;
}
public String getSex() {
return sSex;
}
public int getAge() {
return sAge;
}
public double getJava() {
return sJava;
}
}
```
接下来,创建 5 个学生对象,并输出每个学生的信息:
```java
public static void main(String[] args) {
Student student1 = new Student("001", "张三", "男", 18, 85.5);
Student student2 = new Student("002", "李四", "女", 19, 90.0);
Student student3 = new Student("003", "王五", "男", 20, 76.5);
Student student4 = new Student("004", "赵六", "女", 18, 92.0);
Student student5 = new Student("005", "钱七", "男", 19, 82.5);
System.out.println("学生1信息:" + student1.getNo() + " " + student1.getName() + " " + student1.getSex() + " " + student1.getAge() + " " + student1.getJava());
System.out.println("学生2信息:" + student2.getNo() + " " + student2.getName() + " " + student2.getSex() + " " + student2.getAge() + " " + student2.getJava());
System.out.println("学生3信息:" + student3.getNo() + " " + student3.getName() + " " + student3.getSex() + " " + student3.getAge() + " " + student3.getJava());
System.out.println("学生4信息:" + student4.getNo() + " " + student4.getName() + " " + student4.getSex() + " " + student4.getAge() + " " + student4.getJava());
System.out.println("学生5信息:" + student5.getNo() + " " + student5.getName() + " " + student5.getSex() + " " + student5.getAge() + " " + student5.getJava());
}
```
计算并输出这五个学生 Java 语言成绩的平均值,以及计算并输出他们 Java 语言成绩的最大值和最小值:
```java
double sum = student1.getJava() + student2.getJava() + student3.getJava() + student4.getJava() + student5.getJava();
double average = sum / 5.0;
System.out.println("Java 语言成绩的平均值为:" + average);
double max = Math.max(student1.getJava(), Math.max(student2.getJava(), Math.max(student3.getJava(), Math.max(student4.getJava(), student5.getJava()))));
double min = Math.min(student1.getJava(), Math.min(student2.getJava(), Math.min(student3.getJava(), Math.min(student4.getJava(), student5.getJava()))));
System.out.println("Java 语言成绩的最大值为:" + max);
System.out.println("Java 语言成绩的最小值为:" + min);
```
这样就完成了题目的要求。
阅读全文