实例----学生

代码如下:

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());
    }
};

程序运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值