标准化试题训练系统
标准化试题训练系统按照MVC的设计思想展开程序设计和代码编写。
1.用problem类封装试卷
package basics;
//problem 封装表结构
public class problem {
//变量声明
//是否为选择题
boolean isChoice;
//是否为判断题
boolean isJudge;
//题目内容
String content;
//提供选择
String giveChoiceA,giveChoiceB,giveChoiceC,giveChoiceD;
//题目所带的图像文件的名字
String imageName;
//题目的正确答案
String correctAnswer="QWEQ@#$@!@#1QWEQ";
//初始值必须是不含任何字符的串
String userAnswer="";
//方法定义 get set 封装试题
public boolean getIsChoice(){
return isChoice;
}
public void setIsChoice(boolean b){
isChoice = b;
}
public boolean getIsJudge(){
return isJudge;
}
public void setIsJudge(boolean b){
isJudge = b;
}
public void SetContent(String c){
content = c;
}
public String getContent(){
return content;
}
public String getCorrectAnswer(){
return correctAnswer;
}
public void setCorrectAnswer(String a){
correctAnswer =a;
}
public String getUserAnswer(){
return userAnswer;
}
public void setUserAnswer(String u){
userAnswer = u;
}
public String getGiveChoiceBA(){
return giveChoiceA;
}
public void SteGiveChoiceA(String a){
giveChoiceB = a;
}
public String getGiveChoiceB(){
return giveChoiceB;
}
public void SteGiveChoiceB(String b){
giveChoiceB = b;
}
public String getGiveChoiceC(){
return giveChoiceC;
}
public void SteGiveChoiceC(String c){
giveChoiceC = c;
}
public String getGiveChoiceD(){
return giveChoiceD;
}
public void SteGiveChoiceD(String d){
giveChoiceD = d;
}
public String getImageName(){
return imageName;
}
public void SteImageName(String c){
imageName = c;
}
}
2.若干个problem组成一张试卷,这里用TestParper类封装试卷
package basics;
public class TestPaper {
private problem[]problem=null; //数组的每个单元存放一道题
int index=-1;//指针 下标
String problemSource; //试卷题库
//get set方法封装
public void setProblem(problem[]problem){
this.problem = problem;
}
//getProblem()获取problem的值
public problem getProblem(int i){
//首先判断存放试题的数组的第i 是否为空,或者数组的长度是否为0,如果为空或者0,返回null
//反之返回problem数组的的第i+1个元素
if(problem==null){ //首先判断存放试题的数组是不是为空
return null;
}
if(problem.length==0){//判读数组的长度是不是等于0,即判断数组是不是为空
return null;
}
if(i>=problem.length||i<0){
return null;
}
return problem[i];
}
//获取下一道试题
public problem nextProblem() {
index++;
if(problem==null){
return null;
}
if(problem.length==0){
return null;
}
if(index==problem.length){
index = problem.length-1;
}
return problem[index];
}
//获取前一道试题
public problem previousProblem() {
index--;
if(problem==null){
return null;
}
if(problem.length==0){
return null;
}
if(index<0) {
index = 0;
}
return problem[index];
}
public problem[]getProblem(){
if(problem==null){
return null;
}
if(problem.length==0){
return null;
}
return problem;
}
//试题数量
public int getProblemAmount(){
if(problem==null){
return 0;
}
return problem.length;
}
//封装试卷的题库
public void setProblemSource(String source){
problemSource = source;
}
public String getProblemSource(){
return problemSource;
}
//让老师来批阅试卷
public void acceptTeacher(Teacher teacher){
teacher.giveTestPaperScore(this);
}
}
3.试卷本身不能给出自己的分数,需要其他对象访问试卷,根据试卷中的数据给出相应的评判,为了后期扩展,将评判试卷的方法封装在Teacher接口中。
package basics;
public interface Teacher {
public void giveTestPaperScore(TestPaper testPaper);
}
4.TeacherOne实现接口,给出一种评卷方式
package basics;
import javax.swing.*;
//TeacherOne实现接口Teacher,并重写giveTestPaperScore方法
public class TeacherOne implements Teacher{
public void giveTestPaperScore(TestPaper testPaper){
int correctAmount=0;//计算百分比 局部变量
//首先判断试卷是否为空
if(testPaper==null){
//swing包中JOptionPane类中的方法showMessageDialog表示消息对话框
//parentComponent表示容器 massage表示提示消息 title表示标题
JOptionPane.showMessageDialog
(null,"没答题","消息对话框",JOptionPane.WARNING_MESSAGE);
return;
}
//problem数组的p元素的值就等于TestPaper类中getProblem()获取的值
problem p[]=TestPaper.getProblem();
//如果p为null说明他没做答
if(p==null||p.length==0){
JOptionPane.showMessageDialog
(null,"没回答","消息对话框",JOptionPane.WARNING_MESSAGE);
return;
}
for(int i=0;i<p.length;i++){
boolean b =compare(p[i].getUserAnswer(),p[i].getCorrectAnswer());
if(b)
correctAmount++;
}
double result = (double)correctAmount/(double)p.length;
int r =(int)(result*100);
String s = "共有:"+p.length+"道题."+"您对了"+correctAmount+"题,"+"正确率大约"+r+"%";
JLabel mess = new JLabel(s);
JOptionPane.showMessageDialog(null,mess,"成绩",JOptionPane.PLAIN_MESSAGE);
}
private boolean compare(String s,String t) {
boolean isTrue = true;
for(int i = 0; i < s.length(); i++) {
String temp = "" + s.charAt(i);
if (!(t.contains(temp))) {
isTrue = false;
break;
}
}
for(int i = 0; i < t.length(); i++) {
String temp = "" + t.charAt(i);
if (!(s.contains(temp))) {
isTrue = false;
break;
}
}
return isTrue;
}
}
5.抽取试题
package basics;
public interface GiveTestPaper {
public void getTestPaper(String excelFileName,int amount);
}