1、JAVA语言程序设计课程设计报告书院系名称 计算机与网络安全学院 专业班级 2015级软件工程3班 学号 姓名 指导教师 完成时间 2016年12月1-2号 目 录1.系统功能及任务描述1.1系统总体功能描述1.2主要任务描述2.系统的实现3.系统的测试4.总结4.1自我评价及收获4.2有待解决的问题及进一步完善的思路5.参考文献6.附录(代码附后)1. 系统功能及任务描述1.1系统总体功能描述本课程设计主要功能就是利用随机函数产生N个随机整数(20000以上),对这些数进行多种方法进行排序。1.2主要任务描述1) 至少采用两种方法实现上述问题求解(提示,可采用的方法有插入排序、冒泡排序、选择
2、排序、交换排序等)。并把排序后的结果保存在不同的文件中。2) 统计每一种排序方法的性能(以上机运行程序所花费的时间为准进行对比),找出其中两种较快的方法。3) 如果采用4种或4种以上的方法者,可适当加分。2. 系统的实现2.1开发环境2.1.1工具Eclipse:版本 Mars.1 Release (4.5.1),windows 64位JDK:版本jdk-7u51-windows-x64.exe2.1.2 电脑机型华硕R556L,系统64位2.2界面设计界面采用Swing轻量级组件开发,在1个JFrame窗体中放置5个JLabel标签,5个JButton按钮,3个JTextField文本框。2
3、.3 类设计2.4 主要代码说明2.4.1 主要按钮触发事件2.4.1.1 “确定”按钮给“确定”按钮添加事件监听器addActionListener方法,内部类里实现actionPerformed(ActionEvent e)方法,获取文本框JTextField的数据,然后产生随机数,用LinkedList集合存储进RandomNumber.txt文件,方便增加删除。/确定按钮事件jButton_confirm.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) /
4、 TODO 自动生成的方法存根/集合,用于存储数据LinkedList linkedList = new LinkedList();/获取文本框数据int begin = Integer.parseInt(jTextField_begin.getText();int end = Integer.parseInt(jTextField_end.getText();int num = Integer.parseInt(jTextField_num.getText();/产生随机数for(int i=0;inum;i+)int temp = (int) (Math.random() * (end-b
5、egin)+begin;linkedList.add(temp);/输出到屏幕for(int i=0;inum;i+)System.out.println(Random随机数-+linkedList.get(i);System.out.println(随机数个数-+linkedList.size();/存储到RandomNumber.txt文件中File file = new File(RandomNumber.txt);try OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file),UTF-8)
6、; BufferedWriter bufferedWriter = new BufferedWriter(write); for(int i=0;ilinkedList.size();i+)String str = String.valueOf(linkedList.get(i);String string = new String(str.getBytes(iso8859-1),UTF-8);bufferedWriter.write(string+n);bufferedWriter.close(); catch (IOException e1) / TODO 自动生成的 catch 块e1.
7、printStackTrace();/读取RandomNumber.txt文件InputStreamReader reader;try reader = new InputStreamReader(new FileInputStream(file),UTF-8);BufferedReader bufferedReader = new BufferedReader(reader);String line = null; try while (line = bufferedReader.readLine() != null) System.out.println(RandomNumber文件-+l
8、ine);bufferedReader.close(); catch (IOException e1) / TODO 自动生成的 catch 块e1.printStackTrace(); catch (UnsupportedEncodingException e1) / TODO 自动生成的 catch 块e1.printStackTrace(); catch (FileNotFoundException e1) / TODO 自动生成的 catch 块e1.printStackTrace(););2.4.1.1 “冒泡”按钮 同样给“冒泡”按钮添加监听器,在actionPerformed(A
9、ctionEvent e)方法里创建Bubble类对象,重新创建一个新的LinkedList集合,接着顺序调用Bubble中readerToFile()bubbleSort(linkedList)writerToFile(linkedList)三个方法,其中将在bubbleSort()方法前后获取当前系统时间,最后得到排序所用时间,显示在窗体中。/冒泡排序事件jButton_bubble.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / TODO 自动生成的方法
10、存根Bubble bubble = new Bubble();LinkedList linkedList = new LinkedList();linkedList = bubble.readerToFile();long begin = System.currentTimeMillis();linkedList = bubble.bubbleSort(linkedList);long end = System.currentTimeMillis();jLabel_time.setText(冒泡排序所用时间:+String.valueOf(end-begin)+ms);bubble.write
11、rToFile(linkedList););2.4.2 排序函数2.4.2.1 冒泡排序/冒泡排序LinkedList bubbleSort(LinkedList linkedList)int i,j;Integer temp;for(i=0;ilinkedList.size()-1;i+)for(j=1;jlinkedList.size()-i;j+)if(linkedList.get(j)linkedList.get(j-1)temp = linkedList.get(j);linkedList.set(j, linkedList.get(j-1);linkedList.set(j-1),
12、 temp);System.out.println(排序后*+linkedList);/排序后return linkedList;2.4.2.2 直接插入排序/直接插入排序LinkedList directInsertSort(LinkedList linkedList)int i,j;Integer temp;for(i=0;i=0 & templinkedList.get(j)linkedList.set(j+1), linkedList.get(j);j-;linkedList.set(j+1), temp);System.out.println(排序后*+linkedList);/排序
13、后return linkedList;2.4.2.2 直接选择排序 /直接选择排序 LinkedList directSelectSort(LinkedList linkedList)int i,j,k;Integer temp;for(i=0;ilinkedList.size()-1;i+)k = i;for(j=i+1;jlinkedList.size();j+)if(linkedList.get(j)linkedList.get(k);k = j;if(k!=i)temp = linkedList.get(i);linkedList.set(i, linkedList.get(k);li
14、nkedList.set(k, temp);System.out.println(排序后*+linkedList);/排序后return linkedList;2.4.2.2 快速排序 /直接选择排序 LinkedList quickSort(LinkedList linkedList,int s,int t) int i=s,j=t; int temp; if(si & linkedList.get(j)=temp) j-; linkedList.set(i, linkedList.get(j); while(ij & linkedList.get(i)_),老师如果你电脑好一点,你试试)1
15、)冒泡排序用了15709 ms2) 直接插入排序用了6599 ms3) 直接选择排序用了22012 ms4) 快速排序用了141 ms3.3文件测试验证文件确实有数据4.总结4.1自我评价及收获自我感觉良好,经过老师一年的精心栽培,最后能达到如此成果,呕心沥血做完,倍感荣幸,受益匪浅,老师之恩,来日再报!下面是获益之处:1) 更加熟悉了各排序的方法和效率;2) 认识GUI界面设计,运用了Swing组件的几种组件,从MFC过渡到java实现界面,颇有一种从地狱到天堂的快感;3) 更加深刻的了解了IO流、文件的读取操作、乱码以及格式转换等。4.2有待解决的问题及进一步完善的思路1) 文本框输入为空
16、时,会报错;2) 本次界面采用绝对布局,可以考虑采用流式布局,方格式布局,减少代码的臃肿性;3) 没有美化各组件,相对原始。5. 参考文献1) 课本java基础入门2) java从入门到精通3) 相关链接:1. Java读取文本文件中文乱码问题 2.在线API文档3. java中的随机数Random 4. Java 如何将String转化为Int6. 附录(代码附后) 6.1 RandomSort类import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.File
17、InputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.util.LinkedList;public class Quick void writerToFile(LinkedList linke
18、dList)/存储到Quick.txt文件中File file = new File(Quick.txt);try OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file),UTF-8); BufferedWriter bufferedWriter = new BufferedWriter(write); for(int i=0;ilinkedList.size();i+)String str = String.valueOf(linkedList.get(i);String string = ne
19、w String(str.getBytes(iso8859-1),UTF-8);bufferedWriter.write(string+n);bufferedWriter.close(); catch (IOException e1) / TODO 自动生成的 catch 块e1.printStackTrace(); /直接选择排序 LinkedList quickSort(LinkedList linkedList,int s,int t) int i=s,j=t; int temp; if(si & linkedList.get(j)=temp) j-; linkedList.set(i,
20、 linkedList.get(j); while(ij & linkedList.get(i)=temp) i+; linkedList.set(j, linkedList.get(i); linkedList.set(i, temp); quickSort(linkedList,s,i-1); quickSort(linkedList, i+1, t); return linkedList; LinkedList readerToFile()LinkedList linkedList = new LinkedList();File file = new File(RandomNumber.
21、txt);InputStreamReader reader;try reader = new InputStreamReader(new FileInputStream(file),UTF-8);BufferedReader bufferedReader = new BufferedReader(reader);String line = null; try while (line = bufferedReader.readLine() != null) int temp = Integer.parseInt(line); /System.out.println(bubble-+temp);
22、linkedList.add(temp);bufferedReader.close(); catch (IOException e1) / TODO 自动生成的 catch 块e1.printStackTrace(); catch (UnsupportedEncodingException e1) / TODO 自动生成的 catch 块e1.printStackTrace(); catch (FileNotFoundException e1) / TODO 自动生成的 catch 块e1.printStackTrace();return linkedList;6.2 Bubble类impor
23、t java.io.*;import java.util.LinkedList;public class Bubble void writerToFile(LinkedList linkedList)/存储到Bubble.txt文件中File file = new File(Bubble.txt);try OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file),UTF-8); BufferedWriter bufferedWriter = new BufferedWriter(write); fo
24、r(int i=0;ilinkedList.size();i+)String str = String.valueOf(linkedList.get(i);String string = new String(str.getBytes(iso8859-1),UTF-8);bufferedWriter.write(string+n);bufferedWriter.close(); catch (IOException e1) / TODO 自动生成的 catch 块e1.printStackTrace(); /冒泡排序 LinkedList bubbleSort(LinkedList linke
25、dList)int i,j;Integer temp;for(i=0;ilinkedList.size()-1;i+)for(j=1;jlinkedList.size()-i;j+)if(linkedList.get(j)linkedList.get(j-1)temp = linkedList.get(j);linkedList.set(j, linkedList.get(j-1);linkedList.set(j-1), temp);return linkedList; LinkedList readerToFile()LinkedList linkedList = new LinkedLi
26、st();File file = new File(RandomNumber.txt);InputStreamReader reader;try reader = new InputStreamReader(new FileInputStream(file),UTF-8);BufferedReader bufferedReader = new BufferedReader(reader);String line = null; try while (line = bufferedReader.readLine() != null) int temp = Integer.parseInt(lin
27、e); /System.out.println(bubble-+temp); linkedList.add(temp);bufferedReader.close(); catch (IOException e1) / TODO 自动生成的 catch 块e1.printStackTrace(); catch (UnsupportedEncodingException e1) / TODO 自动生成的 catch 块e1.printStackTrace(); catch (FileNotFoundException e1) / TODO 自动生成的 catch 块e1.printStackTra
28、ce();return linkedList;6.3 DirectInsert类import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.Outpu
29、tStreamWriter;import java.io.UnsupportedEncodingException;import java.util.LinkedList;public class DirectInsert void writerToFile(LinkedList linkedList)/存储到DirectInsert.txt文件中File file = new File(DirectInsert.txt);try OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file),UTF-8
30、); BufferedWriter bufferedWriter = new BufferedWriter(write); for(int i=0;ilinkedList.size();i+)String str = String.valueOf(linkedList.get(i);String string = new String(str.getBytes(iso8859-1),UTF-8);bufferedWriter.write(string+n);bufferedWriter.close(); catch (IOException e1) / TODO 自动生成的 catch 块e1
31、.printStackTrace(); /直接插入排序 LinkedList directInsertSort(LinkedList linkedList) int i,j;Integer temp;for(i=0;i=0 & templinkedList.get(j)linkedList.set(j+1), linkedList.get(j);j-;linkedList.set(j+1), temp);return linkedList; LinkedList readerToFile()LinkedList linkedList = new LinkedList();File file =
32、 new File(RandomNumber.txt);InputStreamReader reader;try reader = new InputStreamReader(new FileInputStream(file),UTF-8);BufferedReader bufferedReader = new BufferedReader(reader);String line = null; try while (line = bufferedReader.readLine() != null) int temp = Integer.parseInt(line); /System.out.
33、println(bubble-+temp); linkedList.add(temp);bufferedReader.close(); catch (IOException e1) / TODO 自动生成的 catch 块e1.printStackTrace(); catch (UnsupportedEncodingException e1) / TODO 自动生成的 catch 块e1.printStackTrace(); catch (FileNotFoundException e1) / TODO 自动生成的 catch 块e1.printStackTrace();return link
34、edList; 6.4 DirectSelect类import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;i
35、mport java.io.UnsupportedEncodingException;import java.util.LinkedList;public class DirectSelect void writerToFile(LinkedList linkedList)/存储到DirectSelect.txt文件中File file = new File(DirectSelect.txt);try OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file),UTF-8); BufferedWriter bufferedWriter = new BufferedWriter(write); for(int i=0;ilinkedList.size();i+)String str = String.valueOf(linkedList.get(i);String string = new String(str.getBytes(iso8859-1),UTF-8);bufferedWriter.write(string+n);