今天去一家公司机试;
遇到一个这样的问题:
设计一份考试程序,包含四道单选题和四道多选题,单选题答对一道得1分,多选答对一道得1.5分,
要求学生能在程序的指导下做题,一直得到最终的分数。
要求用JAVA实现。运用面向对象的方式,至少包含试卷,问题,答案三个类。
这道题把我困住了,请各位高手指点迷津,小弟先谢过。
遇到一个这样的问题:
设计一份考试程序,包含四道单选题和四道多选题,单选题答对一道得1分,多选答对一道得1.5分,
要求学生能在程序的指导下做题,一直得到最终的分数。
要求用JAVA实现。运用面向对象的方式,至少包含试卷,问题,答案三个类。
这道题把我困住了,请各位高手指点迷津,小弟先谢过。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.util.*;
import java.io.*;
public class Exam {
public static void main(String[] args) {
JFrame f = new ExamFrame();
f.setVisible(true);
}
}class ExamFrame extends JFrame {
static int singlequestion;
static int singleanswer;
static int multiplyquestion;
static int multiplyanswer;
static {
Properties prop = new Properties();
try {
prop.load(new FileInputStream("exam.properties"));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
singlequestion = Integer.parseInt(prop.getProperty("singlequestion"));
singleanswer = Integer.parseInt(prop.getProperty("singleanswer"));
multiplyanswer = Integer.parseInt(prop.getProperty("multiplyanswer"));
multiplyquestion = Integer.parseInt(prop.getProperty("multiplyquestion"));
}
Question[] qs = new Question[singlequestion];
Question2[] q2s = new Question2[multiplyquestion];
Score score = new Score();
int rightcount;
JLabel scoreLabel = new JLabel();
public ExamFrame() {
setSize(600,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(12,1));
JPanel titlePanel = new TitlePanel();
add(titlePanel);
Scanner scan = null;
try {
scan = new Scanner(new File("exam.txt"));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
int index=0;
while(scan.hasNextLine()&&index<singlequestion) {
String temp = scan.nextLine(); //System.out.println(temp);
String[] temps = temp.split("!");
Answer[] answers = new Answer[singleanswer];
ButtonGroup group = new ButtonGroup();
for(int i = 1;i<5;i++) {
answers[i-1] = new Answer(temps[i]);
group.add(answers[i-1]);
}
qs[index] = new Question(temps[0],score,answers);
qs[index].setRightAnswer(Integer.parseInt(temps[5]));
add(qs[index++]);
}
index = 0;
while(scan.hasNextLine()) {
String temp = scan.nextLine();
String[] temps = temp.split("!");
Answer2[] answer2s = new Answer2[multiplyanswer];
for(int i=1;i<7;i++) {
answer2s[i-1] = new Answer2(temps[i]);
}
q2s[index] = new Question2(temps[0],score,answer2s);
int answersize = temps.length - 7;
int[] rightanswers = new int[answersize];
int index2 = 0;
for(int i = 7;i<temps.length;i++) {
rightanswers[index2++] = Integer.parseInt(temps[i]);
}
q2s[index].setRightAnswers(rightanswers);
add(q2s[index++]);
}
JPanel buttonPanel = new JPanel();
JButton submit = new JButton("Submit");
add(buttonPanel);
buttonPanel.add(submit);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EventQueue.invokeLater(new Runnable() {
public void run() {
for(Question temp:qs) {
if(temp.check()) {
rightcount+=1;
temp.score();
}
}
for(Question2 temp2:q2s) {
if(temp2.check()) {
rightcount+=1;
temp2.score();
}
}
scoreLabel.setText("you gave "+rightcount+" right answer,\r\n you got "+score.getResult()+" point");
add(scoreLabel);
rightcount = 0;
validate();
}
});
}
});
}
}class Answer extends JRadioButton{
public Answer(String text) {
setText(text);
}}
class Answer2 extends JCheckBox {
//String text;
public Answer2(String text) {
setText(text);
}
}
class Question extends JPanel {
String text;
Score score;
Answer[] answers;
int flag ;
boolean inited;
public Question(String text,Score s,Answer[] as) {
this.text = text;
score = s;
answers = as;
setLayout(new GridLayout(2,1));
JLabel l = new JLabel(text);
add(l);
JPanel p = new JPanel();
add(p);
p.setLayout(new GridLayout(1,4));
for(int i = 0;i<answers.length;i++)
p.add(answers[i]);
}
public void setRightAnswer(int i) {
inited = true;
flag = i;
} public boolean check() {
if(inited&&answers[flag].isSelected())
return true;
return false;
}
public void score() {
score.add(1);
}
}
//String text;
Answer2[] answers;
Score score;
int[] rightanswers;
int flag;
public Question2(String text,Score s,Answer2[] sa) {
// this.text = text;
score = s;
answers = sa;
setLayout(new GridLayout(2,1));
JLabel l = new JLabel(text);
add(l);
int size = answers.length;
JPanel buttonPanel = new JPanel();
add(buttonPanel);
buttonPanel.setLayout(new GridLayout(1,size));
for(int i=0;i<size;i++)
buttonPanel.add(answers[i]);
}
public void setRightAnswers(int[] f) {
rightanswers = f;
flag = f.length;
}
public void score() {
if(check())
score.add(1.5);
}
public boolean check(){
for(int index:rightanswers) {
if(!answers[index].isSelected())
return false;
}
for(int i=0;i<answers.length;i++)
if(answers[i].isSelected()&&!insideAset(i,rightanswers))
return false;
return true;
}
public static boolean insideAset(int i,int[] arr) {
for(int temp:arr) {
if(temp==i)
return true;
}
return false;
}
}class Score {
double score ;
public void add(double d) {
score += d;
}
public double get() {
return score;
}
public double getResult() {
double result = score;
score = 0;
return result;
}
}
class TitlePanel extends JPanel {
static String text ; // = "Exam Test:for the java programmer";
static {
Properties prop = new Properties();
try {
prop.load(new FileInputStream("exam.properties"));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
text = prop.getProperty("title");
}
public TitlePanel() {}
public TitlePanel(String text) {
text = text;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(text==null) {
System.out.println("paint:text == null!");
return;
}
Graphics2D g2 = (Graphics2D)g;
Font titleFont = new Font("SansSerif", Font.BOLD, 20);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D rect = titleFont.getStringBounds(text,context);
float X = (float)(getWidth()-rect.getWidth())/2;
float Y = -(float)(rect.getY());
System.out.println(X+" "+Y);
g2.setFont(titleFont);
g2.drawString(text,X,Y);
}
}
这是我当时的解决方案,用下面几个类来实现:Exam,ExamFrame,Score,Question,Answer,Question2,Answer2,这几个主要的类再加上一个试卷标题类TitlePanel。
Question和Question2分别表示单选题和多选题,Answer和Answer2分别表示单选答案和多选答案。由于单选答案我想用单选框按钮实现,而多选答案我想用复选框来实现,
所以我没有让他们继承同一个类,这样代码显得有点冗余。我想把Question和Question2整合进一个基类中,把Answer和Answer也整合进一个基类中,请问各位该怎样重新设计类。
请各位高手多多指教啊,先谢谢