题目:
定义并测试一个名为Student的类,包括的属性有"学号",“姓名”,以及三门课程"数学",“英语"和"计算机"成绩,包括的方法有计算3门课程的"总分”,“平均分”,“最高分"及"最低分”.
package e1;
class Student //定义学生类
{
private String stuno; //定义学生编号属性
private String name; //student name
private float math; //math.score
private float english; //English.score
private float computer; //computer.score
public Student() //定义无参构造
{
}
//定义有5个参数的构造方法,为类中的属性初始化
public Student(String stuno, String name, float math, float english, float computer) {
this.setStuno(stuno);
this.setName(name);
this.setMath(math);
this.setenglish(english);
this.setComputer(computer);
}
public void setStuno(String s) {
stuno = s;
}
public void setName(String n) {
name = n;
}
public void setMath(float m) {
math = m;
}
public void setenglish(float e) {
english = e;
}
public void setComputer(float c) {
computer = c;
}
public String getStuno() {
return stuno;
}
public String getName() {
return name;
}
public float getMath() {
return math;
}
public float getEnglish() {
return english;
}
public float getcomputer() {
return 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 e1
{
public static void main(String[] args)
{
Student stu = null;
//实例化student对象,并通过构造方法进行赋值
stu = new Student("MLDN-33","李新华",95,95,100);
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.max() );
System.out.println("最低分 : " +stu.min());
}
}
程序运行结果
学生编号 : MLDN-33
学生姓名 : 李新华
数学成绩 : 95.0
英语成绩 : 95.0
计算机成绩 : 100.0
最高分 : 100.0
最低分 : 95.0
Process finished with exit code 0