服务计算概论作业报告_第1页
服务计算概论作业报告_第2页
服务计算概论作业报告_第3页
服务计算概论作业报告_第4页
服务计算概论作业报告_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

.慕测平台测试报告(二)姓名:赵红娜专业:软件工程学号:313060800310-222016年10月22.日针对以下4个项目编写测试用例进行测试。代码如下:题目(1)//BinaryHeapclass////CONSTRUCTION:withoptionalcapacity(thatdefaultsto100)////******************PUBLICOPERATIONS*********************voidinsertx)-->InsertxeMinReturnandallitems//******************ERRORS********************************//ThrowsOverflowifcapacityexceeded/***Implementsabinaryheap.*Notethatall"matching"isbasedonremovesmallestitemthecompareTomethod.//intfindMin()-->Returnsmallest*@authorMarkAllenWeiss//booleanisEmpty()-->ReturnpublicclassBinaryHeaptrueifempty;elsefalse{//booleanisFull()-->Return//@invariantwellFormed();trueiffull;elsefalse/**voidmakeEmpty-->Remove*Constructthebinaryheap.publicBinaryHeap(){this(DEFAULT_CAPACITY);}/***Constructthebinaryheap.*@paramcapacitythecapacityofthebinaryheap.//@requirescapacity>0;//@ensuresisEmpty();publicBinaryHeap(intcapacity){currentSize=0;array=newint[capacity+1];}/***Insertintothepriorityqueue,maintainingheaporder.*Duplicatesareallowed..*@paramxtheitemtoinsert.*@exceptionOverflowifcontainerisfull.publicvoidinsert(intx)throwsOverflow{if(isFull())thrownewOverflow();//Percolateupinthole=++currentSize;for(;hole>1&&x<array[hole/2];hole/=2)array[hole]=array[hole/array[hole]=x;}/***Findthesmallestiteminthepriorityqueue.*@returnthesmallestitem,ornull,ifempty.publicintfindMin(){if(isEmpty())return-1;returnarray[1];}booleanwellFormed(){if(array==null){//array!=nullreturnfalse;}if(currentSize<0||currentSize>=array.length){//currentSize>=0;currentSize<array.length;returnfalse;}for(inti=1;i<currentSize;i++){if(i*2<=currentSize&&array[i]>array[2*i]){.returnfalse;}if(i*2+1<=currentSize&&array[i]>array[2*i+1]){returnfalse;}}returntrue;}/***Removethesmallestitemfromthepriorityqueue.*@returnthesmallestitem,ornull,ifempty.publicintdeleteMin(){if(isEmpty())return-1;intminItem=findMin();array[1]=array[currentSize--];.percolateDown(1);returnminItem;}/***Establishheaporderpropertyfromanarbitrary*arrangementofitems.Runsinlineartime.publicvoidbuildHeap(){for(inti=currentSize/2;i>0;percolateDown(i);}/***Testifthepriorityqueueislogicallyempty.*@returntrueifempty,falseotherwise.publicbooleanisEmpty(){returncurrentSize==0;}/***Testifthepriorityqueueislogicallyfull.*@returntrueiffull,falseotherwise.publicbooleanisFull(){returncurrentSize==array.length-1;}/***Makethepriorityqueuelogicallyempty.//@ensuresisEmpty();publicvoidmakeEmpty().{currentSize=0;}privatestaticfinalintDEFAULT_CAPACITY=100;privateintcurrentSize;//Numberofelementsinheapprivateint[]array;//Theheapray/***Internalmethodtopercolatedownintheheap.*@paramholetheindexatwhichthepercolatebegins.privatevoidpercolateDown(int{intchild;for(;hole*2<=currentSize;hole=child){child=hole*2;if(child!=currentSize&&array[child+1]<array[child])child+;if(array[child]<tmp)array[hole]=array[child];elsebreak;}array[hole]=tmp;}}inttmp=array[hole];./***Exceptionclassforaccessinfullcontainers*suchasstacks,queues,andpriorityqueues. *@authorMarkAllenWeiss publicclassOverflowextendsException {}题目(2)importjava.util.Comparator;importjava.util.Random;/***Aclassthatcontainsseveralsortinges*implementedasstaticmethods.*Arraysarerearrangedwithsmallestitemfirst,*usingcompareTo.*@authorMarkAllenWeisspublicfinalclassSorting{/***Simpleinsertionsort.*@paramaanarrayofComparableitems.publicvoidinsertionSort(int[]a){for(intp=1;p<a.length;{inttmp=a[p];for(j=p;j>0&&tmp<a[j-1];j--)a[j]=a[j-1];a[j]=tmp;}}publicbooleanisSorted(int[]a){for(inti=0;i<a.length-1;i++){if(a[i]>a[i+1]){returnfalse}}turntrue}publicstaticvoidquicksort(int[]{quicksort(a,0,a.length-1);}privatestaticfinalintCUTOFF=10;publicstaticfinalvoid.swapReferences(Object[]a,intindex1,intindex2){Objecttmp=a[index1];a[index1]=a[index2];a[index2]=tmp;}publicstaticfinalvoidswap(int[]a,intindex1,intindex2){inttmp=a[index1];a[index1]=a[index2];a[index2]=tmp;}privatestaticintmedian3(int[]a,intleft,intright){intcenter=(left+right)/2;if(a[center]<a[left])swap(a,left,center);if(a[right]<a[left])swap(a,left,right);if(a[right]<a[center]).swap(a,center,right);swap(a,i,j);//Placepivotatpositionright-1swap(a,center,right-1);returna[right-1];}eak}swap(a,i,right-1);//privatestaticvoidquicksort(int[]a,intleft,intright){if(left+CUTOFF<=right){intpivot=median3(a,left,inti=left,j=right-1;for(;;){while(a[++i]<pivot)}while(a[--j]>pivot)}jquicksortalefti-1);//Sortsmallelementsquicksort(a,i+1,right);//Sortlargeelements}else//DoaninsertionsortonthesubarrayinsertionSort(a,left,}privatestaticvoidinsertionSort(int[]a,intleft,intright){for(intp=left+1;p<=right;{inttmp=a[p];for(j=p;j>left&&tmpaj];j--)a[j]=a[j-1];a[j]=tmp;题目(3)publicclassStatistics{/****@paramnumbers*@returnthelengthofthearraypublicintcalLength(int[]numbers){intlength=numbers.length;returnlength;}/**.}}privatestaticfinalintNUM_ITEMSprivatestaticinttheSeed=1;}**@paramnumbers*@returnthemeanvalueoftheraypublicdoublecalMean(int[]numbers){intlength=calLength(numbers);doublesum;sum=0.0;for(inti=0;i<length;i++){sum+=numbers[i];}doublemean=sum/(double)returnmean;}/****@paramnumbers*@returnthevarvalueofthe.doublemean=calMean(numbers);doublevarsum=0.0;for(inti=0;i<length;i++){varsum=varsum+((numbers[i]-mean)*(numbers[i]-an}doublevar=varsum/(lengthraypublicdoublecalVar(int[]numbers){intlength=calLength(numbers);题目(4)publicclassTriangle{protectedlonglborderA=0;protectedlonglborderB=0;protectedlonglborderC=0;returnvar;}}//ConstructorpublicTriangle(longlborderA,longlborderB,longlborderC){this.lborderA=lborderA;.this.lborderB=lborderB;this.lborderC=lborderC;}/***checkifitisatriangle**@returntruefortriangleandfalsenotpublicbooleanisTriangle(Triangletriangle){booleanisTriangle=false;//checkboundaryif((triangle.lborderA>0&&triangle.lborderA<=Long.MAX_VALU(triangle.lborderB>0&&triangle.lborderB<=(triangle.lborderC>0&&triangle.lborderC<=Long.MAX_VALU){//checkifsubtractionoftwoborderlargerthanthethirdif(diffOfBorders(triangle.lborderA,triangle.lborderB)<triangle.lborderCdiffOfBorders(triangle.lborderB,triangle.lborderC)<triangle.lborderAdiffOfBorders(triangle.lborderC,triangle.lborderA)<triangle.lborderB){isTriangle=true;}}returnisTriangle;}Long.MAX_VALU/***Checkthetypeoftriangle**Consistsof"Illegal","Regular","Scalene","Isosceles"publicStringgetType(Triangletriangle){StringstrType="Illegal";if(isTriangle(triangle)){//IsRegularif(triangle.lborderA==triangle.lborderBtriangle.lborderB==triangle.lborderC).(triangle.lborderA!=triangle.lborderC)){strTypeScalene;} //ifisosceles else{strType="Isosceles";}}returnstrType;}/***calculatethediffbetween{strType="Regular";*}**/ //Ifscalene publiclongdiffOfBorders(longa,elseiflongb){((triangle.lborderA!=triangle.lborderB)return(a>b)?(a-b):(b-a);}(triangle.lborderB!=triangle.lborderC).borders[1]=this.lborderB;/**borders[2]=this.lborderC;*getlengthofbordersreturnborders;}publiclong[]getBorders(){}long[]borders=newlong[3];borders[0]=this.lborderA;题目(1)importorg.junit.After;importorg.junit.Before;importorgjunitTestimportorg.junit.Assert;publicvoidsetUp()Exception{}publicvoidtearDown()throwsthrowsException{publicclassBinaryHeapTest{}.publicvoidtest(){BinaryHeapheap1=newBinaryHeap(1024);for(inti=1024;i>0;i--){try{heap1.insert(i);}catch(Overflowe){Assert.fail(e.getMessage());}}heap1.makeEmpty();heap1.findMin();heap1.deleteMin();}}题目(2)if(heap1.wellFormed()){heap1.buildHeap();}heap1.deleteMin();importorg.junit.Before;importorg.junit.After;importorgjunitTestpublicclassSortingTest{privateintnum;privateint[]num1=newint[50];privateint[]num2=newint[50];privateObject[]ref=newObject[]{3,4,5,6,9};privateSortingsorting;publicvoidsetUp()throws.Exception{}sorting=newSorting();for(inti=0;i<50;i++){num=(int)(Math.random()*50);num1[i]=num;}for(inti=0;i<50;i++){num=(int)(Math.random()*50);num2[i]=num;}publicvoidtest(){sorting.quicksort(num1);sorting.isSorted(num1);sorting.insertionSort(num2);sorting.isSorted(num2);sorting.swapReferences(ref,1,}}}}publicvoidtearDown()throwsException{题目(3)importorg.junit.After;importorg.junit.Before;importorgjunitTestpublicclassStatisticsT

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论