软件测试技术-随堂练习01答案(共8页)_第1页
软件测试技术-随堂练习01答案(共8页)_第2页
软件测试技术-随堂练习01答案(共8页)_第3页
软件测试技术-随堂练习01答案(共8页)_第4页
软件测试技术-随堂练习01答案(共8页)_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

1、精选优质文档-倾情为你奉上随堂练习011. FindBugsA.查看 FindBugs发现的问题,一共有几类问题,各是什么含义?将这几类问题翻译成中文;答:一共有以下三种类型的问题:1. Bug: Invocation of toString on combination in solver.Matrix.main(String)The code invokes toString on an array, which will generate a fairly useless result such as C16f0472. Consider using Arrays.toString to

2、 convert the array into a readable String that gives the contents of the array. 中文:在组合代码调用toString数组,这将产生一个相当无用的结果。考虑使用Arrays.toString方法将数组转化成可读的字符串。2. Bug: solver.Mpute() concatenates strings using + in a loopThe method seems to be building a String using concatenation in a loop. In each iteration,

3、 the String is converted to a StringBuffer/StringBuilder, appended to, and converted back to a String. This can lead to a cost quadratic in the number of iterations, as the growing string is recopied in each iteration. Better performance can be obtained by using a StringBuffer (or StringBuilder in J

4、ava 1.5) explicitly.中文:这个方法似乎是建立在一个循环中使用连接字符串。在每个迭代中,字符串转换为StringBuffer / StringBuilder,附加到,转换回一个字符串。这可能导致成本数量的二次迭代,在每个迭代字符串复制。可以获得更好的性能通过使用StringBuffer显式(或StringBuilder Java 1.5中)。  3. Bug: Unread field: gui.MainWindow.m_gameOutputThis field is never read. Consider removing it from the class.中

5、文:gui.MainWindow.m_gameOutput这个属性没有被读。这个字段是不会被程序读的。考虑把它从类中移除。B.如何解决 FindBugs指出的问题?请修改Sudoku程序,以改正所有 FindBugs指出的问题。注意:同类错误仅需记录一次修改思想即可。答:1. 使用Arrays.toString将数组转换为字符串。2. 将String类型的该变量定义为StringBuffer类型的,并在循环体中用append()方法替代+操作。3. 通过”查找”找出对应的属性的所有代码,并注释删除。(或者调用print方法输出该属性)2. Junit源程序:package sale;publi

6、c class SaleMachine private int countOfBeer, countOfJuice, countOfFiveCents, countOfOneDollar;private String typeOfGoods = "Beer", "Juice"private String resultOfDeal;public SaleMachine()initial();public void initial()/默认售货机countOfBeer = 3;countOfJuice = 3;countOfFiveCents = 3;cou

7、ntOfOneDollar = 3;public SaleMachine(int fiveCents, int oneDollar, int numOfBeer, int numOfOrange)/带参数的初始化售货机countOfFiveCents = fiveCents;countOfOneDollar = oneDollar;countOfBeer = numOfBeer;countOfJuice = numOfOrange;public String currentState()/返回当前售货机状态String state = "库存 n" + "啤酒:

8、" + countOfBeer + "n" + "橙汁: " + countOfJuice + "n" + "五角硬币: " + countOfFiveCents + "n" + "一元硬币: " + countOfOneDollar;return state;public String buy(String type, int money)/用户购买的方法。type是用户选择的产品,money是用户投币种类if(money=50) /如果用户投入5角钱if(typ

