201771010126王燕《面向对象程序设计(Java)》第三周学习总结

 

实验三 Java基本程序设计(2

实验时间 2018-9-13

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()方法,以免出错。

 1 import java.util.Scanner;
 2 public class lx {
 3 
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub  
 6                 @SuppressWarnings("resource")
 7                 Scanner sc = new Scanner(System.in);
 8                 System.out.println("hat is your ID:");
 9                 String n = sc.nextLine();
10                 int i = 6;
11                 String a = n.substring(i, i+4);
12                 String b = n.substring(i+4, i+6);
13                 String c = n.substring(i+6, i+8);        
14                 System.out.println(a+"-"+b+"-"+c);
15             }
16         }

实验3studentfile.txt文件内容是本班同学的学号与姓名,利用此文件编制一个程序,将studentfile.txt文件的信息读入到内存,并提供两类查询功能:(1)输入姓名查询学号;(2)输入学号查询姓名。要求程序具有友好人机交互界面。

编程建议:

(1)从文件中读入学生信息,可以编写如下函数:

public static void StudentsFromFile(String fileName))

(2)输入姓名查找学生学号,可以编写如下函数:

public static String findStudent(String name)

(3)输入学号查找学生姓名,可以编写如下函数:

public static String findStudent(String ID)

 

  1 package defalt;
  2 import java.io.BufferedReader;
  3 import java.io.File;
  4 import java.io.FileReader;
  5 import java.io.IOException;
  6 import java.util.ArrayList;
  7 import java.util.Scanner;
  8 
  9 public class lx2{

10 private static ArrayList<Student>studentList = null;
 11 private static Scanner in;
 12 private static Scanner scanner1;
 13 private static Scanner scanner2;
 14 public static void StudentsFromFile(String fileName){
 15 
 16 File file = new File(fileName); 
 17 BufferedReader reader = null; 
 18 try { 
 19 reader = new BufferedReader(new FileReader(file)); 
 20 String tempString = null; 
 21 while ((tempString = reader.readLine()) != null) { 
 22 String str[] = tempString.split(" ");
 23 if(studentList!= null && str.length > 1) {
 24 //Object studentId;
 25 Student student = new Student();
 26 student.setStudentId(str[0]);
 27 student.setName(str[1]);
 28 studentList.add(student);
 29 }
 30 } 
 31 reader.close(); 
 32 } catch (IOException e) { 
 33 e.printStackTrace(); 
 34 } finally { 
 35 if (reader != null) { 
 36 try { 
 37 reader.close(); 
 38 } catch (IOException e1) { 
 39 } 
 40 } 
 41 } 
 42 }
 43 /**
 44 * @param name
 45 * @return
 46 */
 47 public static String findStudentIdByName(String name) {
 48 String studentId = null;
 49 for(Student student : studentList) {
 50 if(student.getName().equals(name)) {
 51 studentId = student.getStudentId();
 52 break;
 53 }
 54 }
 55 return studentId;
 56 }
 57 public static String findStudentNameById(String ID) {
 58 String studentName = null;
 59 for(Student student : studentList) {
 60 if(student.getStudentId().equals(ID)) {
 61 studentName = student.getName();
 62 break;
 63 }
 64 }
 65 return studentName;
 66 }
 67 public static void main(String args[]) {
 68 String path = "C:\\Users\\Dell\\Documents\\Tencent Files\\1689164469\\FileRecv\\实验三/studentfile.txt";
 69 studentList = new ArrayList<Student>();
 70 StudentsFromFile(path);
 71 int statu = 1;
 72 System.out.println();
 73 while(statu != 0) {
 74 System.out.println("******************");
 75 System.out.println("1:通过姓名查询学号");
 76 System.out.println("2:通过学号查询姓名");
 77 System.out.println("0:退出");
 78 System.out.println("******************");
 79 in = new Scanner(System.in);
 80 statu = in.nextInt();
 81 
 82 switch(statu) {
 83 case 1:{ System.out.println("请输入学生姓名:");
 84 scanner1 = new Scanner(System.in);
 85 String name = scanner1.nextLine();
 86 String Id = findStudentIdByName(name);
 87 if(Id != null) {
 88 System.out.println("姓名: "+name+" 学号: "+Id);
 89 }else {
 90 System.out.println("不存在该学生!请重新查找");
 91 }
 92 
 93 }break;
 94 case 2:{
 95 System.out.println("请输入学生学号:");
 96 scanner2 = new Scanner(System.in);
 97 String Id = scanner2.nextLine();
 98 String name = findStudentNameById(Id);
 99 if(name != null) {
100 System.out.println("姓名: "+name+" 学号: "+Id);
101 }else {
102 System.out.println("不存在该学生!请重新查找");
103 }
104 }break;
105 case 0:
106 statu = 0; break;
107 default:
108 System.out.println("输入错误");
109 }
110 } 
111 System.out.println("byebye!");
112 }
113 
114 }

实验总结:

 在这一系列的实验中,我基本掌握了Java的编程规则、知识要点和一些小技巧,特别是对面向对象的编程思想和风格有了进一步的认识和体会.同时让我对编程更加感兴趣.对于在这些实验过程中、同学互助、查阅资料等基本的学习方式,使我更加领悟到集体和团队的力量,也树立了敢于攻坚的信心.

 

posted @ 2018-09-19 19:15  "o_o"  阅读(204)  评论(0编辑  收藏  举报