当前位置:文档之家› 打字测试软件课程设计报告书

打字测试软件课程设计报告书

软件学院课程设计报告书课程名称面向对象课程设计设计题目打字训练测试软件专业班级学号姓名指导教师2014 年6 月目录1 设计时间 (1)2 设计目的 (1)3 设计题目 (1)4设计任务 (1)5设计内容 (1)5.1需求分析 (1)5.2基本要求 (1)6 功能模块实现 (1)6.1包的描述 (1)6.2类的描述 (1)6.3页面设计 (2)6.4测试文字的加入 (4)6.5测试时间设计 (5)6.6程序源代码 (6)8总结与展望 (12)参考文献 (14)1 设计时间2014年6月16日-2014年6月20日2 设计目的1.熟练掌握Java面向对象程序设计的基础知识2.熟练掌握Java中常用的Awt组件的使用方法3.通过实训,培养学生分析和解决实际问题的能力,提高调试和分析应用程序的能力,并为后续的Java高级编程等专业课程的学习奠定良好的基础。

3 设计题目打字测试软件4设计任务打字测试软件。

要求如下:(1)使用图形用户界面实现;(2)从文件中调入英文录入材料;(3)打字结束后给出错误率,录入时间。

5设计内容5.1需求分析设计一个英文打在测试软件5.2基本要求1.文件中调入英文录入材料2.最后给出错误率3.最后给出平均录入速度6 功能模块实现6.1包的描述由于本次实验使用的类较少,所有的类都建在同一个包下,本次实验也只用到一个包。

6.2类的描述Typing类:主要的构造都在这个类中。

My key类:是该程序的一个内部类。

6.2.1类之间的关系Typing类作为主函数的入口,调用actionPerformed类的方法实现事件监听器。

当发生点击事件时就会弹出输入框,开始英文打字功能;从而达到打字训练测试的目的。

6.2.2类图如图6-1所示图6-1 类图6.3页面设计final String TITLE = "光速打字系统";private Frame f;private Button btStart,btContinue;private Panel pnEast,pnCenter,pnNorth;Font fontTitle=new Font("黑体",Font.BOLD,30);Font fontButton=new Font("宋体",Font.BOLD,24);private Label lbMessage,lbTitle;//tfSource用于显示要练习的文字,tfInput 用户输入框private TextArea taSource,taInput;//总字数和错误的字数private long wordCount,inputWordCount,rightWordCount;//开始,结束时间,用户练习所用的时间private long startTime,overTime,userTime;/** 初始化控件*/private void start() {f = new Frame(TITLE+" by 牛帅"); //窗体标题f.setSize(600,430);f.setResizable(false);//禁止调整窗体的大小pnEast = new Panel();pnCenter = new Panel();btStart =new Button("开始");btStart.setFont(fontButton);btContinue = new Button("完成");btContinue.setFont(fontButton);taSource = new TextArea(10,60);//taSource.lbTitle = new Label(TITLE);lbTitle.setFont(fontTitle);//taSource.setEnabled(false);taSource.setEditable(false);pnNorth = new Panel();lbMessage = new Label("想练习打字的话,请先点击[开始按钮]哦!");taSource.setText("想练习打字的话,请先点击[开始按钮]哦!");taInput = new TextArea(10,60);taInput.setText("欢迎使用"+TITLE);btContinue.setEnabled(false);//窗体布局//窗体使用默认的GridLayout布局f.add(pnNorth,"North");pnNorth.add(lbTitle);//pnEast.add(lbMessage,"South");f.add(lbMessage,"South");f.add(pnEast,"East");pnEast.add(btStart);pnEast.add(btContinue);f.add(pnCenter,"Center");pnCenter.add(taSource);pnCenter.add(taInput);//添加监听器f.addWindowListener(new ClosingWindow());//启用窗体的功能按钮btStart.addMouseListener(new ButtonHandler());btContinue.addMouseListener(new ButtonHandler());f.setVisible(true);}如图6-2所示图6-2 打字测试软件界面图6.4测试文字的加入程序中,通过随机读取当前项目下的txt文件,随机方式是用Random()生0-3之间的数,添加“.txt”后缀,文件位置如图6-3所示。

