[关闭]
@w1024020103 2017-02-25T14:53:13.000000Z 字数 2090 阅读 1043

Lab 3 Notes

CS61B UCBerkeley


Lab 3 Unit Testing with JUnit, Debugging

Test a Reverse Method

Error: 程序包org.junit不存在

1.JPG-18.2kB

Solution:确定已经加了External Library : javalib, 里面包括junit-4.12.jar,重启IntelliJ,不报错.

Writing a Reverse Method

Write a reverse method, and rerun the tests until it passes. If you're stuck (this is a tricky problem with a very clever solution), see the week 3 discussion solutions.

discussion 3 reverse method for SLList (iteratively)
2.JPG-60.1kB

reverse method for IntList (iteratively)

  1. /**
  2. * Returns the reverse of the given IntList.
  3. * This method is destructive. If given null
  4. * as an input, returns null.
  5. */
  6. public static IntList reverse(IntList A){
  7. if( A == null){
  8. return null;
  9. }
  10. IntList frontOfReversed = null;
  11. IntList nextNodeToAdd = A;
  12. while(nextNodeToAdd != null) {
  13. IntList remainderOfOriginal = nextNodeToAdd.rest;
  14. nextNodeToAdd.rest = frontOfReversed;
  15. frontOfReversed = nextNodeToAdd;
  16. nextNodeToAdd = remainderOfOriginal;
  17. }
  18. return frontOfReversed;
  19. }

A Debugging Mystery

Using any combination of the following techniques, figure out whether the bug is in Horrible Steve's code or in Flik enterprise's library:

Tip: JUnit provides methods assertTrue(boolean) and assertTrue(String, boolean) that you might find helpful.

Try to come up with a short explanation of the bug! Check in with your TA to see if your answer is right (not for a grade).

  1. /** An Integer tester created by Flik Enterprises. */
  2. public class Flik {
  3. public static boolean isSameNumber(Integer a, Integer b) {
  4. return a == b;
  5. }
  6. }

I somehow found out the bug to beInteger a, Integer b, so I modified the code then the output were right.

  1. /** An Integer tester created by Flik Enterprises. */
  2. public class Flik {
  3. public static boolean isSameNumber(int a, int b) {
  4. return a == b;
  5. }
  6. }

Explanation: Integer type is diffrent form int.....

Recap

In this lab, we went over:

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注