题目一:文件读写

本次实验需要将Unit3-2中的复试系统改为从文件中读取试题信息并将部分学生试卷信息输出到文件中。
目的:增强使用文件读写的能力
背景:在此作业中,您将创建另一个版本的复试系统。在以前的版本中,试题库的试题数据在应用程序中进行了硬编码。 在此版本中,将从文件中加载数据。 而且,用户将能够以以下三种格式之一将学生试卷信息写入文件:纯文本,HTML或XML。部分工作已为您完成,并在学生资料中提供。您将实现加载试题库的试题数据并保存学生试卷信息的代码。

数据文件

试题类型主要分为三种:英语题、数学题和专业课题。testCatalog.dat文件存储着试题库数据。testCatalog.dat中的每一行只包含一个试题。字段由下划线分割,假定字段本身不包含任何下划线。

英语题数据的格式

EnglishTest_code_title_difficultyDegree_scoreCriteria_type

其中:EnglishTest是标识该行试题类型的前缀;code(string)表示英语题的代码;title(string)表示英语题的题干;difficultyDegree(int)表示英试题的难度系数,其后两项均为string类型,分别表示英试题的得分标准和类型。

数学题数据的格式

MathTest_code_title_difficultyDegree_scoreCriteria_photoURL_calculationProcess

其中:MathTest是标识该行试题类型的前缀;code(string)表示数学题代码;title(string)表示数学题题干;difficultyDegree(int)表示数学题的难度系数,其后三项均为string类型,分别表示数学题的得分标准、图片地址和计算过程。

专业题数据的格式

ProfessionalTest_code_title_difficultyDegree_scoreCriteria_programInstruction_programming_photoURL

其中:ProfessionalTest是标识该行试题类型的前缀;code(string)表示专业
题代码;title(string)表示专业题题干;difficultyDegree(int)表示专业题的难度系数,其后四项均为string类型,分别表示专业题的得分标准、程序说明、程序体和图片地址。

类图

部分类与接口说明

接口FushiTestDatabaseLoader

接口FushiTestDatabaseLoader声明用于生成试题库的方法。
方法:
Catalog loadTestDatabase(String fileName) throws FileNotFoundException,
IOException,
DataFormatException
此方法将指定文件中的信息加载到试题库中并返回试题库。

类DataFormatException

当正在分析的文件中有一行出现以下错误时,将抛出异常。
1)该行没有预期数量的字段;
2)应包含数字的字段却没有包含数字。

类FileFushiTestDatabaseLoader

类FileFushiTestDatabaseLoader实现接口FushiTestDatabaseLoader.它用于从文件中获取试题库。
方法:
1.private EnglishTest readEnglishTest(String line) throws DataFormatException
如果该行没有错误,则此方法返回一个封装英语试题数据的EnglishTest对象。

2.private MathTest readMathTest (String line) throws DataFormatException
如果该行没有错误,则此方法返回一个封装数学试题数据的MathTest对象。

3.private ProfessionalTest readProfessionalTest(String line) throws DataFormatException
如果该行没有错误,则此方法返回一个封装专业题数据的ProfessionalTest对象。

以上方法若有错误均引发一个DataFormatException
4.public TestDatabase loadTestDatabase (String filename) throws FileNotFoundException,
IOException,
DataFormatException
此方法将指定文件中的信息加载到试题库中并试题库。它首先打开文件然后读取并处理文件中的每一行。使用方法String.startsWith确定每行数据的行类型。

类FushiSystem

writeFile方法使用指定的名称创建一个新文件,将指定的字符串写入该文件,然后关闭该文件。
其他方法可直接在实验Unit3-2上进行更改或直接沿用Unit3-2中的方法。

任务:

1.根据题目需求实现复试系统,请添加必要的注释。
2.将试题数据文件路径配置到程序运行参数中。
3.与Unit3-2相同,系统会提示选项列表,供用户选择需要输出的格式。当用户选择特定的格式后,系统会创建相应的包含学生试卷信息的文件。
4.实验需要写一个简单的说明文档,其中包括必要的运行说明及运行结果截图。其中包括系统对不同错误的处理反馈。
5.实验提交内容:项目文件及说明文档。
6.实验的输出结果必须和下面展示的结果一致。(此处学生试卷信息与Unit3-2不同,请注意!)

纯文本:

HTML:


XML:

代码展示

这次的题目输出较实验三的有所不同,其中Plain和XML输出格式变化不大,所以重点展示HTML格式
由于输出的不同种类的Test,有不一样多的元素,所以使用foreach循环遍历学生的exampaper后,要将testitem中的test向下转型再输出,代码如下:

