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 以下 Emain函数中构造两个学生Student变量,分别指向本科生和研究生对象,调用getGrade()方法输出等级
Input本科生类信息,学号、姓名、性别、专业、成绩
研究生类信息,学号、姓名、性别、专业、导师、成绩
Output本科生等级
研究生等级
Sample Input2 chen female cs 90
3 li male sc wang 80
Sample OutputA
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();
}
}