201871010104-陈园园 《面向对象程序设计(java)》第四周学习总结
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 201871010104-陳園園 《面向對象程序設計(java)》第四周學習總結
?
| 項目 | 內容 |
| 這個作業屬于哪個課程 | https://www.cnblogs.com/nwnu-daizh/ |
| 這個作業要求在哪里 | https://www.cnblogs.com/lily-2018/p/11441372.html |
| 作業學習目標 |
第一部分:總結第四章理論知識
1.類與對象概念
(1)類是構造對象的模板或藍圖,由類構造對象的過程稱為創建類的實例。
? (2)對象:即數據,對象有三個特性,行為、狀態、標識。
2.類與對象的關系
(1)類是對象,事物的描述和抽象,是具有相同屬性和行為的對象集合。對象則是該類事物的實例。
?(2)類是一個靜態的概念,類本身不攜帶任何數據。當沒有為類創建任何對象時,類本身不存在于內存空間 中。對象是一個動態的概念。每一個對象都存在著有別于其它對象的屬于自己的獨特的屬性和行為。對象的屬性可以隨著它自己的行為而發生改變。
3.對象與對象變量的關系
(1).Java中想使用對象就必須先構造對象,并指定其初始狀態。
4.通過實驗掌握了預定義類的基本使用方法,熟悉Math類、String類、math類、Scanner??類、LocalDate類的常用API。
5.掌握用戶自定義類的語法規則,包括實例域、靜態域、構造器方法、更改器方法、訪問器方法、靜態方法、main方法、方法參數的定義要求
(1)實例域:可將實例域定義為final,構建對象時必須初始化這樣的域。
(2)靜態域:絕大多數面向對象程序設計語言中,靜態域被稱為類域。如果將域定義為static,每個類中只有一個這樣的域。而每個對象對于所有的實例域卻都有自己的一份拷貝。
(3)靜態方法:靜態方法是一種不能向對象實時操作的方法。可以使用對象調用靜態方法。
(4)構造器方法:構造器與類同名。構造器總是伴隨著new操作符的執行被調用,而不能對一個已經存在的對象調用構造器來達到重新設置實例域的目的。
(5)更改器方法:調用更改器方法后對象的狀態會改變。
(6)訪問器方法:只訪問對象而不修改對象的方法。
(7)main方法:main方法不對任何對象進行操作。靜態的main方法將執行并創建程序所需要的對象。
6.重載
? 多個方法有相同的名字、不同的參數、便產生了重載。Java允許重載任何方法,而不只是構造器方法。
7.包
?? Java允許使用包將類組織起來。借助包可以方便地組織自己的代碼,并將自己的代碼與別人提供的代碼庫分開管理。而且使用包可以確保類名的唯一性。
8.文檔注釋技術
包括類注釋,方法注釋,域注釋,通用注釋,包與概述注釋等。
第二部分:實驗部分
實驗名稱:實驗三 類與對象的定義及使用
1.? 實驗目的:
(1) 熟悉PTA平臺線上測試環境;
(2) 理解用戶自定義類的定義;
(3) 掌握對象的聲明;
(4) 學會使用構造函數初始化對象;
(5) 使用類屬性與方法的使用掌握使用;
(6) 掌握package和import語句的用途。
3. 實驗步驟與內容:
實驗1?任務1
公民身份證號碼按照GB11643—1999《公民身份證號碼》國家標準編制,由18位數字組成:前6位為行政區劃分代碼,第7位至14位為出生日期碼,第15位至17位為順序碼,第18位為校驗碼。從鍵盤輸入1個身份證號,將身份證號的年月日抽取出來,按年-月-日格式輸出。注意:輸入使用Scanner類的nextLine()方法,以免出錯。
代碼如下:
import java.util.Scanner;public class Main{public static void main(String[] args){Scanner in = new Scanner(System.in);String s1 = in.nextLine();String s2,s3,s4;s2 = s1.substring(6,10);s3 = s1.substring(10,12);s4 = s1.substring(12,14);System.out.println(s2+"-"+s3+"-"+s4);}}運行結果如下:
?
?
?
?
?
實驗1? 任務2
tudentfile.txt文件內容是某班同學的學號與姓名,利用此文件編制一個程序,將studentfile.txt文件的信息讀入到內存,并提供兩類查詢功能:
(1)輸入姓名查詢學號;
(2)輸入學號查詢姓名。要求程序具有友好人機交互界面。 編程建議:
(1)從文件中讀入學生信息,可以編寫如下函數: public static void StudentsFromFile(String fileName))
(2)輸入姓名查找學生學號,可以編寫如下函數: public static String findStudentName(String name)
(3)輸入學號查找學生姓名,可以編寫如下函數: public static String findStudentID(String ID)。
代碼如下:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner;public class Main1 {// private static Student students[];private static ArrayList<Student> list;public static void main(String[] args) {list = new ArrayList<>();Scanner in = new Scanner(System.in);try {readFile("studentfile.txt");System.out.println("請選擇操作,1按姓名,2按學號,3退出");int i;while ((i = in.nextInt()) != 3) {switch (i) {case 1:System.out.println("請輸入姓名");String name = in.next();Student student = findStudentByName(name);if (student == null) {System.out.println("沒找到");} else {System.out.println(student.toString());}System.out.println("請選擇操作,1按姓名,2按學號,3退出");break;case 2:System.out.println("請輸入學號");String id = in.next();Student student1 = findStudentById(id);if (student1 == null) {System.out.println("沒找到");} else {System.out.println(student1.toString());}System.out.println("請選擇操作,1按姓名,2按學號,3退出");break;default:System.out.println("輸入有誤");System.out.println("請選擇操作,1按姓名,2按學號,3退出");break;}}} catch (IOException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}finally {in.close();}}public static void readFile(String path) throws IOException {FileReader reader = new FileReader(path);BufferedReader br = new BufferedReader(reader);String result;while ((result = br.readLine()) != null) {Student student = new Student();student.setName(result.substring(13));student.setID(result.substring(0,12));list.add(student);}br.close();}public static Student findStudentByName(String name) {for (Student student : list) {if (student.getName().equals(name)) {return student;}}return null;}public static Student findStudentById(String Id) {for (Student student : list) {if (student.getID().equals(Id)) {return student;}}return null;} }class Student {private String name;private String ID;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getID() {return ID;}public void setID(String iD) {ID = iD;}@Overridepublic String toString() {// TODO 自動生成的方法存根return "姓名是:" + name + "學號是:" + ID;} }運行結果如下:
?
?
實驗2?
測試程序1
參考教材104頁EmployeeTest.java,設計StudentTest.java,定義Student類,包含name(姓名)、sex(性別)序1:
1)編輯、編譯、調試運行程序4-2(教材104頁);
2)結合程序運行結果,掌握類的定義與類對象的用法,并在程序代碼中添加類與對象知識應用的注釋;
3) 嘗試在項目中編輯兩個類文件(Employee.java、?EmployeeTest.java ),編譯并運行程序。
4)javascore(java成績)三個字段,編寫程序,從鍵盤輸入學生人數,輸入學生信息,并按以下表頭輸出學生信息表:
??姓名 ???性別? java成績
代碼如下:
import java.time.*;/*** This program tests the Employee class.* @version 1.13 2018-04-10* @author Cay Horstmann*/ public class EmployeeTest {public static void main(String[] args){// fill the staff array with three Employee objectsEmployee[] staff = new Employee[3];//構造了一個Employee數組,并填入了三個雇員對象。staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);// raise everyone's salary by 5%for (Employee e : staff)//利用Employee中的raiseSalary方法將每一個雇員的薪水提高5%e.raiseSalary(5);// print out information about all Employee objectsfor (Employee e : staff)//調用getName方法、getSalary方法和getHireDay方法將每個雇員的信息打印出來System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay());} }class Employee {private String name;private double salary;private LocalDate hireDay;public Employee(String n, double s, int year, int month, int day){name = n;salary = s;hireDay = LocalDate.of(year, month, day);}public String getName(){return name;}public double getSalary(){return salary;}public LocalDate getHireDay(){return hireDay;}public void raiseSalary(double byPercent){double raise = salary * byPercent / 100;salary += raise;} }運行結果如下:
?設計StudentTest.java,定義Student類,包含name(姓名)、sex(性別)序1:
代碼如下:
import java.util.Scanner;public class cyy {String 姓名;String 性別;double java成績;@SuppressWarnings("unused")private String 姓名3;public static void main(String[] args) {System.out.println("請輸入學生人數");Scanner sc = new Scanner(System.in);int totalStudent = sc.nextInt();cyy[] stus = new cyy[totalStudent];for(int i=0;i<totalStudent;i++){cyy s = new cyy();stus[i]=s;System.out.println("請輸入第"+(i+1)+"個學生的姓名");s.set姓名3(sc.next());System.out.println("請輸入第"+(i+1)+"個學生的性別");s.性別 = sc.next();System.out.println("請輸入第"+(i+1)+"個學生的Java成績");s.java成績 = sc.nextDouble();}printcyy(stus);sc.close();}private static void printcyy(cyy[] stus) {// TODO Auto-generated method stub}public static void printtudents(cyy[] s){System.out.println("姓名\t性別\tJava成績");for(int i=0;i<s.length;i++){System.out.println(s[i].姓名+"\t"+s[i].性別+"\t"+s[i].java成績);}}public String get姓名3() {return 姓名;}public void set姓名3(String 姓名3) {this.姓名 = 姓名3;}}運行結果如下:
?
?
?
測試程序2:
l?編輯、編譯、調試運行程序4-3(教材116);
l?結合程序運行結果,理解程序代碼,掌握靜態域(netxtId)與靜態方法(getNextId)的用法,在相關代碼后添加注釋;
l?理解Java單元(類)測試的技巧。
代碼如下:
/*** This program demonstrates static methods.* @version 1.02 2008-04-10* @author Cay Horstmann*/ public class StaticTest {public static void main(String[] args){// fill the staff array with three Employee objectsvar staff = new Employee[3];//構造Employee數組,并有三個雇員對象;staff[0] = new Employee("Tom", 40000);staff[1] = new Employee("Dick", 60000);staff[2] = new Employee("Harry", 65000);// print out information about all Employee objectsfor (Employee e : staff){e.setId(); //打印每個雇員的信息;System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="+ e.getSalary());}int n = Employee.getNextId(); // calls static methodSystem.out.println("Next available id=" + n);} } //打印每個雇員的信息; class Employee {private static int nextId = 1;private String name;private double salary;private int id;public Employee(String n, double s){//構造Employee類的對象,并聲明局部變量name,salary,hireday;name = n;salary = s;id = 0;}public String getName(){return name;}public double getSalary(){return salary;}public int getId(){return id;}public void setId(){id = nextId; // set id to next available idnextId++;}public static int getNextId(){return nextId; // returns static field}public static void main(String[] args) // unit test{var e = new Employee("Harry", 50000);System.out.println(e.getName() + " " + e.getSalary());} }
運行結果如下:
?
?
?
測試程序3:
l?編輯、編譯、調試運行程序4-4(教材121);
l?結合程序運行結果,理解程序代碼,掌握Java方法參數的用法,在相關代碼后添加注釋;
代碼如下:
/*** This program demonstrates parameter passing in Java.* @version 1.01 2018-04-10* @author Cay Horstmann*/ public class ParamTest {public static void main(String[] args){/** Test 1: Methods can't modify numeric parameters//測試方法不能修改數值參數*/System.out.println("Testing tripleValue:");double percent = 10;System.out.println("Before: percent=" + percent);tripleValue(percent);//調用 tripleValueSystem.out.println("After: percent=" + percent);/** Test 2: Methods can change the state of object parameters//調用 tripleValue*/System.out.println("\nTesting tripleSalary:");var harry = new Employee("Harry", 50000);System.out.println("Before: salary=" + harry.getSalary());tripleSalary(harry);System.out.println("After: salary=" + harry.getSalary());/** Test 3: Methods can't attach new objects to object parameters*/System.out.println("\nTesting swap:");var a = new Employee("Alice", 70000);var b = new Employee("Bob", 60000);System.out.println("Before: a=" + a.getName());System.out.println("Before: b=" + b.getName());swap(a, b);//用交換函數交換a,bSystem.out.println("After: a=" + a.getName());System.out.println("After: b=" + b.getName());}public static void tripleValue(double x) // doesn't work{x = 3 * x;System.out.println("End of method: x=" + x);}public static void tripleSalary(Employee x) // works{x.raiseSalary(200);System.out.println("End of method: salary=" + x.getSalary());}public static void swap(Employee x, Employee y){Employee temp = x;x = y;y = temp;System.out.println("End of method: x=" + x.getName());System.out.println("End of method: y=" + y.getName());} }class Employee // simplified Employee class {private String name;private double salary;//類的實例域定義來存放的需要操作的數據;public Employee(String n, double s){name = n;salary = s;}public String getName(){return name;}public double getSalary(){return salary;}public void raiseSalary(double byPercent){double raise = salary * byPercent / 100;salary += raise;} }
運行結果如下:
測試程序4:
l?編輯、編譯、調試運行程序4-5(教材129);
結合程序運行結果,理解程序代碼,掌握Java用戶自定義類的用法,掌握對象構造方法及對象使用方法,在相關代碼后添加注釋。
代碼如下:
import java.util.*;/*** This program demonstrates object construction.* @version 1.02 2018-04-10* @author Cay Horstmann*/ public class ConstructorTest {public static void main(String[] args){// fill the staff array with three Employee objectsvar staff = new Employee[3];//構造Employee數組,并有三個雇員對象;staff[0] = new Employee("Harry", 40000);staff[1] = new Employee(60000);staff[2] = new Employee();// print out information about all Employee objectsfor (Employee e : staff) //打印每個雇員的信息System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="+ e.getSalary());} }class Employee {private static int nextId;private int id;private String name = ""; // instance field initializationprivate double salary;// static initialization blockstatic{var generator = new Random();// set nextId to a random number between 0 and 9999 將nextId設置為0到999之間的隨機值nextId = generator.nextInt(10000);}// object initialization block{id = nextId;nextId++;}// three overloaded constructors //三個重載的構造public Employee(String n, double s){name = n;salary = s;}public Employee(double s){// calls the Employee(String, double) constructorthis("Employee #" + nextId, s);}// the default constructorpublic Employee(){// name initialized to ""--see above// salary not explicitly set--initialized to 0// id initialized in initialization block}public String getName(){return name; //實例域name的訪問器方法}public double getSalary(){return salary; //實例域salary的訪問器方法}public int getId(){return id; //實例域id的訪問器方法} }運行結果如下:
?
?
測試程序5:
l?編輯、編譯、調試運行程序4-6、4-7(教材135);
l?結合程序運行結果,理解程序代碼,掌握Java包的定義及用法,在相關代碼后添加注釋;
4-6代碼如下:
import com.horstmann.corejava.*; // the Employee class is defined in that packageimport static java.lang.System.*;/*** This program demonstrates the use of packages.* @version 1.11 2004-02-19* @author Cay Horstmann*/ public class PackageTest {public static void main(String[] args){// because of the import statement, we don't have to use // com.horstmann.corejava.Employee here import語句,不需要使用com.horstmann.corejavavar harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);harry.raiseSalary(5);// because of the static import statement, we don't have to use System.out hereout.println("name=" + harry.getName() + ",salary=" + harry.getSalary()); //由于使用了靜態導入語句,在這里不需要使用System.out} }4-6運行結果如下:
?4-7代碼如下:
package com.horstmann.corejava; //將類放入包中// the classes in this file are part of this packageimport java.time.*; //java.time包的導入// import statements come after the package statement 導入語句位于PACKAGE語句之后/*** @version 1.11 2015-05-08* @author Cay Horstmann*/ public class Employee {private String name;private double salary;private LocalDate hireDay;public Employee(String name, double salary, int year, int month, int day){this.name = name; //this用來引用當前對象this.salary = salary;hireDay = LocalDate.of(year, month, day);}public String getName(){return name;}public double getSalary(){return salary;}public LocalDate getHireDay(){return hireDay;}public void raiseSalary(double byPercent){double raise = salary * byPercent / 100;salary += raise;} }4-7運行結果如下:
?
?
實驗總結:
通過這次實驗,我初步了解了java中OOP的概念,學習了如何自定義類去編程,加深了對java面向對象的特點的理解。?經過過本章《對象與類》的學習?,首先對于本章的理論知識有了初步的了解。如使用預定義類,用戶自定義類,靜態域與靜態方法,還有方法參數,包以及文檔注釋等等。另外,通過實驗測試程序也對本章知識有了更多的認識。在學習了類之后,有學習了靜態域和靜態方法的用法,在這一次的學習過程當中,覺得頗有收獲,覺得比較簡單易懂,了解了靜態域、靜態常量、靜態方法、工廠方法和main方法的概念,并通過實例程序去真正理解了這些概念。希望在以后老師的講解過程中有更大的進步。
?
?
?
?
?
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/chanyeol1127/p/11568965.html
總結
以上是生活随笔為你收集整理的201871010104-陈园园 《面向对象程序设计(java)》第四周学习总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 测试用例-----听歌项目
- 下一篇: 【数据结构】— 『队列』的实现以及Lee