2-1 学生、大学生、研究生类-2

修改题目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()方法输出等级

输入格式:

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

输出格式:

本科生等级
研究生等级

输入样例:

在这里给出一组输入。例如:

2 chen female cs 90

3 li male sc wang 80

输出样例:

在这里给出相应的输出。例如:

A

B

代码长度限制    16 KB

时间限制           400 ms

内存限制           64 MB

 代码如下:(以下代码可在lightly在线编译器上直接运行)

import java.util.Scanner;
public class Main{
    public static void main(String[] args) 
    {
         Scanner in = new Scanner(System.in);      
         int no1= in.nextInt();
         String name1= in.next();      
         String sex1= in.next();     
         String major1= in.next();
         int score1= in.nextInt();
         Student stu1 = new UnderGraduateStudent(no1, name1, sex1,major1,score1);
 
         int no2= in.nextInt();
         String name2= in.next();      
         String sex2= in.next();      
         String major2= in.next();
         String mentor2= in.next();//导师
         int score2= in.nextInt();
         Student stu2 = new GraduateStudent(no2, name2, sex2, major2,mentor2,score2);
         
         stu1.getGrade();
         stu2.getGrade();
         
         in.close(); 
    }
}
abstract class Student
{
	int no;
    String name;
    String sex;
    int score;
    Student(){
    	no = 0;
    	name = null;
    	sex = null;
    };
    Student(int no, String name, String sex, int score){
    	this.name = name;//名字跟形参相同用this
    	this.no = no;
    	this.sex = sex;
    	this.score = score;
    }
    public Student(int no, String name, String sex){
        this.name = name;
        this.no = no;
        this.sex = sex;
    }
    public void setSex(String sex){
    	this.sex = sex;
    }
    public void setNo(int no) {
        this.no = no;
    }
 
    public int getNo() {
        return no;
    }
 
    public String getName() {
        return name;
    }
 
    public String getSex() {
        return sex;
    }
    public int getScore(){
    	return score;
    }
    public void setscore(int score) {
    	this.score = score;
    }
    public abstract void getGrade();//抽象方法
   
}
class UnderGraduateStudent extends Student {//子类跟父类
    String major;
    UnderGraduateStudent(int no, String name, String sex, String major,int score){
        super(no,name,sex,score);
        this.major = major;
    }
    public String getMajor() {
        return major;
    }
 
    public void setMajor(String major)  {
        this.major = major;
    }
 
	@Override//覆盖(重载)
	public void getGrade() {
		if(score>=80){
            System.out.println("A");
        }else if(score>=70){
            System.out.println("B");
        }else if(score>=60){
            System.out.println("C");
        }else if(score>=50){
            System.out.println("D");
        }else{
            System.out.println("E");
        }
	}
}
class GraduateStudent extends UnderGraduateStudent {
    String mentor;
    GraduateStudent(int no, String name, String sex, String major, String mentor,int score){
        super(no, name, sex, major,score);
        this.mentor = mentor;
    }
 
    public String getSupervisor() {
        return mentor;
    }
 
    public void setMentor(String mentor) {
        this.mentor = mentor;
    }
 
    @Override//覆盖(重载)
    public void getGrade() {
    	if (score >= 90) {
            System.out.println("A");
        } else if (score >= 80) {
            System.out.println("B");
        } else if (score >= 70) {
            System.out.println("C");
        } else if (score >= 60) {
            System.out.println("D");
        } else {
            System.out.println("E");
        }
    }
}
 
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值