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

熱點內容
遠程訪問伺服器ip地址 瀏覽:312
程序員吃雞蛋炒菜 瀏覽:173
在哪裡看俄羅斯電視劇app 瀏覽:308
怎麼找資料庫伺服器地址 瀏覽:486
伺服器調試怎麼翻譯 瀏覽:921
php如何處理ajax請求 瀏覽:211
php數組下標存在 瀏覽:707
php獲取ip歸屬地 瀏覽:175
撩女程序員怎麼辦 瀏覽:508
百度伺服器做什麼 瀏覽:192
打開軟體加速伺服器有什麼危害 瀏覽:87
php去除數組下標 瀏覽:794
ipad的app內容哪裡看 瀏覽:284
遇見空間app在哪裡 瀏覽:547
用命令對一個文件內容進行統計 瀏覽:317
華為交換機配置命令縮寫 瀏覽:344
鏈接伺服器出現問題怎麼處理 瀏覽:833
華為手機怎麼打開加密 瀏覽:676
開利中央空調壓縮機 瀏覽:742
程序員面臨脫發 瀏覽:872