使用java编写2个代码。1、定义一个学生类Student,成员变量包括姓名、性别、年龄、语文成绩、数学成绩和英语成绩,定义两个成员方法,一个是求总分,另一个是求平均分。2、定义一个测试类StudentTest,创建一个Student对象,对其进行赋值并计算总分与平均分。
时间: 2023-05-29 11:07:33 浏览: 152
StudentClass.rar_Java编程_Java_
1. 学生类Student的代码:
```java
public class Student {
private String name;
private String gender;
private int age;
private double chineseScore;
private double mathScore;
private double englishScore;
public Student(String name, String gender, int age, double chineseScore, double mathScore, double englishScore) {
this.name = name;
this.gender = gender;
this.age = age;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
}
public double getTotalScore() {
return chineseScore + mathScore + englishScore;
}
public double getAverageScore() {
return getTotalScore() / 3;
}
}
```
2. 测试类StudentTest的代码:
```java
public class StudentTest {
public static void main(String[] args) {
Student student = new Student("张三", "男", 18, 80, 90, 85);
double totalScore = student.getTotalScore();
double averageScore = student.getAverageScore();
System.out.println("学生 " + student.getName() + " 的总分是:" + totalScore);
System.out.println("学生 " + student.getName() + " 的平均分是:" + averageScore);
}
}
```
阅读全文