導航:首頁 > 編程語言 > 大一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考試編程題相關的資料

熱點內容
代碼加密常用方法 瀏覽:950
安卓手機如何解除已禁用 瀏覽:396
演算法的隨機性 瀏覽:485
高中解壓體育游戲 瀏覽:532
androidstudior丟失 瀏覽:345
命令行筆記 瀏覽:737
360目標文件夾訪問拒絕 瀏覽:518
3b編程加工指令 瀏覽:789
c8051f系列單片機選型手冊 瀏覽:772
南昌php程序員 瀏覽:511
bcs命令 瀏覽:446
如何在伺服器指向域名 瀏覽:417
車床編程可以做刀嗎 瀏覽:519
ln命令源碼 瀏覽:791
用粘液做解壓手套 瀏覽:331
icloud收信伺服器地址 瀏覽:500
編程思考者 瀏覽:453
壓縮機型號用什麼氟利昂 瀏覽:553
農機空氣壓縮機 瀏覽:666
程序員下載歌曲 瀏覽:897