學生成績統計
package 成績;
public class Grade {
private double chinese; //語文成績
private double math; //數學成績
private double english; //英語成績
public Grade(double chinese, double math, double english) //構造函數
{
this.chinese= chinese;
this.math = math;
this.english = english;
}
public double total() { //計算總成績
return chinese + math + english;
}
public void printTotal() { //輸出總成績
System.out.println(total());
}
public double average() { //計算平均值
return total() / 3;
}
public void printAverage() { //輸出平均值
System.out.println(average());
}
public double getchinese() { //獲取語文成績
return chinese;
}
public void setchinese(double chinese) {
this.chinese = chinese;
}
public double getMath() { //獲取數學成績
return math;
}
public void setMath(double math) {
this.math = math;
}
public double getenglish() { //獲取英語成績
return english;
}
public void setenglish(double english) {
this.english = english;
}
}
class Student {
String name; //學生姓名
Grade grade; //成績
public Student(String name, Grade grade) {
this.name = name;
this.grade = grade;
}
public static void main(String[] args) { //主程序調用
Student s = new Student("Cici", new Grade(98, 94, 90));
s.grade.printAverage();
s.grade.printTotal();
}
}
結果:
94.0
282.0
學生成績統計