HTML

public class HTMLStudentsFormatter implements StudentsFormatter{/*** singletonInstance 作为调用formatStudents方法的通道*/private static HTMLStudentsFormatter singletonInstance=null;/*** singletonInstance的无参构造方法*/public HTMLStudentsFormatter(){}/*** @param studentCatalog 传入一个需要输出信息的学生列表** @return s 返回输出字符串*/@Overridepublic String formatStudents(StudentCatalog studentCatalog) {String s="\n<html>\n";s=s+"  <body>\n";s=s+"    <center><h2>Student Catalog</h2></center>\n";for(Student student : studentCatalog.students){s=s+"    <hr>\n";s=s+"    <h4>"+student.getId()+" "+student.getName()+"</h4>\n";s=s+"      <blockquote>\n";String tmp1="";for(TestItem testitem : student.getExamPaper().testitem){String tmp=new String (tmp1+testitem.getTest ().getCode ().charAt (0));//通过code属性的第一个字符来判定进行那种Test的输出if(tmp.equals ("M")){s=s+"        "+testitem.getTest ().getCode ()+"|"+testitem.getTest ().getTitle ()+"|"+testitem.getTest ().difficultyDegree+"|"+testitem.getTest ().scoreCriteria+"|"+((MathTest)testitem.getTest ()).getPhotoURL ()+"|"+((MathTest)testitem.getTest ()).getcalculationProcess ()+"<br/>";}else if(tmp.equals ("P")){s=s+"        "+testitem.getTest ().getCode ()+"|"+testitem.getTest ().getTitle ()+"|"+testitem.getTest ().difficultyDegree+"|"+testitem.getTest ().scoreCriteria+"|"+((ProfessionalTest)testitem.getTest ()).getprograminstruction ()+"|"+((ProfessionalTest)testitem.getTest ()).programming+"|"+((ProfessionalTest)testitem.getTest ()).getpotoURL ()+"<br/>";}else if(tmp.equals ("E")){s=s+"        "+testitem.getTest ().getCode ()+"|"+testitem.getTest ().getTitle ()+"|"+testitem.getTest ().difficultyDegree+"|"+testitem.getTest ().scoreCriteria+"|"+((EnglishTest)testitem.getTest ()).gettype ()+"<br/>";}tmp1="";s=s+"\n";}s=s+"      </blockquote>\n";}s=s+"  </body>\n";s=s+"</html>";return s;}/*** @return singletonInstance 当singletonInstance为空时创建对象*/public static HTMLStudentsFormatter getSingletonInstance() {if(singletonInstance==null){singletonInstance=new HTMLStudentsFormatter();}return singletonInstance;}
}

DataFormatException

//自定义异常基本格式
public class DataFormatException extends Exception{public DataFormatException(){super();}public DataFormatException(String message){super(message);}
}

FileFushiTestDatabaseLoader

import java.io.*;
import java.lang.*;
import java.util.ArrayList;
public class FileFushiTestDatabaseLoader implements FushiTestDatabaseLoader{private final ArrayList<String> data=new ArrayList<> ();/*** * @param line 读取testcatalog.dat中的一行字符串* @return  返回一个EnglishTest*/private EnglishTest readEnglishTest(String line){String[] DATA=line.split ("_");if(DATA.length!=6){try {throw new DataFormatException("This line contains incomplete data");} catch (DataFormatException e) {e.printStackTrace ();}}int m=Integer.parseInt (DATA[3]);return new EnglishTest (DATA[1],DATA[2],m,DATA[4],DATA[5]);}/*** * @param line 读取testcatalog.dat中的一行字符串* @return 返回一个EnglishTest*/private MathTest readMathTest(String line){String[] DATA=line.split ("_");if(DATA.length!=7){try {throw new DataFormatException("This line contains incomplete data");} catch (DataFormatException e) {e.printStackTrace ();}}int m=Integer.parseInt (DATA[3]);return new MathTest (DATA[1],DATA[2],m,DATA[4],DATA[5],DATA[6]);}/*** * @param line 读取testcatalog.dat中的一行字符串* @return 返回一个EnglishTest*/private ProfessionalTest readProfessionalTest(String line){String[] DATA=line.split ("_");if(DATA.length!=8){try {throw new DataFormatException("This line contains incomplete data");} catch (DataFormatException e) {e.printStackTrace ();}}int m=Integer.parseInt (DATA[3]);return new ProfessionalTest (DATA[1],DATA[2],m,DATA[4],DATA[5],DATA[6],DATA[7]);}/*** * @param filename 读取的文件名称* @return 返回已经初始化的 TestDatabase*/public TestDatabase loadTestDatabase (String filename){//使用BR字符流初始化 data (ArrayList<String>)BufferedReader BR;{try {BR = new BufferedReader(new FileReader("testCatalog.dat"));while(true){try {String line;if ((BR.readLine () !=null)){line=BR.readLine ();data.add(line);}else{break;}}catch (IOException e) {e.printStackTrace ();}}}catch (FileNotFoundException e) {e.printStackTrace ();}}TestDatabase testdatabase=new TestDatabase ();for(String tmpstring : data){String[] DATA=tmpstring.split ("_");//switch语句判断是哪种Test类型switch (DATA[0]) {case "EnglishTest" -> testdatabase.addTest (readEnglishTest (tmpstring));case "MathTest" -> testdatabase.addTest (readMathTest (tmpstring));case "ProfessionalTest" -> testdatabase.addTest (readProfessionalTest (tmpstring));}}return testdatabase;}
}

testCatalog.dat(题目自带)

EnglishTest_E001_Translate the following text into English._2_Smooth,fluent and without language problems or wrong words_C-E
EnglishTest_E002_Translate the following article content._3_Clear logic and no language problems_E-C
EnglishTest_E003_Choose the correct answer based on the content being played._3_Correct answer_Hearing
EnglishTest_E004_Translate the following Chinese in English._2_No grammatical errors_C-E
EnglishTest_E005_Translate the following English in Chinese._2_Sentence fluent_E-C
EnglishTest_E006_Choose the correct answer based on contextual dialogue_2_Right_Hearing
EnglishTest_E007_Translate the following article content._3_Smooth and fluent_C-E
EnglishTest_E008_Translate to Chinese_3_Complete translation_E-C
EnglishTest_E009_Listen to the dialogue and choose Xiao Ming’s weekend schedule._3_Correct answer_Hearing
EnglishTest_E010_Translate the following text into English._2_Use the correct words and smooth_C-E
MathTest_M001_Find the inflection points of the following functions._2_Right_no image_no
MathTest_M002_Which of the following series converge?_3_Right_no image_no
MathTest_M003_Find the differential equation of the following function._3_Necessary problem-solving process_no image_The first step is to find the integral. The second step is to find the value of the constant c.
MathTest_M004_Find the angle between the two planes A and B._2_Correct answer_http://image.com/m004_no
MathTest_M005_Several extreme points in the figure below._3_Right_http://image.com/m005_no
MathTest_M006_Find the inflection points of the following functions._2_Right_no image_no
MathTest_M007_Find the differential equation of the following function._3_Clear problem solving process and correct value_no image_Find the value of the constant b and the differential equation
MathTest_M008_The area enclosed by the following curve and the coordinate axis is?_1_Correct answer_http://image.com/m008_no
MathTest_M009_Find general solutions of differential equations._2_The parameters are correct_no image_Find the value of the parameter
MathTest_M010_Find the function f(x)._2_Right_http://image.com/m010_no
ProfessionalTest_P001_What are the characteristics of JAVA language?_1_Right_Name at least three of the characteristics_no_no image
ProfessionalTest_P002_Fill in the following blanks to realize the calculation of the sum of the numbers between 1-200 that are not divisible by 5._2_Correct result at run_Fill in the code in the blank._no_http://image.com/p002
ProfessionalTest_P003_The time complexity of the algorithm refers to?_1_Correct answer_no_no_no image
ProfessionalTest_P004_The idea of dynamic programming_3_Right_no_no_no image
ProfessionalTest_P005_The design of the database includes two aspects of design content, they are?_2_Similar in meaning_no_no_no image
ProfessionalTest_P006_The difference between java and c._2_At least three points_At least three points_no_no image
ProfessionalTest_P007_The difference between process and thread._3_At least three points_At least three points_no_no image
ProfessionalTest_P008_The time complexity of the following code is?_1_Right_no_no_http://image.com/p008
ProfessionalTest_P009_Benefits of thread pool._3_Can name the key benefits_no_no_no image
ProfessionalTest_P010_Enter 5 numbers to find their maximum and average._3_Correct result at run_Time complexity cannot exceed n._no_no image

西北工业大学#面向对象编程实验#实验四->第一题相关推荐

