JAVA小测试(定义一个Student类)

题目:

定义并测试一个名为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

  • 11
    点赞
  • 106
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值