图6-3 文件位置图代码实现部分:if (e.getSource() == btStart) {//获取练习用的文字file = rd.nextInt(3)+".txt";//System.out.println("打开的文件是:"+file);taSource.setText(fileToString("text/"+file));taInput.setText("");lbMessage.setText("亲,您现在可以练习了!加油!!");startTime = System.currentTimeMillis();btStart.setEnabled(false);btContinue.setEnabled(true);}6.5测试时间设计为了能够更方便地设置时间,在程序中用startTime,overTime,userTime三个变量来保存时间。

startTime是用来保存用户开始测试的系统时间,overTime是用来保存用户结束测试时的系统时间,相减得到用户测试所用的总时间(userTime)。

通过startTime = System.currentTimeMillis() 获得当前系统的时间。

6.6程序源代码package buttonAction02;/** 已经实现了按钮的单击事件了* 练习资料是以txt文件形式放在项目目录下text目录里* */import java.awt.*;import java.awt.event.*;import java.util.Random;import java.io.*;public class Typing{final String TITLE = "光速打字系统";private Frame f;private Button btStart,btContinue;private Panel pnEast,pnCenter,pnNorth;Font fontTitle=new Font("黑体",Font.BOLD,30);Font fontButton=new Font("宋体",Font.BOLD,24);private Label lbMessage,lbTitle;//tfSource用于显示要练习的文字,tfInput 用户输入框private TextArea taSource,taInput;//总字数和错误的字数private long wordCount,inputWordCount,rightWordCount;//开始,结束时间,用户练习所用的时间private long startTime,overTime,userTime;/** 初始化控件*/private void start() {f = new Frame(TITLE+" by 曹世龙"); //窗体标题f.setSize(600,430);f.setResizable(false);//禁止调整窗体的大小pnEast = new Panel();pnCenter = new Panel();btStart =new Button("开始");btStart.setFont(fontButton);btContinue = new Button("完成");btContinue.setFont(fontButton);taSource = new TextArea(10,60);lbTitle = new Label(TITLE);lbTitle.setFont(fontTitle);//taSource.setEnabled(false);taSource.setEditable(false);pnNorth = new Panel();lbMessage = new Label("想练习打字的话,请先点击[开始按钮]哦!"); taSource.setText("想练习打字的话,请先点击[开始按钮]哦!"); taInput = new TextArea(10,60);taInput.setText("欢迎使用"+TITLE);btContinue.setEnabled(false);//窗体布局//窗体使用默认的BorderLayout布局f.add(pnNorth,"North");pnNorth.add(lbTitle);//pnEast.add(lbMessage,"South");f.add(lbMessage,"South");f.add(pnEast,"East");pnEast.add(btStart);pnEast.add(btContinue);f.add(pnCenter,"Center");pnCenter.add(taSource);pnCenter.add(taInput);//添加监听器f.addWindowListener(new ClosingWindow());//启用窗体的功能按钮btStart.addMouseListener(new ButtonHandler());btContinue.addMouseListener(new ButtonHandler());f.setVisible(true);}/** 接收按钮的事件*/class ButtonHandler extends MouseAdapter {public void mouseClicked(MouseEvent e){ String file;Random rd = new Random();if (e.getSource() == btStart) {//获取练习用的文字file = rd.nextInt(3)+".txt";/*所有的测试文字,都以txt文件形式保存在当前目录下的text目录里* 通过随机读取当前项目下的txt文件获取内容*/taSource.setText(fileToString("text/"+file));taInput.setText("");lbMessage.setText("亲,您现在可以练习了!加油!!");//用currentTimeMillis方法来获得当前系统的时间startTime = System.currentTimeMillis();btStart.setEnabled(false);btContinue.setEnabled(true);}else if(e.getSource()==btContinue){overTime = System.currentTimeMillis();wordCount=taSource.getText().length();inputWordCount=taInput.getText().length();//用于判断用户是否输入了文字if(inputWordCount == 0){lbMessage.setText("貌似你什么也没输入呀!");}else{//计算用户所用的时间,并转换成以秒为单位userTime = (overTime-startTime)/1000;//统计错误的文字数rightWordCount=rightWordCount(taSource.getText(),taInput.getText());lbMessage.setText("亲,打字练习结束了哦!您共用了"+userTime+"秒,总字数为:"+wordCount+"您正确输入了"+rightWordCount+"正确率为"+(rightWordCount*1.0)/(wordCount*1.0));}btStart.setEnabled(true);btContinue.setEnabled(false);}}private int rightWordCount(String str,String strSub) {int count = 0;//由于思维不严谨,语句中本应该是&&符,我错用了||符,//所以测试时一直有问题,for(int i=0;i<str.length()&&i<strSub.length();i++){//统计的是正确的总数!!!if(str.charAt(i)==strSub.charAt(i))count++;}return count;}}/** 给窗体上的控制按钮启用监听器*/class ClosingWindow extends WindowAdapter {public void windowClosing(WindowEvent e) {System.exit(0);}}public static void main(String[] args){Typing fm = new Typing();fm.start();}/** 传入一个文件名,方法以字符串形式返回文件的内容*/ public String fileToString(String filePath){int n = 0,i = 0;byte[] data = new byte[1024];FileInputStream f = null;try {f = new FileInputStream(filePath);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {n = f.read();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}while(n!=-1){data[i] = (byte)n;i++;try {n = f.read();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}String str = new String(data,0,i);//close filetry {f.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return str;}7 运行与测试7.1当用户没有输入文字时界面图,如图7-1所示图7-17.2当用户输入一部分文字后,点击完成,程序会给出结果,如图7-2所示图7-28总结与展望在课程设计的过程中,我用到了java中awt的各种控件和布局方法,又通过查阅资料和向同学、老师求教,学会了一些没接触过的东西,如设置字体,和调用系统时间等。

相关主题