定义一个表示学生信息的类Student,要求如下: (1)类Student的成员变量: INO表示学号:aName表示姓名;sSer表示性别;sAge表示年验;sJava表示Java 课程成绩。(2)类Student带参数的构造方法: 在构造方法中通过形参完成对成员变量的斌值操作。(3)类Student的方法成员: getNo():获得学号: getName():获得姓名;getSex():获得性别: getAge():获得年龄;getJava():获得Java课程成绩 (4)根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输 出这五个学生Java语言成绩的平均值。
时间: 2024-01-26 10:01:40 浏览: 65
以下是实现类Student的代码:
```java
public class Student {
private int INO;
private String aName;
private String sSex;
private int sAge;
private double sJava;
public Student(int INO, String aName, String sSex, int sAge, double sJava) {
this.INO = INO;
this.aName = aName;
this.sSex = sSex;
this.sAge = sAge;
this.sJava = sJava;
}
public int getNo() {
return INO;
}
public String getName() {
return aName;
}
public String getSex() {
return sSex;
}
public int getAge() {
return sAge;
}
public double getJava() {
return sJava;
}
}
```
接下来是创建五个该类的对象,并输出每个学生的信息以及计算这五个学生Java语言成绩的平均值的代码:
```java
public class Main {
public static void main(String[] args) {
Student student1 = new Student(1, "Tom", "male", 18, 78.5);
Student student2 = new Student(2, "Mary", "female", 19, 89.0);
Student student3 = new Student(3, "John", "male", 20, 91.5);
Student student4 = new Student(4, "Lucy", "female", 21, 88.0);
Student student5 = new Student(5, "Bob", "male", 22, 84.5);
System.out.printf("学生1的信息:%d %s %s %d %.1f%n", student1.getNo(), student1.getName(), student1.getSex(), student1.getAge(), student1.getJava());
System.out.printf("学生2的信息:%d %s %s %d %.1f%n", student2.getNo(), student2.getName(), student2.getSex(), student2.getAge(), student2.getJava());
System.out.printf("学生3的信息:%d %s %s %d %.1f%n", student3.getNo(), student3.getName(), student3.getSex(), student3.getAge(), student3.getJava());
System.out.printf("学生4的信息:%d %s %s %d %.1f%n", student4.getNo(), student4.getName(), student4.getSex(), student4.getAge(), student4.getJava());
System.out.printf("学生5的信息:%d %s %s %d %.1f%n", student5.getNo(), student5.getName(), student5.getSex(), student5.getAge(), student5.getJava());
double average = (student1.getJava() + student2.getJava() + student3.getJava() + student4.getJava() + student5.getJava()) / 5;
System.out.printf("Java语言成绩的平均值为:%.1f%n", average);
}
}
```
输出结果如下:
```
学生1的信息:1 Tom male 18 78.5
学生2的信息:2 Mary female 19 89.0
学生3的信息:3 John male 20 91.5
学生4的信息:4 Lucy female 21 88.0
学生5的信息:5 Bob male 22 84.5
Java语言成绩的平均值为:86.3
```
在输出学生信息和计算平均值时,使用了`printf`方法来格式化输出,使得输出结果更加清晰易读。
阅读全文