1. 程式人生 > >學生、大學生、研究生類-2

學生、大學生、研究生類-2

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()方法輸出等級
import java.util.Scanner;

public class Main {
	public static void main(String [] args) {
		
		Scanner scan = new Scanner(System.in);      
        int no = scan.nextInt();
        String name = scan.next();      
        String sex = scan.next();         
        String major = scan.next();
        int grade = scan.nextInt();
        CollegeStudent c = new CollegeStudent(no, name, sex, major);
        c.getGrade(grade);

        no = scan.nextInt();
        name = scan.next();      
        sex = scan.next();      
        major = scan.next();
        String supervisor = scan.next();
        grade = scan.nextInt();
        
        GraduateStudent g = new GraduateStudent(no, name, sex, major, supervisor );
        g.getGrade(grade);
        scan.close(); 
   }

}


abstract class Student{
	int no;
	String name;
	public Student(int no, String name, String sex, int grade, char level) {
		super();
		this.no = no;
		this.name = name;
		this.sex = sex;
		this.grade = grade;
		this.level = level;
	}

	String sex;
	int grade;
	char level;
	
	public abstract void getGrade(int grade) ;
	
	public void setGrade(int grade) {
		this.grade = grade;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	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 Student(int no, String name, String sex) {
		super();
		this.no = no;
		this.name = name;
		this.sex = sex;
	}
	
	public void attendClass(String className) {}
	
	public void print() {
		System.out.println("no: " + this.no);
		System.out.println("name: " + this.name);
		System.out.println("sex: " + this.sex);
	}
	
}

 class CollegeStudent extends Student{
	
	String major;

	public CollegeStudent(int no, String name, String sex,String major) {
		super(no, name, sex);
		// TODO Auto-generated constructor stub
		this.major = major;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}
	
	public void getGrade(int grade) {
		this.grade = grade;
		
		if(this.grade>=80 && this.grade<100) {
			this.level = 'A';
		}else if(this.grade>=70 && this.grade<80){
			this.level = 'B';
		}else if(this.grade>=60 && this.grade<70) {
			this.level = 'C';
		}else if(this.grade>=50 && this.grade<60) {
			this.level = 'D';
		}else {
			this.level = 'E';
		}
		
		System.out.println(this.level);
	}
	
	public void print() {
		System.out.println("no: " + this.no);
		System.out.println("name: " + this.name);
		System.out.println("sex: " + this.sex);
		System.out.println("major: " + this.major);
	}
}

 class GraduateStudent extends CollegeStudent{

	String supervisor;
	
	public GraduateStudent(int no, String name, String sex, String major,String supervisor) {
		super(no, name, sex, major);
		// TODO Auto-generated constructor stub
		this.supervisor = supervisor;
	}

	public String getSupervisor() {
		return supervisor;
	}

	public void setSupervisor(String supervisor) {
		this.supervisor = supervisor;
	}
	
	public void doResearch() {
		System.out.println(this.name + " is doing research");
	}
	
	public void getGrade(int grade) {
		this.grade = grade;
		
		if(this.grade>=90 && this.grade<100) {
			this.level = 'A';
		}else if(this.grade>=80 && this.grade<90){
			this.level = 'B';
		}else if(this.grade>=70 && this.grade<80) {
			this.level = 'C';
		}else if(this.grade>=60 && this.grade<70) {
			this.level = 'D';
		}else {
			this.level = 'E';
		}
		
		System.out.println(this.level);
	}
	
	public void print() {
		System.out.println("no: " + this.no);
		System.out.println("name: " + this.name);
		System.out.println("sex: " + this.sex);
		System.out.println("major: " + this.major);
		System.out.println("supervisor: " + this.supervisor);
	}
	
	
}