PTA 7-4 学生、大学生、研究生类-2 (20 分) JAVA

Description

修改题目125(学生类-本科生类-研究生类)
为学生类添加属性成绩,添加相应的get和set函数,添加函数getGrade()表示获得等级,该函数应当为抽象函数。
本科生和研究生的等级计算方式不同,如下所示

本科生标准 研究生标准
[80–100) A [90–100) A
[70–80) B [80–90) B
[60–70) C [70–80) C
[50–60) D [60–70) D
50 以下 E 60 以下 E

main函数中构造两个学生Student变量,分别指向本科生和研究生对象,调用getGrade()方法输出等级
Input

本科生类信息,学号、姓名、性别、专业、成绩
研究生类信息,学号、姓名、性别、专业、导师、成绩
Output

本科生等级
研究生等级
Sample Input

2 chen female cs 90
3 li male sc wang 80
Sample Output

A
B

import java.util.Scanner;

abstract class Student{
    String name;
    int id;
    String sex;
//    String major;
    int grade;
    String major;
    Student(int id, String name, String sex, int grade, String major){
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.grade = grade;
        this.major = major;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    abstract void getGrade();
}

class Undergraduate extends Student{
    // 学号、姓名、性别、专业、成绩

    Undergraduate(int id, String name, String sex , String major, int grade){
        super(id, name, sex, grade, major);
        this.major = major;
    }

    @Override
    void getGrade() {
        if(grade >= 80){
            System.out.println('A');
        }
        else if(grade >= 70){
            System.out.println('B');
        }
        else if(grade >= 60){
            System.out.println('C');
        }
        else if(grade >= 50){
            System.out.println('D');
        }
        else{
            System.out.println('E');
        }
    }
}

class Graduate extends Student{
    String teacher;
    Graduate(int id, String name, String sex, String major, String teacher, int grade){
        super(id, name, sex, grade, major);
        this.teacher = teacher;
    }

    @Override
    void getGrade() {
        if(grade >= 90){
            System.out.println('A');
        }
        else if(grade >= 80){
            System.out.println('B');
        }
        else if(grade >= 70){
            System.out.println('C');
        }
        else if(grade >= 60){
            System.out.println('D');
        }
        else{
            System.out.println('E');
        }
    }
}

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int id = sc.nextInt();
        String name = sc.next();
        String sex = sc.next();
        String major = sc.next();
        int grade = sc.nextInt();
        Student a = new Undergraduate(id, name, sex, major, grade);
        a.getGrade();

        id = sc.nextInt();
        name = sc.next();
        sex = sc.next();
        major = sc.next();
        String teacher = sc.next();
        grade = sc.nextInt();
        Student b = new Graduate(id, name, sex, major, teacher, grade);
        b.getGrade();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值