一、功能描述
定義考題類(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) {
return false;
}
return answers[0] == this.answers;
}
/**
* 獲取題目選項
*
* @return
*/
public String[] getOptions() {
return options;
}
/***
* 設置題目選項
*
* @param options
*/
public void setOptions(String[] options) {
this.options = options;
}
/**
* 獲取標準答案
*
* @return
*/
public int getAnswers() {
return answers;
}
/**
* 設置標準答案
*
* @param answers
*/
public void setAnswers(int answers) {
this.answers = answers;
}
@Override
public String toString() {
String text = this.getText() + "\n";
for (String op : this.getOptions()) {
text += op + "\n";
}
return text;
}
}
3. 定義多選題類,繼承考題類
MultiChoise.java
package com.feonix;
/**
* 多選題類
*
* @author FeoniX
*
*/
public class MultiChoice extends Question {
/** 多選題題目選項 */
private String[] options;
/** 多選題標準答案 */
private int[] answers;
public MultiChoice(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 != this.answers.length) {
return false;
}
// 正確的選項個數
int corrects = 0;
// 因爲考生的選項個數和答案一致,但是順序不一定一致
for (int i = 0; i < answers.length; i++) {
for (int j = 0; j < answers.length; j++) {
// 所以找到一個一致的,就記錄一下
if (answers[i] == this.answers[j]) {
corrects++;
}
}
}
// 如果正確的個數和答案個數不一致,就說明答錯了,否則就是答對了
return this.answers.length == corrects;
}
/**
* 獲取題目選項
*
* @return
*/
public String[] getOptions() {
return options;
}
/***
* 設置題目選項
*
* @param options
*/
public void setOptions(String[] options) {
this.options = options;
}
/**
* 獲取標準答案
*
* @return
*/
public int[] getAnswers() {
return answers;
}
/**
* 設置標準答案
*
* @param answers
*/
public void setAnswers(int[] answers) {
this.answers = answers;
}
@Override
public String toString() {
String text = this.getText() + "\n";
for (String op : this.getOptions()) {
text += op + "\n";
}
return text;
}
}
4. 定義測試類
package com.feonix;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
/** 存放考題的list集合 */
private static List<Question> list = new ArrayList<Question>();
public static void main(String[] args) {
// 這裏調用初始化考題的方法
initQuestions();
Scanner key = new Scanner(System.in);
String answer = "";
String[] answers = new String[4];
System.out.println("模擬考試現在開始");
System.out.println("本套試題分爲單選題和多選題,單選題只能選擇一個選項,多選題多個選項中間以英文逗號“,”分隔\n");
for (Question question : list) {
System.out.println(question);
System.out.println("請選擇:");
answer = key.next();
answers = answer.split(",");
boolean result = question.check(stringArrayToIntegerArray(answers));
System.out.println("回答" + (result ? "正確" : "錯誤") + "\n");
System.out.println("==========================================================================\n");
}
key.close();
}
/**
* 初始化生成試卷
* 這裏手動寫的兩個,後續可以添加從數據庫裏面讀取
*/
private static void initQuestions() {
String text = "1. (單選題)下面的程序執行結果爲:( )\npublic class Point {\n\tint y = 7;\n\tpublic void step(int y) {\n\t\ty += y;\n\t\tSystem.out.println(y);}\n\tpublic static void main(String[] args) {\n\t\tPoint p = new Point();\n\t\tp.step(10);\n\t}\n}\n";
String[] options = new String[] { "1. 14", "2. 20", "3. 10", "4. 17" };
int answer = 2;
Question singleChoose = new SingleChoice(text, options, answer);
list.add(singleChoose);
text = "2. (多選題)下列關於JVM的內存結構描述正確的是:( )\n";
options = new String[] { "1. 類的各種信息在堆中保存", "2. 棧用於存放程序運行過程中所有的局部變量", "3. 堆一般用於存儲使用new關鍵字創建的對象",
"4. 類是JVM的內存結構" };
int[] answers = { 2, 3 };
Question multiChoose = new MultiChoice(text, options, answers);
list.add(multiChoose);
}
/**
* String類型的數組轉爲Integer類型的數組
*
* @param array
* @return
*/
private static int[] stringArrayToIntegerArray(String[] array) {
int[] ins = new int[array.length];
for (int i = 0; i < array.length; i++) {
ins[i] = Integer.parseInt(array[i]);
}
return ins;
}
}