9、e.equals(typeOfGoods0) /如果用户选择啤酒if(countOfBeer>0) /如果还有啤酒countOfBeer-;countOfFiveCents+;resultOfDeal = "请取出饮料"return resultOfDeal;elseresultOfDeal = "目前该饮料已经售完"return resultOfDeal;else if (type.equals(typeOfGoods1) /用户选择橙汁if(countOfJuice > 0)countOfJuice-;countOfFiveCents+;

10、resultOfDeal = "请取出饮料"return resultOfDeal;elseresultOfDeal = "目前该饮料已经售完"return resultOfDeal;elseresultOfDeal = "目前不提供该饮料"return resultOfDeal;else if(money=100) /如果用户投入一元钱if(countOfFiveCents > 0) /如果售货机有零钱if(type.equals(typeOfGoods0)&&countOfBeer>0)/如果用户选择啤

11、酒而且还有啤酒countOfBeer-;countOfFiveCents-;countOfOneDollar+;resultOfDeal = "请取饮料,并取回找钱"return resultOfDeal;else if (type.equals(typeOfGoods1)&&countOfJuice>0)/如果用户选择橙汁而且还有橙汁countOfJuice -;countOfFiveCents -;countOfOneDollar +;resultOfDeal = "请取饮料,并取回找钱"return resultOfDeal;

12、elseif(type.equals(typeOfGoods0)&&countOfBeer<=0)resultOfDeal = "目前该饮料已经售完"return resultOfDeal;else if(type.equals(typeOfGoods1)&&countOfJuice<=0)resultOfDeal = "目前该饮料已经售完"return resultOfDeal;elseresultOfDeal = "目前不提供该饮料"return resultOfDeal;elseres

13、ultOfDeal = "对不起,目前不能找钱,请投 5 角硬币"return resultOfDeal;resultOfDeal = "请投 5 角或者1 元的硬币"return resultOfDeal;测试程序以下测试场景均基于上述程序:测试场景:投币 1 元购买橙汁三次,然后投币 1 元购买啤酒三次。预期结果:显示“对不起,目前不能找钱,请投 5 角硬币”函数代码:public void test_NoCoin()SaleMachine sm = new SaleMachine();assertEquals(sm.buy("orange

14、", 100), "请取饮料,并取回找钱");assertEquals(sm.buy("orange", 100), "请取饮料,并取回找钱");assertEquals(sm.buy("orange", 100), "请取饮料,并取回找钱");assertEquals(sm.buy("beer",100), "对不起,目前不能找钱,请投 5 角硬币");测试结果:通过测试场景:投币1元,购买可乐预期结果:显示“目前不提供该饮料”函数代码:pub

15、lic void test_Cola()/售货机有5角,投币1元,选择非啤酒非橙汁SaleMachine sm = new SaleMachine();assertEquals(sm.buy("Cola", 100), "目前不提供该饮料");测试结果:通过测试场景:投币5块,购买橙汁预期结果:显示"请投 5 角或者1 元的硬币"函数代码:public void test_500orange()/售货机没有啤酒剩余,用户投币5角,选择啤酒SaleMachine sm = new SaleMachine();assertEquals(s

16、m.buy("orange", 500), "请投 5 角或者1 元的硬币");测试结果:通过测试场景:投币 1元购买啤酒一次预期结果:显示“请取饮料,并取回找钱”函数代码:public void test_50Beer3()/测试连续买三次啤酒每次投币5角SaleMachine sm = new SaleMachine();assertEquals(sm.buy("beer", 50), "请取饮料,并取回找钱");测试结果:通过测试场景:投币 5角购买啤酒四次预期结果:显示“目前该饮料已经售完”函数代码:pub

17、lic void test_50Beer3()/测试连续买三次啤酒每次投币5角SaleMachine sm = new SaleMachine();assertEquals(sm.buy("beer", 50), "请取出饮料");assertEquals(sm.buy("beer", 50), "请取出饮料");assertEquals(sm.buy("beer", 50), "请取出饮料");assertEquals(sm.buy("beer", 50), "目前该饮料已经售完");测试结果:通过测试场景:初始化五角/一元硬币/啤酒/橙汁都为4个,投币50,购买5次橙汁预期结果:显示“请取出饮料”*4 + "目前该饮料已经售完"函数代码:public void test_50Orange4()/售货机没有啤酒剩余,用户投币5角,选择啤酒SaleMachine sm = new SaleMachine(4,4,4,4);assertEquals(sm.buy("orange", 50), "请取出饮料&quo

温馨提示

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

评论

0/150

提交评论