日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

南邮java大作业实验报告_南京邮电大学java第三次实验报告

發布時間:2025/4/16 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 南邮java大作业实验报告_南京邮电大学java第三次实验报告 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實 驗 報 告

( 2017 / 2018學年 第2學期)

課程名稱

JAVA語言程序設計

實驗名稱

Java集成開發環境的安裝與使用、

Java變量、表達式與控制結構

實驗時間

2018

4

11

指導單位

計算機學院軟件教學中心

指導教師

許棣華

學生姓名

王利國

班級學號

B160209

學院(系)

電子與光學工程學院,微電子學院

專??? 業

微電子科學與工程

實驗名稱

方法、數組和類

指導教師

許棣華

實驗類型

上機

實驗學時

2

實驗時間

2017.4.11

三、實驗內容

1. 在前面實驗二已定義的學生類Student的基礎上,以Student類為父類,為學生類派生出一個子類為大學生類(CollegeStudent)。

CollegeStudent 類在學生類上增加一個專業(profession)數據屬性;方法上增加獲得專業和設置專業兩個方法。并對超類中的toString( )方法進行重寫,使得CollegeStudent類中的toString( )方法除了顯示學生類的信息外,還要顯示它的專業屬性。

編寫測試程序的主類。在主類中創建一個Student對象和CollegeStudent對象,并顯示或修改這兩個對象的屬性值。

packagelg.test;//測試類

