一、功能描述
定义考题类(Question)及其子类,目前先实现了单选和多选两种题型,其他题型可以扩展
完成考题类(Question),单选题(SingleChoice)和多选题(MultiChoice)是其子类
要求:
- Question包含题干属性(text)
- Question包含检测标准答案的方法 boolean check(int[] answers)
- 参数answers:用户提供的答案(注意:单选题只有一个答案)
- 单选题(SingleChoice)和多选题(MultiChoice)是Question的子类
- MultiChoice包含属性:
选项:String[] options
多选标准答案:int[] answers - SingleChoice包含属性:
选项:String[] options
单选标准答案:int answers
- MultiChoice包含属性:
- 在MultiChoice实现参数为(String text,String[] options,int[] answers)的构造方法
- 参数text:题干
- 参数options:选项
- 参数answers:多选标准答案(正确选项的序号)
- 在SingleChoice实现参数为(String text,String[] options,int answers)的构造方法
- 参数text:题干
- 参数options:选项
- 参数answers:单选标准答案(正确选项的序号)
- 在SingleChoice和MultiChoice类中重写Question类中的check方法
- 方法中提供具体的检查用户答案的逻辑
二、实现代码
1. 定义考题类
Question.java
package com.feonix;
/**
* 考题类
*
* @author FeoniX
*
*/
public abstract class Question {
/** 题干 */
private String text;
/**
* 有参构造器
*
* @param text 题干
*/
public Question(String text) {
this.text = text;
}
/**
* 检测标准答案的方法
*
* @param answers 考生填写的答案
* @return
*/
public abstract boolean check(int[] answers);
/**
* 获取题干
*
* @return
*/
public String getText() {
return text;
}
/**
* 设置题干
*
* @param text
*/
public void setText(String text) {
this.text = text;
}
}
2. 定义单选题类,继承考题类
SingleChoise.java
package com.feonix;
/**
* 单选题类
*
* @author FeoniX
*
*/
public class SingleChoice extends Question {
/** 单选题题目选项 */
private String[] options;
/** 单选题标准答案 */
private int answers;
/**
* 有参构造器
*
* @param text 题干
* @param options 题目选项
* @param answers 题目答案
*/
public SingleChoice(String text, String[] options, int answers) {
super(text);
this.options = options;
this.answers = answers;
}
/**
* 检测标准答案的方法
*
* @param answers 考生填写的答案
* @return
*/
@Override
public boolean check(int[] answers) {
// 单选题不能有多个答案
if (answers.length > 1)