今天的一道面试题,湖南青果软件
设计一份考试程序,包含四道单选题和四道多选题,单选题答对一道得1分,多选答对一道得1.5分,
要求学生能在程序的指导下做题,一直得到最终的分数。
要求用JAVA实现。运用面向对象的方式,至少包含试卷,问题,答案三个类。
import javax.swing.*;
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();
}
});
}
});
}
}