用Java写PTA 7-5 学生、大学生、研究生类-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
import java.util.Scanner;
abstract class Student{
int number;
int achievement;
String name;
String sex;
String profession;
String daoshi;
Student(int number, String name, String sex, int achievement, String profession){
this.number = number;
this.name = name;
this.sex = sex;
this.achievement = achievement;
this.profession = profession;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number=number;
}
public int getAchievement() {
return achievement;
}
public void setAchievement(int achievement) {
this.achievement=achievement;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
abstract void getGrade();
}
class Undergraduate extends Student{
Undergraduate(int number, String name, String sex , String profession, int achievement){
super(number, name, sex, achievement, profession);
this.profession = profession;
}
void getGrade() {
if(achievement >= 80){
System.out.println('A');
}
else if(achievement >= 70){
System.out.println('B');
}
else if(achievement >= 60){
System.out.println('C');
}
else if(achievement>= 50){
System.out.println('D');
}
else{
System.out.println('E');
}
}
}
class GraduateStudent extends Student{
GraduateStudent(int number, String name, String sex, String profession, String daoshi, int achievement){
super(number, name, sex, achievement, profession);
this.daoshi = daoshi;
}
void getGrade() {
if(achievement >= 90){
System.out.println('A');
}
else if(achievement>= 80){
System.out.println('B');
}
else if(achievement >= 70){
System.out.println('C');
}
else if(achievement>= 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 number1=sc.nextInt();
String name1=sc.next();
String sex1=sc.next();
String profession1=sc.next();
int achievement1=sc.nextInt();
int number2=sc.nextInt();
String name2=sc.next();
String sex2=sc.next();
String profession2=sc.next();
String daoshi=sc.next();
int achievement2=sc.nextInt();
Student student1=new Undergraduate(number1,name1,sex1, profession1, achievement1);
Student student2=new GraduateStudent(number2,name2, sex2,profession2,daoshi,achievement2);
student1.getGrade();
student2.getGrade();
}
}