導航:首頁 > 編程語言 > 大一java考試編程題

大一java考試編程題

發布時間:2024-03-17 00:16:38

java編程題 本人新手,求詳解。

先看下最終的結果吧,是不是你想要的?

其中,Student是父類,PostGraate是子類,繼承自父類Student,Main是主類,用於創建對象以及把這些對象的功能調用起來。

---------------------------Student代碼如下:------------------------------

/**

* 學生類

* @author 逍遙

*

*/

public class Student {

//學號

private int sId;

//姓名

private String sName;

//數學成績

private double mathScore;

//計算機成績

private double computerScore;

/**

* 獲取學號

* @return

*/

public int getsId() {

return sId;

}

/**

* 設置學號

* @param sId

*/

public void setsId(int sId) {

this.sId = sId;

}

/**

* 獲取姓名

* @return

*/

public String getsName() {

return sName;

}

/**

* 設置姓名

* @param sName

*/

public void setsName(String sName) {

this.sName = sName;

}

/**

* 獲取數學成績

* @return

*/

public double getMathScore() {

return mathScore;

}

/**

* 設置數學成績

* @param mathScore

*/

public void setMathScore(double mathScore) {

this.mathScore = mathScore;

}

/**

* 獲取計算機成績

* @return

*/

public double getComputerScore() {

return computerScore;

}

/**

* 設置計算機成績

* @param computerScore

*/

public void setComputerScore(double computerScore) {

this.computerScore = computerScore;

}

/**

* 輸出成員變數(4個成員變數)的信息。

*/

public void print(){

System.out.println("學號:"+sId);

System.out.println("姓名:"+sName);

System.out.println("計算機成績:"+mathScore);

System.out.println("數學成績:"+computerScore);

}

}

---------------------------Student代碼結束------------------------------

---------------------------PostGraate代碼如下:------------------------------

/**

* 研究生類

* @author 逍遙

*

*/

public class PostGraate extends Student{

//導師姓名

private String tName;

//研究方向

private String ResearchDirection;

/**

* 獲取導師姓名

* @return

*/

public String gettName() {

return tName;

}

/**

* 設置導師姓名

* @param tName

*/

public void settName(String tName) {

this.tName = tName;

}

/**

* 獲取研究方向

* @return

*/

public String getResearchDirection() {

return ResearchDirection;

}

/**

* 設置研究方向

* @param researchDirection

*/

public void setResearchDirection(String researchDirection) {

ResearchDirection = researchDirection;

}

/**

* 研究生類重寫父類的void print()方法,功能是輸出成員變數(6個成員變數)的信息

*/

@Override

public void print() {

// TODO Auto-generated method stub

super.print();

System.out.println("導師姓名:"+tName);

System.out.println("研究方向:"+ResearchDirection);

}

}

---------------------------PostGraate代碼結束------------------------------

---------------------------Main代碼如下:------------------------------

import java.util.Scanner;


/**

* 主類

* @author 逍遙

*

*/

public class Main {


/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

//用於獲取從鍵盤上輸入的信息

Scanner input=new Scanner(System.in);

//創建一個Student類的對象

Student student=new Student();

//從鍵盤上輸入其屬性信息

System.out.print("請輸入學生的學號:");

student.setsId(input.nextInt());

System.out.print("請輸入學生的姓名:");

student.setsName(input.next());

System.out.print("請輸入學生的數學成績:");

student.setMathScore(input.nextDouble());

System.out.print("請輸入學生的計算機成績:");

student.setComputerScore(input.nextDouble());

//並且通過其print方法輸出這些信息;

student.print();

//創建一個PostGraate類的對象

PostGraate postGraate=new PostGraate();

//從鍵盤上輸入其屬性信息

System.out.print("請輸入研究生的學號:");

postGraate.setsId(input.nextInt());

System.out.print("請輸入研究生的姓名:");

postGraate.setsName(input.next());

System.out.print("請輸入研究生的數學成績:");

postGraate.setMathScore(input.nextDouble());

System.out.print("請輸入研究生的計算機成績:");

postGraate.setComputerScore(input.nextDouble());

System.out.print("請輸入研究生的導師姓名:");

postGraate.settName(input.next());

System.out.print("請輸入研究生的研究方向:");

postGraate.setResearchDirection(input.next());

//並且通過其print方法輸出這些信息。

postGraate.print();

}


}

---------------------------Main代碼結束------------------------------

=================知識點的簡單總結=================

本題考察的知識點是面向對象的三大特性之一:繼承。

Student為父類,包含了學號、姓名、數學成績和計算機成績4個屬性,以及一個print()方法。

PostGraate 繼承父類的時候,繼承了父類中的所有方法,因為方法我都是用的public,而屬性繼承不了,因為我在父類中用了封裝,所有屬性都用private修飾了,想訪問屬性的話,必須通過get、set方法,這里,我重寫了父類中的print方法,通過super.print();調用了父類中的print()方法。

最後就是Main類,提供了main方法作為入口函數,用於按要求聲明這些對象以及去調用對象中的方法。

閱讀全文

與大一java考試編程題相關的資料

熱點內容
阿里伺服器有什麼作用 瀏覽:749
java程序開發範例 瀏覽:888
java17formac下載 瀏覽:614
rrc是什麼單片機 瀏覽:530
linux默認網關設置 瀏覽:679
java函數return 瀏覽:25
鄭州app積分商城哪裡好 瀏覽:610
7za命令解壓zip 瀏覽:906
硬碟加密無法進入系統 瀏覽:560
四葉花演算法 瀏覽:741
寶寶濾鏡app哪裡下載 瀏覽:1002
java保護代碼 瀏覽:806
游戲海外伺服器什麼意思 瀏覽:568
快手網紅程序員排名 瀏覽:99
首先會通過什麼尋找伺服器的ip地址 瀏覽:199
網頁代碼加密解碼 瀏覽:285
wyks1ms文件夾 瀏覽:93
什麼app可以看柯南高清 瀏覽:425
加密的盤文件恢復 瀏覽:22
絕對路徑能查找隱藏文件夾嗎 瀏覽:568