20162326 齐力锋 实验三
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20162326 齐力锋 实验三相关的知识,希望对你有一定的参考价值。
实验报告
课程:程序设计与数据结构
班级: 1623
姓名: 齐力锋
学号:2016232 成绩: 2分 指导教师:娄嘉鹏 王志强
实验日期:11月6日
密级:非密级
预习程度: 已预习
必修/选修: 必修
实验序号: 2326
实验一:
- 完成教材P302 Searching.Java ,P305 Sorting.java中方法的测试
- 不少于10个测试用例,提交测试用例设计情况(正常,异常,边界,正序,逆序),用例数据中要包含自己学号的后四位
- 提交运行结果图(可多张)
实验二:
- 重构你的代码
- 把Sorting.java Searching.java放入 cn.edu.besti.cs1623.(姓名首字母+四位学号) 包中
- 把测试代码放test包中
- 重新编译,运行代码,提交编译,运行的截图(IDEA,命令行两种)
实验三:
- 参考http://www.cnblogs.com/maybe2030/p/4715035.html 在Searching中补充查找算法并测试
- 提交运行结果截图
实验四:
- 补充实现课上讲过的排序方法:希尔排序,堆排序,桶排序,二叉树排序等
- 测试实现的算法(正常,异常,边界)
- 提交运行结果截图
- 推送相关代码到码云上
实验过程及问题解决:
一、
第一项实验主要是对Searching和Sorting的各个方法进行测试
主要考察对泛型的应用和对查找算法、排序算法的了解和应用
实验一并不是很难,只是步骤较多。
需要测试边界情况——数组的0下标位置和最后一个元素
异常情况——数组中没有的元素
正序和逆序
依次将各个方法使用Junit测试,测试成功即可
package test; import cn.edu.besti.cs1623qi26.Searching; import junit.framework.Test; import junit.framework.TestSuite; import junit.framework.TestCase; /** * cn.edu.besti.cs1623qi26.Searching Tester. * * @author <Authors name> * @since <pre>11/06/2017</pre> * @version 1.0 */ public class SearchingTest extends TestCase { public SearchingTest(String name) { super(name); } public void setUp() throws Exception { super.setUp(); } public void tearDown() throws Exception { super.tearDown(); } /** * * Method: linearSearch(Comparable[] data, Comparable target) * */ public void testLinearSearch() throws Exception { //TODO: Test goes here... Comparable Test[] = {2,0,1,6,2,3,2,6}; assertEquals( 2, Searching.linearSearch(Test,2)); assertEquals( 0, Searching.linearSearch(Test,0)); assertEquals( 1, Searching.linearSearch(Test,1)); assertEquals( 6, Searching.linearSearch(Test,6)); assertEquals( 3, Searching.linearSearch(Test,3)); assertEquals( 2, Searching.linearSearch(Test,2)); assertEquals( null, Searching.linearSearch(Test,4));//yic异常 无该值 Comparable Test1[] = {"齐","力","锋"}; assertEquals("齐", Searching.linearSearch(Test1,"齐")); assertEquals("力", Searching.linearSearch(Test1,"力")); assertEquals("锋", Searching.linearSearch(Test1,"锋")); } /** * * Method: binarySearch(Comparable[] data, Comparable target) * */ public void testBinarySearch() throws Exception { //TODO: Test goes here... Comparable Test[] = {2,0,1,6,2,3,2,6}; assertEquals(null, Searching.binarySearch(Test,2));//异常情况 二分查找的查找池无序 Comparable testNormal [] = {1,2,3,5,7,9,16,18}; assertEquals( 3, Searching.binarySearch(testNormal,3)); //正常情况 正序 assertEquals( 5, Searching.binarySearch(testNormal,5)); //正常情况 正序 assertEquals( 7, Searching.binarySearch(testNormal,7)); //正常情况 正序 assertEquals( 9, Searching.binarySearch(testNormal,9)); //正常情况 正序 assertEquals( 16, Searching.binarySearch(testNormal,16)); //正常情况 正序 assertEquals( 18, Searching.binarySearch(testNormal,18)); //正常情况 正序 assertEquals(null, Searching.binarySearch(testNormal,4));//异常情况 无该值 Comparable testReverse [] = {18,16,9,7,5,3,2,1}; assertEquals( 3, Searching.binarySearch(testNormal,3)); //正常情况 逆序 assertEquals( 5, Searching.binarySearch(testNormal,5)); //正常情况 逆序 assertEquals( 7, Searching.binarySearch(testNormal,7)); //正常情况 逆序 assertEquals( 9, Searching.binarySearch(testNormal,9)); //正常情况 逆序 assertEquals( 16, Searching.binarySearch(testNormal,16)); //正常情况 逆序 assertEquals( 18, Searching.binarySearch(testNormal,18));//正常情况 逆序 } public static Test suite() { return new TestSuite(SearchingTest.class); } }
package test; import cn.edu.besti.cs1623qi26.Sorting; import junit.framework.Test; import junit.framework.TestSuite; import junit.framework.TestCase; /** * cn.edu.besti.cs1623qi26.Sorting Tester. * * @author <Authors name> * @since <pre>11/06/2017</pre> * @version 1.0 */ public class SortingTest extends TestCase { public SortingTest(String name) { super(name); } public void setUp() throws Exception { super.setUp(); } public void tearDown() throws Exception { super.tearDown(); } /** * * Method: selectionSort(Comparable[] data) * */ public void testSelectionSort() throws Exception { //TODO: Test goes here... Comparable testNormal [] = {1,3,6,5,7,9,20,18}; Sorting.selectionSort(testNormal); System.out.println("选择排序测试"); for (Comparable i : testNormal) System.out.print(i + " "); } /** * * Method: insertionSort(Comparable[] data) * */ public void testInsertionSort() throws Exception { //TODO: Test goes here... Comparable testNormal [] = {1,3,6,5,7,9,20,18}; Sorting.insertionSort(testNormal); System.out.println("插入排序测试"); for (Comparable i : testNormal) System.out.print(i + " "); } /** * * Method: bubbleSort(Comparable[] data) * */ public void testBubbleSort() throws Exception { //TODO: Test goes here... Comparable testNormal [] = {1,3,6,5,7,9,20,18}; Sorting.bubbleSort(testNormal); System.out.println("冒泡排序测试"); for (Comparable i : testNormal) System.out.print(i + " "); } /** * * Method: quickSort(Comparable[] data, int min, int max) * */ public void testQuickSort() throws Exception { //TODO: Test goes here... Comparable testNormal [] = {1,3,6,5,7,9,20,18}; Sorting.quickSort(testNormal,0,7); System.out.println("快速排序测试"); for (Comparable i : testNormal) System.out.print(i + " "); } /** * * Method: mergeSort(Comparable[] data, int min, int max) * */ public void testMergeSort() throws Exception { //TODO: Test goes here... Comparable testNormal [] = {1,3,6,5,7,9,20,18}; Sorting.mergeSort(testNormal,0,7); System.out.println("归并排序测试"); for (Comparable i : testNormal) System.out.print(i + " "); } /** * * Method: merge(Comparable[] data, int first, int mid, int last) * */ public void testMerge() throws Exception { //TODO: Test goes here... Comparable testNormal [] = {1,3,6,5,7,9,20,18,31}; Sorting.merge(testNormal,0,4,8); System.out.println("归并排序测试"); for (Comparable i : testNormal) System.out.print(i + " "); } /** * * Method: main(String args[]) * */ public void testMain() throws Exception { //TODO: Test goes here... } /** * * Method: swap(Comparable[] data, int index1, int index2) * */ public void testSwap() throws Exception { //TODO: Test goes here... /* try { Method method = cn.edu.besti.cs1623qi26.Sorting.getClass().getMethod("swap", Comparable[].class, int.class, int.class); method.setAccessible(true); method.invoke(<Object>, <Parameters>); } catch(NoSuchMethodException e) { } catch(IllegalAccessException e) { } catch(InvocationTargetException e) { } */ } /** * * Method: partition(Comparable[] data, int min, int max) * */ public static Test suite() { return new TestSuite(SortingTest.class); } }
二、
把Sorting.java Searching.java放入 cn.edu.besti.cs1623qi26 包中
把测试代码放到test包中
在测试代码前需要输入一下代码:
package test; import cn.edu.besti.cs1623qi26.Searching; import junit.framework.Test; import junit.framework.TestSuite; import junit.framework.TestCase;
在Linux下的方法
第一步 工程打包成一个jar包
第二步 用import关键字导入包 变量必须是public访问权限
第三步 在linux下执行java -jar jar包名字 就可以实现一个.java文件调用另一个.java文件了
package cn.edu.besti.cs1623qi26; //******************************************************************** // cn.edu.besti.cs1623qi26.Searching.java Java Foundations // // Contains various search algorithms that operate on an array of // Comparable objects. //******************************************************************** public class Searching { //----------------------------------------------------------------- // Searches the specified array of objects using a linear search // algorithm. Returns null if the target is not found. //----------------------------------------------------------------- public static Comparable linearSearch (Comparable[] data, Comparable target) { Comparable result = null; int index = 0; while (result == null && index < data.length) { if (data[index].compareTo(target) == 0) result = data[index]; index++; } return result; } /*----------------------------------------------------------------- // Searches the specified array of objects using a binary search // algorithm. Returns null if the target is not found. //----------------------------------------------------------------- */ public static Comparable binarySearch (Comparable[] data, Comparable target) { Comparable result = null; int first = 0, last = data.length-1, mid; while (result == null && first <= last) { mid = (first + last) / 2; // determine midpoint if (data[mid].compareTo(target) == 0) result = data[mid]; else if (data[mid].compareTo(target) > 0) last = mid - 1; else first = mid + 1; } return result; } }
package test; import cn.edu.besti.cs1623qi26.Searching; import junit.framework.Test; import junit.framework.TestSuite; import junit.framework.TestCase; /** * cn.edu.besti.cs1623qi26.Searching Tester. * * @author <Authors name> * @since <pre>11/06/2017</pre> * @version 1.0 */ public class SearchingTest extends TestCase { public SearchingTest(String name) { super(name); } public void setUp() throws Exception { super.setUp(); } public void tearDown() throws Exception { super.tearDown(); } /** * * Method: linearSearch(Comparable[] data, Comparable target) * */ public void testLinearSearch() throws Exception { //TODO: Test goes here... Comparable Test[] = {2,0,1,6,2,3,2,6}; assertEquals( 2, Searching.linearSearch(Test,2)); assertEquals( 0, Searching.linearSearch(Test,0)); assertEquals( 1, Searching.linearSearch(Test,1)); assertEquals( 6, Searching.linearSearch(Test,6)); assertEquals( 3, Searching.linearSearch(Test,3)); assertEquals( 2, Searching.linearSearch(Test,2)); assertEquals( null, Searching.linearSearch(Test,4));//yic异常 无该值 Comparable Test1[] = {"齐","力","锋"}; assertEquals("齐", Searching.linearSearch(Test1,"齐")); assertEquals("力", Searching.linearSearch(Test1,"力")); assertEquals("锋", Searching.linearSearch(Test1,"锋")); } /** * * Method: binarySearch(Comparable[] data, Comparable target) * */ public void testBinarySearch() throws Exception { //TODO: Test goes here... Comparable Test[] = {2,0,1,6,2,3,2,6}; assertEquals(null, Searching.binarySearch(Test,2));//异常情况 二分查找的查找池无序 Comparable testNormal [] = {1,2,3,5,7,9,16,18}; assertEquals( 3, Searching.binarySearch(testNormal,3)); //正常情况 正序 assertEquals( 5, Searching.binarySearch(testNormal,5)); //正常情况 正序 assertEquals( 7, Searching.binarySearch(testNormal,7)); //正常情况 正序 assertEquals( 9, Searching.binarySearch(testNormal,9)); //正常情况 正序 assertEquals( 16, Searching.binarySearch(testNormal,16)); //正常情况 正序 assertEquals( 18, Searching.binarySearch(testNormal,18)); //正常情况 正序 assertEquals(null, Searching.binarySearch(testNormal,4));//异常情况 无该值 Comparable testReverse [] = {18,16,9,7,5,3,2,1}; assertEquals( 3, Searching.binarySearch(testNormal,3)); //正常情况 逆序 assertEquals( 5, Searching.binarySearch(testNormal,5)); //正常情况 逆序 assertEquals( 7, Searching.binarySearch(testNormal,7)); //正常情况 逆序 assertEquals( 9, Searching.binarySearch(testNormal,9)); //正常情况 逆序 assertEquals( 16, Searching.binarySearch(testNormal,16)); //正常情况 逆序 assertEquals( 18, Searching.binarySearch(testNormal,18));//正常情况 逆序 } public static Test suite() { return new TestSuite(SearchingTest.class); } }
三、
以上是关于20162326 齐力锋 实验三的主要内容,如果未能解决你的问题,请参考以下文章
2062326 齐力锋 实验一《Java开发环境的熟悉》实验报告
2062326 齐力锋 实验二《Java面向对象程序设计》实验报告