實 驗 報 告
( 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對象,並顯示或修改這兩個對象的屬性值。
package lg.test; //測試類 public class Demo31 { public static void main(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()); } } class Student { private String studentID; private String name; private String sex; private int age; private static int count; public static int getCount() { return count; } Student(String studentID, String name, String sex, int age) { this.studentID = studentID; this.name = name; this.sex = sex; this.age = age; } @Override public String toString() { return "Student{" + "studentID='" + studentID + '\'' + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; } //studen的set和get方法 public String getStudentID() { return studentID; } public void setStudentID(String studentID) { this.studentID = studentID; } 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void setCount(int count) { Student.count = count; } } //新建立的CoolegeStudent對象 class CollegeStudent extends Student{ private String profession; CollegeStudent(String studentID, String name, String sex, int age, String profession) { super( studentID, name, sex, age ); this.profession = profession; } //屬性的get & set方法 public String getProfession() { return profession; } public void setProfession(String profession) { this.profession = profession; } @Override public String toString() { return "CollegeStudent{" + "profession='" + profession + '\'' + "studentID='" + super.getStudentID() + '\'' + ", name='" + super.getName() + '\'' + ", sex='" + super.getSex() + '\'' + ", age=" + super.getAge() + '}'; } }
2. 設計一個人員類(Person),其中包含一個方法pay,代表人員的工資支出。再從Person類派生出助教類(Assistant)、
講師類(Instructor)、副教授類(Assistant Professor)和教授類(Professor)。其中:工資支出=基本工資+授課時數*每課時兼課金。
但助教基本工資為800,每課時兼課金25,講師基本工資為1000,每課時兼課金35,
副教授基本工資為1200,每課時兼課金40,教授基本工資為1400,每課時兼課金50。
① 將Person定義為抽象類,pay為抽象方法,設計程序實現多態性。
② 將Person定義為接口,設計程序實現多態性。
第一:通過類來實現
class Person { public int basic; public int hour; public int charge; public Person() { } public Person(int basic, int charge) { this.basic = basic; this.charge = charge; } public void pay(int hour) { System.out.println( hour + "小時后的工資為" + (basic + hour * charge) ); } } class Assistant extends Person { public Assistant() { super( 800, 25 ); } } class Instructor extends Person { public Instructor() { super( 1000, 35 ); } } class AssistantProfessor extends Person { public AssistantProfessor() { super( 1200, 40 ); } } class Professor extends Person { public Professor() { super( 1400, 50 ); } }
第二:通過抽象方法實現
abstract class Person { public int hour; // public int basic; // public int charge; // public Person() { // } // public Person(int basic, int charge) { // this.basic = basic; // this.charge = charge; // } public abstract void pay(int hour); } class Assistant extends Person { @Override public void pay(int hour) { System.out.println( hour + "小時后的工資為" + (800 + hour * 25) ); } // public Assistant() { // super( 800, 25 ); // } } class Instructor extends Person { @Override public void pay(int hour) { System.out.println( hour + "小時后的工資為" + (1000 + hour * 35) ); } // public Instructor() { // super( 1000, 35 ); // } } class AssistantProfessor extends Person { @Override public void pay(int hour) { System.out.println( hour + "小時后的工資為" + (1200 + hour * 40) ); } // public AssistantProfessor() { // super( 1200, 40 ); // } } class Professor extends Person { @Override public void pay(int hour) { System.out.println( hour + "小時后的工資為" + (1400 + hour * 50) ); } // public Professor() { // super( 1400, 50 ); // } }
第三:通過接口實現
interface Person { void pay(int hour); } class Assistant implements Person { @Override public void pay(int hour) { System.out.println( hour + "小時后的工資為" + (800 + hour * 25) ); } // public Assistant() { // super( 800, 25 ); // } } class Instructor implements Person { @Override public void pay(int hour) { System.out.println( hour + "小時后的工資為" + (1000 + hour * 35) ); } // public Instructor() { // super( 1000, 35 ); // } } class AssistantProfessor implements Person { @Override public void pay(int hour) { System.out.println( hour + "小時后的工資為" + (1200 + hour * 40) ); } // public AssistantProfessor() { // super( 1200, 40 ); // } } class Professor implements Person { @Override public void pay(int hour) { System.out.println( hour + "小時后的工資為" + (1400 + hour * 50) ); } // public Professor() { // super( 1400, 50 ); // } }
3. 從鍵盤輸入兩個數,進行相除,顯示商。當輸入串中含有非數字時或除數為0時,通過異常處理機制,使程序能正確運行。
package leetcode; import java.util.InputMismatchException; import java.util.Scanner; /** * @Author liguo * @Description . 從鍵盤輸入兩個數,進行相除,顯示商。當輸入串中含有非數字時或除數為0時,通過異常處理機制,使程序能正確運行。 * @Data 2018-04-03 */ public class Demo { public static void main(String[] args) { int oper1 = 0; //定義被除數 int oper2 = 0; //定義除數 Scanner in = new Scanner( 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( "程序結束" ); } } }
四、實驗小結(包括問題和解決方法、心得體會等)
使用try-catch進行異常處理,遇到很多問題
1:不知道捕獲異常的種類
2:關於異常的處理
3:輸入輸出格式異常的處理,關於Scanner方法,使用in.nexInt()方法,其讀取的
都是控制台中的第一行。
//網上測試的好多異常處理,親測並不能使用。
