Java综合设计.docx
- 文档编号:7903987
- 上传时间:2023-01-27
- 格式:DOCX
- 页数:16
- 大小:101.41KB
Java综合设计.docx
《Java综合设计.docx》由会员分享,可在线阅读,更多相关《Java综合设计.docx(16页珍藏版)》请在冰豆网上搜索。
Java综合设计
面向对象程序综合设计
设计报告
题目:
标准化模拟考试
专业:
网络工程
班级:
组长:
小组成员:
指导教师:
成绩:
计算机学院
2015年7月2日
一、实验原理
向程序里导入三份试题,分别为test1.txt、test2.txt和test3.txt,并在选择试题文件里选择一份试题,读取相应试题,并且提交相应的选择的答案,若所选的题号在试题里并未出现,则显示“试题内容为空,考试结束!
”可以暂停计时,在答完后可以看到:
分数:
……,正确回答:
…….,你的回答:
……。
二、实验内容
编写程序实现标准化模拟考试,进行网上模拟考试。
实验代码:
1、importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassEnglishTestextendsJFrame{
privatestaticfinallongserialVersionUID=4769821566862427509L;
TestAreatestPanel=null;
Containercon=null;
publicEnglishTest(){
super("标准化模拟考试");
testPanel=newTestArea();
con=getContentPane();
con.add(testPanel,BorderLayout.CENTER);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
setVisible(true);
setBounds(60,40,660,460);
con.validate();
validate();
}
publicstaticvoidmain(Stringargs[]){
newEnglishTest();
}
}
2、importjava.io.*;
importjava.util.*;
publicclassReadTestquestion{
Stringfilename="",correctAnswer="",testContent="",selection="";
intscore=0;
longtime=0;
boolean完成考试=false;
Filef=null;
FileReaderin=null;
BufferedReader读取=null;
publicvoidsetFilename(Stringname){
filename=name;
score=0;
selection="";
try{
if(in!
=null&&读取!
=null){
in.close();
读取.close();
}
f=newFile(filename);
in=newFileReader(f);
读取=newBufferedReader(in);
correctAnswer=(读取.readLine()).trim();
Stringtemp=(读取.readLine()).trim();
StringTokenizertoken=newStringTokenizer(temp,":
");
inthour=Integer.parseInt(token.nextToken());
intminute=Integer.parseInt(token.nextToken());
intsecond=Integer.parseInt(token.nextToken());
}catch(Exceptione){
testContent="没有选择试题";
}
}
publicStringgetFilename(){
returnfilename;
}
publiclonggetTime(){
returntime;
}
publicvoidset完成考试(booleanb){
完成考试=b;
}
publicbooleanget完成考试(){
return完成考试;
}
publicStringgetTestContent(){
try{
Strings=null;
StringBuffertemp=newStringBuffer();
if(读取!
=null){
while((s=读取.readLine())!
=null){
if(s.startsWith("**"))
break;
temp.append("\n"+s);
if(s.startsWith("endend")){
System.out.println("Ff");
in.close();
读取.close();
完成考试=true;
}
}
testContent=newString(temp);
}else{
testContent=newString("没有选择试题");
}
}catch(Exceptione){
testContent="试题内容为空,考试结束!
!
";
}
returntestContent;
}
publicvoidsetSelection(Strings){
selection=selection+s;
}
publicintgetScore(){
score=0;
intlength1=selection.length();
intlength2=correctAnswer.length();
intmin=Math.min(length1,length2);
for(inti=0;i try{ if(selection.charAt(i)==correctAnswer.charAt(i)) score++; }catch(StringIndexOutOfBoundsExceptione){ i=0; } } returnscore; } publicStringgetMessages(){ intlength1=selection.length(); intlength2=correctAnswer.length(); intlength=Math.min(length1,length2); Stringmessage="正确答案: "+correctAnswer.substring(0,length)+"\n" +"你的回答: "+selection+"\n"; returnmessage; } } 3、importjava.awt.*; importjavax.swing.*; importjava.awt.event.*; importjava.io.*; classFileNameimplementsFilenameFilter{ Stringstr=null; FileName(Strings){ str="."+s; } publicbooleanaccept(Filedir,Stringname){ returnname.endsWith(str); } } publicclassTestAreaextendsJPanelimplementsActionListener,ItemListener, Runnable{ privatestaticfinallongserialVersionUID=-2170619048041033841L; Choicelist=null; JTextArea试题显示区=null,消息区=null; JCheckBoxbox[]; JButton提交该题答案,读取下一题,查看分数; ReadTestquestion读取试题=null; JLabelwelcomeLabel=null; ThreadcountTime=null; longtime=0; JTextFieldtimeShow=null; boolean是否关闭计时器=false,是否暂停计时=false; JButton暂停或继续计时=null; publicTestArea(){ list=newChoice(); String当前目录=System.getProperty("user.dir"); Filedir=newFile(当前目录); FileNamefileTxt=newFileName("txt"); StringfileName[]=dir.list(fileTxt); for(inti=0;i list.add(fileName[i]); } 试题显示区=newJTextArea(15,12); 试题显示区.setLineWrap(true); 试题显示区.setWrapStyleWord(true); 试题显示区.setFont(newFont("TimesRoman",Font.PLAIN,14)); 试题显示区.setForeground(Color.blue); 消息区=newJTextArea(8,8); 消息区.setForeground(Color.blue); 消息区.setLineWrap(true); 消息区.setWrapStyleWord(true); countTime=newThread(this); Strings[]={"A","B","C","D"}; box=newJCheckBox[4]; for(inti=0;i<4;i++){ box[i]=newJCheckBox(s[i]); } 暂停或继续计时=newJButton("暂停计时"); 暂停或继续计时.addActionListener(this); 提交该题答案=newJButton("提交该题答案"); 读取下一题=newJButton("读取第一题"); 读取下一题.setForeground(Color.blue); 提交该题答案.setForeground(Color.blue); 查看分数=newJButton("查看分数"); 查看分数.setForeground(Color.blue); 提交该题答案.setEnabled(false); 提交该题答案.addActionListener(this); 读取下一题.addActionListener(this); 查看分数.addActionListener(this); list.addItemListener(this); 读取试题=newReadTestquestion(); JPanelpAddbox=newJPanel(); for(inti=0;i<4;i++){ pAddbox.add(box[i]); } BoxboxH1=Box.createVerticalBox(),boxH2=Box.createVerticalBox(),baseBox=Box .createHorizontalBox(); boxH1.add(newJLabel("选择试题文件")); boxH1.add(list); boxH1.add(newJScrollPane(消息区)); boxH1.add(查看分数); timeShow=newJTextField(20); timeShow.setHorizontalAlignment(SwingConstants.RIGHT); timeShow.setEditable(false); JPanelp1=newJPanel(); p1.add(newJLabel("剩余时间: ")); p1.add(timeShow); p1.add(暂停或继续计时); boxH1.add(p1); boxH2.add(newJLabel("试题内容: ")); boxH2.add(newJScrollPane(试题显示区)); JPanelp2=newJPanel(); p2.add(pAddbox); p2.add(提交该题答案); p2.add(读取下一题); boxH2.add(p2); baseBox.add(boxH1); baseBox.add(boxH2); setLayout(newBorderLayout()); add(baseBox,BorderLayout.CENTER); welcomeLabel=newJLabel("欢迎考试! ",JLabel.CENTER); welcomeLabel.setFont(newFont("隶书",Font.PLAIN,24)); welcomeLabel.setForeground(Color.blue); add(welcomeLabel,BorderLayout.NORTH); } publicvoiditemStateChanged(ItemEvente){ timeShow.setText(null); 是否关闭计时器=false; 是否暂停计时=false; 暂停或继续计时.setText("暂停计时"); Stringname=(String)list.getSelectedItem(); 读取试题.setFilename(name); 读取试题.set完成考试(false); time=读取试题.getTime(); if(countTime.isAlive()){ 是否关闭计时器=true; countTime.interrupt(); } countTime=newThread(this); 消息区.setText(null); 试题显示区.setText(null); 读取下一题.setText("读取第一题"); 提交该题答案.setEnabled(false); 读取下一题.setEnabled(true); welcomeLabel.setText("欢迎考试,你选择的试题: "+读取试题.getFilename()); } publicvoidactionPerformed(ActionEvente){ if(e.getSource()==读取下一题){ 读取下一题.setText("读取下一题"); 提交该题答案.setEnabled(true); StringcontentTest=读取试题.getTestContent(); 试题显示区.setText(contentTest); 消息区.setText(null); 读取下一题.setEnabled(false); try{ countTime.start(); }catch(Exceptionevent){ } } if(e.getSource()==提交该题答案){ 读取下一题.setEnabled(true); 提交该题答案.setEnabled(false); Stringanswer="? "; for(inti=0;i<4;i++){ if(box[i].isSelected()){ answer=box[i].getText(); box[i].setSelected(false); break; } } 读取试题.setSelection(answer); } if(e.getSource()==查看分数){ intscore=读取试题.getScore(); Stringmessages=读取试题.getMessages(); 消息区.setText("分数: "+score+"\n"+messages); } if(e.getSource()==暂停或继续计时){ if(是否暂停计时==false){ 暂停或继续计时.setText("继续计时"); 是否暂停计时=true; }elseif(是否暂停计时==true){ 暂停或继续计时.setText("暂停计时"); 是否暂停计时=false; countTime.interrupt(); } } } publicsynchronizedvoidrun(){ while(true){ if(time<=0){ 是否关闭计时器=true; countTime.interrupt(); 提交该题答案.setEnabled(false); 读取下一题.setEnabled(false); timeShow.setText("用时尽,考试结束"); }elseif(读取试题.get完成考试()){ 是否关闭计时器=true; timeShow.setText("考试效果: 分数*剩余时间(秒)="+1.0*读取试题.getScore() *(time/1000)); countTime.interrupt(); 提交该题答案.setEnabled(false); 读取下一题.setEnabled(false); }elseif(time>=1){ time=time-1000; longleftTime=time/1000; longleftHour=leftTime/3600; longleftMinute=(leftTime-leftHour*3600)/60; longleftSecond=leftTime%60; timeShow.setText(""+leftHour+"小时"+leftMinute+"分" +leftSecond+"秒"); } try{ Thread.sleep(1000); }catch(InterruptedExceptionee){ if(是否关闭计时器==true) return; } while(是否暂停计时==true){ try{ wait(); }catch(InterruptedExceptionee){ if(是否暂停计时==false){ notifyAll();}}}}}} 相应测试题: test1.txt 1.Takeanumbrellawithyouincase_____. A.itwillrainB.itrains C.itrainingD.itrained 2.Heisnolongerthehonestman_____hewas. A.whoB.whom C.whichD.that 3.Duringtherecession,thousandsofworkerswere_____ A.laidonB.laiddown C.laidoutD.laidoff **************************************** test2.txt 1.ThelostcaroftheLeeswasfound____inthe woodsoffthehighway. A.vanishedB.abandoned C.scatteredD.rejected 2.Plasedonotbe____byhisbadmannerssiance heismerelytryingtoattractattention. A.disregardedB.distorted C.irritetedD.intervened 3.Bytheyear2000,scientistsprobably____acure forcancer. A.willbediscovering B.arediscovering C.willhavediscovered D.havediscovered 4.Heistooyoungtobeableto____
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 综合 设计