public classDemo31 {public static voidmain(String[] args) {

Student one= new Student( "16020912", "王寧寧","男" , 19);

CollegeStudent two= new CollegeStudent( "16020913", "王利國","男" , 19 ,"微電子科學與工程");

System.out.println("未進行修改的時候的屬性值");

System.out.println(one.toString());

System.out.println(two.toString());

System.out.println("修改后的屬性值");

one.setAge(20);

two.setProfession("微電子");

System.out.println(one.toString());

System.out.println(two.toString());

}

}classStudent {privateString studentID;privateString name;privateString sex;private intage;private static intcount;public static intgetCount() {returncount;

}

Student(String studentID, String name, String sex,intage) {this.studentID =studentID;this.name =name;this.sex =sex;this.age =age;

}

@OverridepublicString toString() {return "Student{" +

"studentID='" + studentID + '\'' +

", name='" + name + '\'' +

", sex='" + sex + '\'' +

", age=" + age +

'}';

}//studen的set和get方法

publicString getStudentID() {returnstudentID;

}public voidsetStudentID(String studentID) {this.studentID =studentID;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicString getSex() {returnsex;

}public voidsetSex(String sex) {this.sex =sex;

}public intgetAge() {returnage;

}public void setAge(intage) {this.age =age;

}public static void setCount(intcount) {

Student.count=count;

}

}//新建立的CoolegeStudent對象

class CollegeStudent extendsStudent{privateString profession;

CollegeStudent(String studentID, String name, String sex,intage, String profession) {super( studentID, name, sex, age );this.profession =profession;

}//屬性的get & set方法

publicString getProfession() {returnprofession;

}public voidsetProfession(String profession) {this.profession =profession;

}

@OverridepublicString toString() {return "CollegeStudent{" +

"profession='" + profession + '\'' +

"studentID='" + super.getStudentID() + '\'' +

", name='" + super.getName() + '\'' +

", sex='" + super.getSex() + '\'' +

", age=" + super.getAge() +

'}';

}

}

View Code

2. 設計一個人員類(Person),其中包含一個方法pay,代表人員的工資支出。再從Person類派生出助教類(Assistant)、

講師類(Instructor)、副教授類(Assistant Professor)和教授類(Professor)。其中:工資支出=基本工資+授課時數*每課時兼課金。

但助教基本工資為800,每課時兼課金25,講師基本工資為1000,每課時兼課金35,

副教授基本工資為1200,每課時兼課金40,教授基本工資為1400,每課時兼課金50。

① 將Person定義為抽象類,pay為抽象方法,設計程序實現多態性。

② 將Person定義為接口,設計程序實現多態性。

第一:通過類來實現

classPerson {public intbasic;public inthour;public intcharge;publicPerson() {

}public Person(int basic, intcharge) {this.basic =basic;this.charge =charge;

}public void pay(inthour) {

System.out.println( hour+ "小時后的工資為" + (basic + hour *charge) );

}

}class Assistant extendsPerson {publicAssistant() {super( 800, 25);

}

}class Instructor extendsPerson {publicInstructor() {super( 1000, 35);

}

}class AssistantProfessor extendsPerson {publicAssistantProfessor() {super( 1200, 40);

}

}class Professor extendsPerson {publicProfessor() {super( 1400, 50);

}

}

View Code

第二:通過抽象方法實現

abstract classPerson {public inthour;//public int basic;//public int charge;//public Person() {//}//public Person(int basic, int charge) {//this.basic = basic;//this.charge = charge;//}

public abstract void pay(inthour);

}class Assistant extendsPerson {

@Overridepublic void pay(inthour) {

System.out.println( hour+ "小時后的工資為" + (800 + hour * 25) );

}//public Assistant() {//super( 800, 25 );//}

}class Instructor extendsPerson {

@Overridepublic void pay(inthour) {

System.out.println( hour+ "小時后的工資為" + (1000 + hour * 35) );

}//public Instructor() {//super( 1000, 35 );//}

}class AssistantProfessor extendsPerson {

@Overridepublic void pay(inthour) {

System.out.println( hour+ "小時后的工資為" + (1200 + hour * 40) );

}//public AssistantProfessor() {//super( 1200, 40 );//}

}class Professor extendsPerson {

@Overridepublic void pay(inthour) {

System.out.println( hour+ "小時后的工資為" + (1400 + hour * 50) );

}//public Professor() {//super( 1400, 50 );//}

}

View Code

第三:通過接口實現

interfacePerson {void pay(inthour);

}class Assistant implementsPerson {

@Overridepublic void pay(inthour) {

System.out.println( hour+ "小時后的工資為" + (800 + hour * 25) );

}//public Assistant() {//super( 800, 25 );//}

}class Instructor implementsPerson {

@Overridepublic void pay(inthour) {

System.out.println( hour+ "小時后的工資為" + (1000 + hour * 35) );

}//public Instructor() {//super( 1000, 35 );//}

}class AssistantProfessor implementsPerson {

@Overridepublic void pay(inthour) {

System.out.println( hour+ "小時后的工資為" + (1200 + hour * 40) );

}//public AssistantProfessor() {//super( 1200, 40 );//}

}class Professor implementsPerson {

@Overridepublic void pay(inthour) {

System.out.println( hour+ "小時后的工資為" + (1400 + hour * 50) );

}//public Professor() {//super( 1400, 50 );//}

}

View Code

3. 從鍵盤輸入兩個數,進行相除,顯示商。當輸入串中含有非數字時或除數為0時,通過異常處理機制,使程序能正確運行。

packageleetcode;importjava.util.InputMismatchException;importjava.util.Scanner;/*** @Author liguo

* @Description . 從鍵盤輸入兩個數,進行相除,顯示商。當輸入串中含有非數字時或除數為0時,通過異常處理機制,使程序能正確運行。

* @Data 2018-04-03*/

public classDemo {public static voidmain(String[] args) {int oper1 = 0; //定義被除數

int oper2 = 0; //定義除數

Scanner in = newScanner( System.in );try{//數據輸入和輸出

System.out.print( "請輸入被除數:");

oper1=in.nextInt();

System.out.print("請輸入除數:");

oper2=in.nextInt();

System.out.println("計算結果:" + oper1 /oper2 );

}catch(ArithmeticException e2) {

System.out.println("異常1:除數為零!,請重新輸入除數");

oper2=in.nextInt();

System.out.println("計算結果:" + oper1 /oper2 );

}catch(InputMismatchException e1) {

System.out.println("異常2:輸入不為數字!,請重新輸入");//int one = in.nextInt();//int two = in.nextInt();//System.out.println( "計算結果:" + one / two );

System.out.print( "請輸入被除數:");

String a=in.next();

oper1=in.nextInt();

System.out.print("請輸入除數:");

oper2=in.nextInt();

System.out.println("計算結果:" + oper1 /oper2 );

}//catch (NumberFormatException e4) {;//System.out.println( "FormatException4:" + e4.getMessage() );//oper1 = in.nextInt();//oper2 = in.nextInt();//System.out.println( "計算結果:" + oper1 / oper2 );//}

finally{

System.out.println("程序結束");

}

}

}

View Code

四、實驗小結(包括問題和解決方法、心得體會等)

使用try-catch進行異常處理,遇到很多問題

1:不知道捕獲異常的種類

2:關于異常的處理

3:輸入輸出格式異常的處理,關于Scanner方法,使用in.nexInt()方法,其讀取的

都是控制臺中的第一行。

//網上測試的好多異常處理,親測并不能使用。

總結

以上是生活随笔為你收集整理的南邮java大作业实验报告_南京邮电大学java第三次实验报告的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。