定义一个学生类student,成员变量包括姓名,性别,年龄,语文成绩,数学成绩和英语成绩,成员方法包括get XXX方法,set XXX方法,求总分和平均分,定义一个测试类student test,创建一个student对象,对其进行赋值并计算总分与平均分
时间: 2024-12-19 15:18:36 浏览: 16
StudentClass.rar_Java编程_Java_
首先,我们可以定义一个名为`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 String getName() { return name; }
public String getGender() { return gender; }
public int getAge() { return age; }
public double getChineseScore() { return chineseScore; }
public double getMathScore() { return mathScore; }
public double getEnglishScore() { return englishScore; }
// 设置方法
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 setChineseScore(double chineseScore) { this.chineseScore = chineseScore; }
public void setMathScore(double mathScore) { this.mathScore = mathScore; }
public void setEnglishScore(double englishScore) { this.englishScore = englishScore; }
// 计算总分和平均分方法
public double getTotalScore() {
return chineseScore + mathScore + englishScore;
}
public double getAverageScore() {
return getTotalScore() / 3.0;
}
}
```
接下来,我们创建一个名为`StudentTest`的测试类,用于创建和操作`Student`对象:
```java
public class StudentTest {
public static void main(String[] args) {
// 创建一个Student对象
Student student = new Student("张三", "男", 18, 95.0, 90.0, 88.0);
// 赋值
student.setName("李四");
student.setAge(20);
student.setMathScore(93.0);
// 计算总分和平均分
double totalScore = student.getTotalScore();
double averageScore = student.getAverageScore();
System.out.println("学生信息: " + student.getName());
System.out.println("总分: " + totalScore);
System.out.println("平均分: " + averageScore);
}
}
```
在这个例子中,`StudentTest`的`main`方法创建了一个`Student`对象,并对学生的各项信息进行了设置,然后计算了总分和平均分,并将结果打印出来。
阅读全文