王艳 201771010127《面向对象程序设计(java)》第三周学习总结

一:理论知识总结:

第一章:主要概述了java相比其他程序设计语言(如C语言、c++)之间的不同性能。为我们揭示了java这种语言的设计初衷一节截至目前java语言达到的效果。另外,还简要介绍了java的诞生和发展历程。

第二章:具体讲述了如何下载和安装JDK,书上附有一些程序来说明。然后,通过对1)控制台应用2)图形应用3)applet三个具体的典型java程序的编译和运行,指导读者适用简易的JDK、可启用java文本编辑器以及一个javaIDE。通过对这一章的复习,对JDK比之前更加熟悉。

第三章:本章开始讨论java语言。设计些java语言中的基础知识,如变量、循环以及简单的函数。本章的学习内容与C语言很大的相似之处。通过这一张,我们可以使用java进行一些简单的程序的编写。

二:实验部分。

实验一:

采用个人账号登录https://pintia.cn/使用邀请码588329加入PTA平台NWNU-2017NISE教学班(西北师范大学 计算机科学与工程学院 2017级 网络与信息安全),完成《2018秋季西北师范大学面向对象程序设计(Java)(ch1-ch3)测试题1》,测试时间120分钟;

实验二:

公民身份证号码按照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学习中还有很多很多的不足,想要编写一个较为完整的程序,目前所学知识还远远不够。在以后的学习中,我定会更加努力学习,注重细节。希望在下次考试中,自己能有所提升。

posted @ 2018-09-17 00:11  JAVA!!  阅读(144)  评论(0编辑  收藏  举报