馬凱軍201771010116《面向對象程序設計(java)》第三周學習總結
第一部分 理論知識學習與復習部分
1、在第一章裏主要對Java中常見的誤解這部分進行了細讀,也對Java的“白皮書”術語認真的看了一遍,對Java術語有了更深的理解。
2、在第二章中對Java程序的編輯環境eclipse通過課本理論知識的學習,以及在電腦上通過程序的運行,對Java環境更加的熟悉,也對編輯界面的一些快捷鍵有了初步的掌握和了解
3、從第三章中對平時在編程中常出現的字符串,控制流進行了學習和鞏固,為自己以後的編程打下堅實的基礎, 也認真學習了函數的調用,多重選擇switch語句理論知識部分,並且通過程序的編程進行了理解。
1、實驗目的與要求
(1)進一步掌握Eclipse集成開發環境下java程序開發基本步驟;
(2)熟悉PTA平臺線上測試環境;
(3)掌握Java語言構造基本程序語法知識(ch1-ch3);
(4)利用已掌握Java語言基本程序設計知識,學習設計開發含有一個主類、類內可有多個方法的應用程序。
2、實驗內容和步驟
實驗1:采用個人賬號登錄https://pintia.cn/,使用邀請碼588329加入PTA平臺NWNU-2017NISE教學班(西北師範大學 計算機科學與工程學院 2017級 網絡與信息安全),完成《2018秋季西北師範大學面向對象程序設計(Java)(ch1-ch3)測試題1》,測試時間120分鐘;
實驗2-實驗3在課後完成
實驗2:公民身份證號碼按照GB11643—1999《公民身份證號碼》國家標準編制,由18位數字組成:前6位為行政區劃分代碼,第7位至14位為出生日期碼,第15位至17位為順序碼,第18位為校驗碼。從鍵盤輸入1個身份證號,將身份證號的年月日抽取出來,按年-月-日格式輸出。註意:輸入使用Scanner類的nextLine()方法,以免出錯。
輸入樣例:
34080019810819327X
輸出樣例:
1981-08-19
package ssfs;
import java.util.Scanner;
public class erwwq {
String year;
String month;
String day;
static String id;
@SuppressWarnings("resource")
public void InputId() {
Scanner sc = new Scanner(System.in);
System.out.println("ID:");
id = sc.nextLine();
}
public void CutId(String id){
year = id.substring(6, 10);
month = id.substring(10, 12);
day = id.substring(12, 14);
}
public String toString(){
return "出身日期:" + year + " - " + month + " - " + day ;
}
public static void main(String[] args) {
erwwq peopleId = new erwwq();
peopleId.InputId();
peopleId.CutId(id);
System.out.println(peopleId.toString());
}
}
實驗3:studentfile.txt文件內容是本班同學的學號與姓名,利用此文件編制一個程序,將studentfile.txt文件的信息讀入到內存,並提供兩類查詢功能:(1)輸入姓名查詢學號;(2)輸入學號查詢姓名。要求程序具有友好人機交互界面。
編程建議:
(1)從文件中讀入學生信息,可以編寫如下函數:
public static void StudentsFromFile(String fileName))
(2)輸入姓名查找學生學號,可以編寫如下函數:
public static String findStudent(String name)
(3)輸入學號查找學生姓名,可以編寫如下函數:
package ssfs;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class wed {
private static ArrayList<Student> studentList = null;
public static void StudentsFromFile(String fileName){
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
String str[] = tempString.split(" ");
if(studentList != null && str.length > 1) {
Student student = new Student();
student.setStudentId(str[0]);
studentList.add(student);
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
public static String findStudentIdByName(String name) {
String studentId = null;
for(Student student : studentList) {
if(student.getName().equals(name)) {
studentId = student.getStudentId();
break;
}
}
return studentId;
}
public static String findStudentNameById(String ID) {
String studentName = null;
for(Student student : studentList) {
if(student.getStudentId().equals(ID)) {
studentName = student.getName();
break;
}
}
return studentName;
}
public static void main(String args[]) {
String path = "D:/studentfile.txt";
studentList = new ArrayList<Student>();
StudentsFromFile(path);
int statu = 1;
System.out.println();
while(statu != 0) {
System.out.println("1:通過姓名查詢學生學號");
System.out.println("2:通過學號查詢學生姓名");
System.out.println("0:退出");
Scanner scanner = new Scanner(System.in);
statu = scanner.nextInt();
switch(statu) {
case 1:{
System.out.println("請輸入學生姓名:");
Scanner scanner1 = new Scanner(System.in);
String name = scanner1.nextLine();
String Id = findStudentIdByName(name);
if(Id != null) {
System.out.println("姓名: "+name+" 學號: "+Id);
}else {
System.out.println("不存在該學生!請重新查找");
}
}break;
case 2:{
System.out.println("請輸入學生學號:");
Scanner scanner2 = new Scanner(System.in);
String Id = scanner2.nextLine();
String name = findStudentNameById(Id);
if(name != null) {
System.out.println("姓名: "+name+" 學號: "+Id);
}else {
System.out.println("不存在該學生!請重新查找");
}
}break;
case 0:
statu = 0; break;
default:
System.out.println("輸入錯誤");
}
} System.out.println("byebye!");
}
}
實驗總結:
首先對前三章的理論知識內容詳細的過了一遍,對自己一些沒有掌握的的知識進行了回顧和再次的學習,主要在這次的學習中掌握了Java語言構造基本程序語法知識(ch1-ch3),也對Java環境的字符串,控制流程有了深入的了解,在第二個實驗中對輸入使用Scanner類的nextLine()方法也有了初步的掌握,在第三個實驗中主要練習了一下文件的輸入和輸出,在這個過程中掌握了輸入文件的代碼格式,也對多重選擇:switch語句的基本結構有了掌握和了解。通過這麽多的學習和程序運行的實踐,感覺自己還是很多知識不知道,自己寫一個簡單完整程序還是非常的困難,也許是自己C語言基礎沒紮好,現在學習Java時感覺有許多障礙,還是要在程序的編寫上多下功夫,這樣才會發現自己那裏不足,這樣才會讓自己編寫出更加好和完整的程序。
馬凱軍201771010116《面向對象程序設計(java)》第三周學習總結