1.实验项目内容:
设计学生类,其中应包含学生的三项成绩(计算机,英语,数学),要求可以求总分、平均分、最高分、最低分,并且可以输出一个学生的完整信息。
2.实验要求:
- 根据需求定义类(成员变量和成员方法)
- 所有属性都必须被封装
- 所有属性必须通过getter和setter访问
- 如果需要应增加构造方法为属性赋值
- 所有的信息不要在类中直接输出,而是交给方法调用处输出(在类中不能出现System.out.println()语句,如果出现则类的设计不合格).
class Student { private String name; private int age; private float computer; private float math; private float english; public Student(String n,int a,float c,float m,float e){ this.setName(n); this.setAge(a); this.setComputer(c); this.setMath(m); this.setEnglish(e); } //求和 public float sum(){ return computer+math+english; } //平均值 public float avg(){ return this.sum()/3; } //最大值 public float max(){ float max=computer>math?math:computer; max=max>english?max:english; return max; } //最小值 public float min(){ float min=computer<math?math:computer; min=min<english?min:english; return min; } //Info学生信息 public String getInto(){ return "学生信息:"+"\n"+ "姓名:"+this.getName()+"\n"+ "年龄:"+this.getAge()+"\n"+ "计算机成绩:"+this.getComputer()+"\n"+ "数学成绩:"+this.getMath()+"\n"+ "英语成绩:"+this.getEnglish()+"\n"; } //赋值 public void setName(String n){ this.name=n; } public void setAge(int a){ this.age=a; } public void setComputer(float c){ this.computer=c; } public void setMath(float m){ this.math=m; } public void setEnglish(float e){ this.english=e; } public String getName(){ return name; } public int getAge(){ return age; } public float getComputer(){ return computer; } public float getMath(){ return math; } public float getEnglish(){ return english; } } public class Demo{ public static void main(String[] args){ Student stu=new Student("杨慕尧",20,65.0f,83.0f,99.0f); stu.getInto(); System.out.println(stu.getInto()); System.out.println("总分:"+stu.sum()); System.out.println("平均分:"+stu.avg()); System.out.println("最高分:"+stu.max()); System.out.println("最低分:"+stu.min()); } }