原型模式(上机考试抽题)

2024-04-26 23:36

本文主要是介绍原型模式(上机考试抽题),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

定义

原型模式主要解决的问题就是创建复对象,⽽这部分 对象 内容本身⽐较复杂,⽣成过程可能从库或者RPC接⼝中获取数据的耗时较⻓,因此采⽤克隆的⽅式节省时间。

上机考试抽题

从⼀部分可以上机考试的内容开始,在保证⼤家的公平性⼀样的题⽬下,开始出现试题混排更有做的好的答案选项也混排。这样⼤⼤的增加了抄的成本,也更好的做到了考试的公平性。因为需要实现⼀个上机考试抽题的服务,因此在这⾥建造⼀个题库题⽬的场景类信息,⽤于创建; 选择题 、 问答题 。

选择题

public class ChoiceQuestion {private String name; // 题⽬private Map<String, String> option; // 选项;A、B、C、Dprivate String key; // 答案;Bpublic ChoiceQuestion() {}public ChoiceQuestion(String name, Map<String, String> option, String key) {this.name = name;this.option = option;this.key = key;}// ...get/set
}

问答题

public class AnswerQuestion {private String name; // 问题private String key; // 答案public AnswerQuestion() {}public AnswerQuestion(String name, String key) {this.name = name;this.key = key;}// ...get/set
}

代码实现

题⽬选项乱序操作⼯具包

/**
* 乱序Map元素,记录对应答案key
* @param option 题⽬
* @param key 答案
* @return Topic 乱序后 {A=c., B=d., C=a., D=b.}
*/
static public Topic random(Map<String, String> option, String key) {Set<String> keySet = option.keySet();ArrayList<String> keyList = new ArrayList<String>(keySet);Collections.shuffle(keyList);HashMap<String, String> optionNew = new HashMap<String, String>();int idx = 0;String keyNew = "";for (String next : keySet) {String randomKey = keyList.get(idx++);if (key.equals(next)) {keyNew = randomKey;}optionNew.put(randomKey, option.get(next));}return new Topic(optionNew, keyNew);
}

克隆对象处理类

public class QuestionBank implements Cloneable {private String candidate; // 考⽣private String number; // 考号private ArrayList<ChoiceQuestion> choiceQuestionList = new ArrayList<ChoiceQuestion>();private ArrayList<AnswerQuestion> answerQuestionList = new ArrayList<AnswerQuestion>();public QuestionBank append(ChoiceQuestion choiceQuestion) {choiceQuestionList.add(choiceQuestion);return this;}public QuestionBank append(AnswerQuestion answerQuestion) {answerQuestionList.add(answerQuestion);return this;}@Overridepublic Object clone() throws CloneNotSupportedException {QuestionBank questionBank = (QuestionBank) super.clone();questionBank.choiceQuestionList = (ArrayList<ChoiceQuestion>) choiceQuestionList.clone();questionBank.answerQuestionList = (ArrayList<AnswerQuestion>) answerQuestionList.clone();// 题⽬乱序Collections.shuffle(questionBank.choiceQuestionList);Collections.shuffle(questionBank.answerQuestionList);// 答案乱序ArrayList<ChoiceQuestion> choiceQuestionList = questionBank.choiceQuestionList;for (ChoiceQuestion question : choiceQuestionList) {Topic random = TopicRandomUtil.random(question.getOption(), question.getKey());question.setOption(random.getOption());question.setKey(random.getKey());}return questionBank;}public void setCandidate(String candidate) {this.candidate = candidate;}public void setNumber(String number) {this.number = number;}@Overridepublic String toString() {StringBuilder detail = new StringBuilder("考⽣:" + candidate + "\r\n" +"考号:" + number + "\r\n" +"--------------------------------------------\r\n" +"⼀、选择题" + "\r\n\n");for (int idx = 0; idx < choiceQuestionList.size(); idx++) {detail.append("第").append(idx + 1).append("题:").append(choiceQuestionList.get(idx).getName()).append("\r\n");Map<String, String> option = choiceQuestionList.get(idx).getOption();for (String key : option.keySet()) {detail.append(key).append(":").append(option.get(key)).append("\r\n");}detail.append("答案:").append(choiceQuestionList.get(idx).getKey()).append("\r\n\n");}detail.append("⼆、问答题" + "\r\n\n");for (int idx = 0; idx < answerQuestionList.size(); idx++) {detail.append("第").append(idx + 1).append("题:").append(answerQuestionList.get(idx).getName()).append("\r\n");detail.append("答案:").append(answerQuestionList.get(idx).getKey()).append("\r\n\n");}return detail.toString();}
}