  1. 西北工业大学#面向对象编程实验#实验三

    西北工业大学·面向对象编程实验三 3.1 题目说明:给定Project类,在ProjectArray中实现缺失功能,并在测试类TestProjectArray中运行成功显示"All test ...

  2. java实验Java面向对象编程_Java实验项目 面向对象编程.doc

    Java实验项目 面向对象编程 Java实验项目二 面向对象编程 第1部分 类与对象 [实验目的] 熟悉Java面向对象程序设计的基本思想. 掌握类与对象的定义及使用方法. 掌握package语句与i ...

  3. 怎么理解面向对象编程【java基础第一讲】

    面向对象编程(OOP)是什么 OOP:Object Oriented Programming.要讲面向对象我们先来理解一下面向过程的含义,如果你已经有c语言相关的开发经验可能就已经明白面向过程的意义. ...

  4. 面向对象是什么?为什么我们要先学面向过程,再学面向对象编程?到底什么是面向对象编程?

         面向对象是什么?为什么我们要先学面向过程,再学面向对象程?到底什么是面向对象编程? 回答:面向过程是计算机思维,计算机的思路就是取指执行,一条直道走到底,它可不会管你什么抽象,不管什么业务建 ...

  5. java面向对象编程基础实验报告_20155313 实验三《Java面向对象程序设计》实验报告...

