王豔 201771010127《面向對象程序設計(java)》第三週學習總結

一:理論知識總結:java

第一章:主要概述了java相比其餘程序設計語言(如C語言、c++)之間的不一樣性能。爲咱們揭示了java這種語言的設計初衷一節截至目前java語言達到的效果。另外,還簡要介紹了java的誕生和發展歷程。c++

第二章:具體講述瞭如何下載和安裝JDK,書上附有一些程序來講明。而後,經過對1)控制檯應用2)圖形應用3)applet三個具體的典型java程序的編譯和運行,指導讀者適用簡易的JDK、可啓用java文本編輯器以及一個javaIDE。經過對這一章的複習,對JDK比以前更加熟悉。數組

第三章:本章開始討論java語言。設計些java語言中的基礎知識,如變量、循環以及簡單的函數。本章的學習內容與C語言很大的類似之處。經過這一張,咱們可使用java進行一些簡單的程序的編寫。安全

二:實驗部分。網絡

實驗一:app

採用我的帳號登陸https://pintia.cn/使用邀請碼588329加入PTA平臺NWNU-2017NISE教學班(西北師範大學 計算機科學與工程學院 2017級 網絡與信息安全),完成《2018秋季西北師範大學面向對象程序設計(Java)(ch1-ch3)測試題1》,測試時間120分鐘;eclipse

實驗二:編輯器

公民身份證號碼按照GB11643—1999《公民身份證號碼》國家標準編制,由18位數字組成:前6位爲行政區劃分代碼,第七位至14位爲出生日期碼,第15位至17位爲順序碼,第18位爲校驗碼。從鍵盤輸入1個身份證號碼,將身份證號的年月日抽取出來,按年-月-日格式輸出。注意:輸入使用Scanner類的nextLine()方法,以避免出錯。函數

實驗步驟:工具

程序中要用到Scanner類,而Scanner類是定義在工具包中,故需調用Util工具包。用substring截取指定長度的數組。

在eclipse中編寫以下程序:

package workPractice.work1;

import java.util.*;

public class ID {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("輸入身份證號碼:");
String L = in.nextLine();
if (L.length() == 18) {
String year = L.substring(6, 10);
String month = L.substring(10, 12);
String day = L.substring(12, 14);

System.out.println(year + "-" + month + "-" + day);
} else {
System.out.println("您輸入的身份證號碼有誤!");
}
}

}

程序運行結果如所示:

實驗三:

studentfile.txt文件內容是本班同窗的學號與姓名,利用此文件編制一個程序,將studentfile.txt文件的信息讀入到內存,並提供兩類查詢功能:(1)輸入姓名查詢學號;(2)輸入學號查詢姓名。要求程序具備友好人機交互界面。

在eclipse中編寫以下程序:

package workPractice.work1;

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 Test {
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]);
student.setName(str[1]);
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!");

}
}

因爲在此程序中使用了ArrayList<Student> studentList,因此須要新建一個student類,程序以下所示

package workPractice.work1;

public class Student {
String studentId = null;
String name = null;

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;
}

}

程序運行結果如圖所示:

2.實驗總結:

       經過前三週的學習,在本週的實驗課上進行考試時,選擇題和判斷題這些考察基礎知識的題作起來以爲還好,但到後面須要編寫程序時,仍是以爲頗有困難。只有第一個程序馬馬虎虎能夠算能寫的出來,後面幾個就以爲很吃力,並且編寫一個程序須要花費很長時間。這都是對程序編寫知識還不夠了解、學習還不夠深刻致使的。對於實驗二和實驗三,好比如何截取指定片斷、如何編寫某些具體函數就須要在書上一遍遍查找。尤爲是實驗三,基本就是徹底不會,最後在同窗的幫助下寫完了實驗三,發現有不少知識本身都不會。

      經過此次本身編寫程序,我發現本身在Java學習中還有不少不少的不足,想要編寫一個較爲完整的程序,目前所學知識還遠遠不夠。在之後的學習中,我定會更加努力學習,注重細節。但願在下次考試中,本身能有所提高。

相關文章
相關標籤/搜索