初始化试卷数据

public class QuestionBankController {private QuestionBank questionBank = new QuestionBank();public QuestionBankController() {Map<String, String> map01 = new HashMap<String, String>();map01.put("A", "JAVA2 EE");map01.put("B", "JAVA2 Card");map01.put("C", "JAVA2 ME");map01.put("D", "JAVA2 HE");map01.put("E", "JAVA2 SE");Map<String, String> map02 = new HashMap<String, String>();map02.put("A", "JAVA程序的main⽅法必须写在类⾥⾯");map02.put("B", "JAVA程序中可以有多个main⽅法");map02.put("C", "JAVA程序中类名必须与⽂件名⼀样");map02.put("D", "JAVA程序的main⽅法中如果只有⼀条语句,可以不⽤{}(⼤括号)括起来");Map<String, String> map03 = new HashMap<String, String>();map03.put("A", "变量由字⺟、下划线、数字、$符号随意组成;");map03.put("B", "变量不能以数字作为开头;");map03.put("C", "A和a在java中是同⼀个变量;");map03.put("D", "不同类型的变量,可以起相同的名字;");Map<String, String> map04 = new HashMap<String, String>();map04.put("A", "STRING");map04.put("B", "x3x;");map04.put("C", "void");map04.put("D", "de$f");Map<String, String> map05 = new HashMap<String, String>();map05.put("A", "31");map05.put("B", "0");map05.put("C", "1");map05.put("D", "2");questionBank.append(new ChoiceQuestion("JAVA所定义的版本中不包括", map01, "D")).append(new ChoiceQuestion("下列说法正确的是", map02, "A")).append(new ChoiceQuestion("变量命名规范说法正确的是", map03, "B")).append(new ChoiceQuestion("以下()不是合法的标识符",map04, "C")).append(new ChoiceQuestion("表达式(11+3*8)/4%3的值是", map05, "D")).append(new AnswerQuestion("⼩红⻢和⼩⿊⻢⽣的⼩⻢⼏条腿", "4条腿")).append(new AnswerQuestion("铁棒打头疼还是⽊棒打头疼", "头最疼")).append(new AnswerQuestion("什么床不能睡觉", "⽛床")).append(new AnswerQuestion("为什么好⻢不吃回头草", "后⾯的草没了"));}public String createPaper(String candidate, String number) throws CloneNotSupportedException {QuestionBank questionBankClone = (QuestionBank) questionBank.clone();questionBankClone.setCandidate(candidate);questionBankClone.setNumber(number);return questionBankClone.toString();}
}

测试验证

@Test
public void test_QuestionBank() throws CloneNotSupportedException {QuestionBankController questionBankController = new QuestionBankController();System.out.println(questionBankController.createPaper("花花", "1000001921032"));System.out.println(questionBankController.createPaper("⾖⾖", "1000001921051"));System.out.println(questionBankController.createPaper("⼤宝", "1000001921987"));
}

这篇关于原型模式(上机考试抽题)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/938969

相关文章

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

大数据spark3.5安装部署之local模式详解

《大数据spark3.5安装部署之local模式详解》本文介绍了如何在本地模式下安装和配置Spark,并展示了如何使用SparkShell进行基本的数据处理操作,同时,还介绍了如何通过Spark-su... 目录下载上传解压配置jdk解压配置环境变量启动查看交互操作命令行提交应用spark,一个数据处理框架

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

2024年流动式起重机司机证模拟考试题库及流动式起重机司机理论考试试题

题库来源:安全生产模拟考试一点通公众号小程序 2024年流动式起重机司机证模拟考试题库及流动式起重机司机理论考试试题是由安全生产模拟考试一点通提供,流动式起重机司机证模拟考试题库是根据流动式起重机司机最新版教材,流动式起重机司机大纲整理而成(含2024年流动式起重机司机证模拟考试题库及流动式起重机司机理论考试试题参考答案和部分工种参考解析),掌握本资料和学校方法,考试容易。流动式起重机司机考试技

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

软考系统规划与管理师考试证书含金量高吗?

2024年软考系统规划与管理师考试报名时间节点: 报名时间:2024年上半年软考将于3月中旬陆续开始报名 考试时间:上半年5月25日到28日,下半年11月9日到12日 分数线:所有科目成绩均须达到45分以上(包括45分)方可通过考试 成绩查询:可在“中国计算机技术职业资格网”上查询软考成绩 出成绩时间:预计在11月左右 证书领取时间:一般在考试成绩公布后3~4个月,各地领取时间有所不同