    一.实验内容 XP基础 XP核心实践 相关工具 二.实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器>课程 2.完成实验.撰写实验报 ...

  6. java实验Java面向对象编程_java 实验三 面向对象程序设计(无脑实验系列)

    实验7 运算符重载 (1)定义日期类,重载 "++"和"--"运算符,使之能处理两个日期类对象自增和自减运算,并且自增和自减又分为前缀和后缀运算.(可继续完善. ...

  7. 西农大 Java 实验四 第一题

    任务一:界面设计 请设计一个界面,包括:菜单,文本框,按钮,标签,文本区,选择框,单选按钮和下拉列表. package first;import java.awt.BorderLayout; impo ...

  8. 面向对象编程(第四篇)

    issubclass()与isinstance() 内置函数issubclass()用于判断类对象与类对象之间的关系 内置函数isinstance()用于判断实例对象与类对象之间的关系 内置函数iss ...

  9. 西北工业大学网安22考研847真题分享

    在整个备考过程中,我发现市面上卖的资料很多都是从其他地方上凑得,并不是所谓真题,附加答疑也一言难尽,真题占比50%以下,这是我自己回忆的847真题,希望可以帮到大家,有需要可以留言 一.osi参考模型 ...

最新文章

  1. 自学python要多久才能学会-怎么自学python,大概要多久?
  2. Jmeter 命令行选项目录
  3. 解决jar包乱码 in 创新实训 智能自然语言交流系统
  4. python运算符的分类_python对象——标准类型运算符
  5. spring-boot-1.4x后@ConfigurationProperties注解舍弃location
  6. Vmware Update Manager安装错误,错误代码:25085
  7. php访问oracle写sql不能换行
  8. Longhorn的糟糕体验!
  9. Oracle 统计信息收集
  10. 2、Linux多线程,线程的分离与结合
  11. 深度现场教学安徽省安庆市青年干部培训参观杭州梦想小镇实例
  12. 【学习笔记-1】- 非线性规划的最优性一阶/二阶必要条件之例题(12道)
  13. 中小型企业网络IP地址规划案例
  14. mysql配置secure_file_priv
  15. dnf红眼补丁在哪下载_dnf红眼技能变红补丁
  16. [C++] 栈的压入、弹出序列
  17. ITK4.12+VS2015配置详解
  18. Scala可变长度参数和:_*使用
  19. Excel VBA(04)数组和字典
  20. ConfigurationChanged流程梳理(屏幕旋转、语言及字体切换)

热门文章

  1. Django开发中整合新浪微博API
  2. 谈谈uniapp中uniapp的$emit,$on,$once,$off的使用方法
  3. 通付盾实力入选《2022中国网络安全产业势能榜》
  4. MATLAB 绘制分段函数曲线并添加图形标注(至少包括标题和坐标轴说明)
  5. 使用Flutter完成10个商业项目后的经验教训,2021大厂安卓面试集合
  6. 随着“过失导致死亡的诉讼案”要求 在发生灾难性的行人伤亡事件后, 关闭“北公园中心(NorthPark Center)
  7. 形容计算机老师风采的句子,描述老师的唯美句子 送给老师最煽情的话
  8. 高性能linux构建Cf游戏,构建CF卡Linux系统的几个常见难题
  9. android8.0键盘灯关闭,安卓永久关闭键盘灯的方法
  10. 7-3 打印指定大小的表格 (10 分)