代码如下:
class Student {
private String stuno; //学号
private String name; //姓名
private float math; //数学成绩
private float english; //英语成绩
private float computer; //计算机成绩
public Student() { //定义无参构造
}
public Student(String stuno, String name, float math, float english, float computer) { //定义有参构造
this.stuno = stuno;
this.name = name;
this.math = math;
this.english = english;
this.computer = computer;
}
//Getter Setter方法
public String getStuno() {
return stuno;
}
public void setStuno(String stuno) {
this.stuno = stuno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getMath() {
return math;
}
public void setMath(float math) {
this.math = math;
}
public float getEnglish() {
return english;
}
public void setEnglish(float english) {
this.english = english;
}
public float getComputer() {
return computer;
}
public void setComputer(float computer) {
this.computer = computer;
}
//方法
public float sum() { //总分
return math + english + computer;
}
public float avg() { // 平均分
return this.sum() / 3;
}
public float max() { //最高分
float max = math;
max = max > computer ? max : computer;
max = max > english ? max : english;
return max;
}
public float min() { //最低分
float min = math;
min = min < computer ? min : computer;
min = min < english ? min : english;
return min;
}
};
public class ExampleDemo01 {
public static void main(String args[]) {
Student stu = null;
stu = new Student("001", "学生001", 95.0f, 89.0f, 96.0f);
System.out.println("学号" + stu.getStuno());
System.out.println("姓名" + stu.getName());
System.out.println("数学成绩" + stu.getMath());
System.out.println("英语成绩" + stu.getEnglish());
System.out.println("计算机成绩" + stu.getComputer());
System.out.println("总分" + stu.sum());
System.out.println("平均分" + stu.avg());
System.out.println("最高分" + stu.max());
System.out.println("最低分" + stu.min());
}
};
程序运行结果: