Java设计一个表示学生的类

Java设计一个表示学生的类_第1张图片

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

你可能感兴趣的:(java实例)