Java设计一个表示学生的类

本文介绍了一个简单的Java程序,用于创建学生对象,并记录学生的姓名、年龄及三门课程的成绩。程序还提供了计算总分、平均分、最高分及最低分的方法。

这里写图片描述

package student;

class InOfStudent{
    private String name;
    private int age;
    private double computer;
    private double english;
    private double math;

    public InOfStudent(){}//构造方法
    public InOfStudent(String n,int a,double c,double e,double m){
        this.setName(n);
        this.setAge(a);
        this.setComputer(c);
        this.setEnglish(e);
        this.setMath(m);
    }

    public String getName() {//姓名
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {//
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public double getComputer() {//
        return computer;
    }
    public void setComputer(double computer) {
        this.computer = computer;
    }

    public double getEnglish() {//
        return english;
    }
    public void setEnglish(double english) {
        this.english = english;
    }

    public double getMath() {//
        return math;
    }
    public void setMath(double math) {
        this.math = math;
    }

    public double score(){
        return (this.computer + this.english + this.math);
    }

    public double average(){
        return((this.computer + this.english + this.math)/3);
    }

    public double top(){
        double max;
        max = this.computer > this.english ? this.computer : this.english;
        max = max > this.math ? max : this.math;
        return max;
    }

    public double down(){
        double min;
        min = this.computer < this.english ? this.computer : this.english;
        min = min < this.math ? min : this.math;
        return min;
    }
}

public class Student {

    public static void main(String[] args) {
        InOfStudent person = new InOfStudent("嘟嘟",19,79,80,90);
        System.out.println("姓名:" + person.getName() + "\n" + "年龄:" + person.getAge() + "\n" + "计算机:" + person.getComputer() + "\n" + "英语:" + person.getEnglish() + "\n" + "数学:" + person.getMath());
        System.out.println("总分:" + person.score());
        System.out.println("平均分:" + person.average());
        System.out.println("最高分:" + person.top());
        System.out.println("最低分:" + person.down());
    }

}

代码运行结果:

姓名:嘟嘟
年龄:19
计算机:79.0
英语:80.0
数学:90.0
总分:249.0
平均分:83.0
最高分:90.0
最低分:79.0

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值