楼主: jaly50
跳转到指定楼层
上一主题 下一主题
收起左侧

Berkeley CS 61B Data Structures(in Java) Homework5 加分+讨论帖

 
🔗
joannahsu 2016-5-18 00:20:17 | 只看该作者
全局:
本帖最后由 joannahsu 于 2016-5-18 00:29 编辑

Done.交作业

1.png (46.09 KB, 下载次数: 0)

part1

part1

评分

参与人数 1学分 +1 收起 理由
yingy4 + 1 坚持的不错,再接再厉!

查看全部评分

回复

使用道具 举报

🔗
James822 2016-5-20 02:16:51 | 只看该作者
全局:
本帖最后由 James822 于 2016-5-20 02:25 编辑

作业虽然完成了 但我和其他同学不太一样 并不是卡在并补的计算上 而是对throw catch感觉没搞懂
一直在想为什么不直接判mylist是否为Null来表示是正常节点还是不可用节点(sentinel或者已删除的节点) 而必须要throw再catch
大家代码里应该都会用到这样的表达吧 比如对某个DList遍历的时候
那try 是加在while这个loop呢还是里面.item的函数外面呢

因为while里判断的条件正是throw判断的isvalid的代码所以感觉很重复 而且只要能进while循环的节点 只要不next prev去操作其他Node的话 肯定是合法的
但由于throw catch的语法又必须要求你写try 即使while循环里没有这样的操作(比如仅仅是对本node的item())
真心没搞懂为什么要加throw catch操作而不是直接利用Mylist的Null给外部访问的人
视频里老师说的是invalidenode的异常信息比null提供的信息更全  可我完全可以提供多个表示不同含义的invalid常量也能做到
比如null 是 0, invalid node是65535之类的



while (sIncur.isValidNode())  //总会先判这个节点的有效性
{
     .....
     sIncur.item();   //这中间又会用到Item的值  
     .....

}


运行的结果如下  感谢前面同学提供空集合测试代码
  1. Set s = {  3  4  }
  2. Set s2 = {  4  5  }
  3. Set s3 = {  3  5  8  }
  4. After s.union(s2), s = {  3  4  5  }
  5. After s.intersect(s3), s = {  3  5  }
  6. s.cardinality() = 2
  7. Empty Set s4 = {  }
  8. s4.cardinality() = 0
  9. After s4.union(s4), s4 = {  }
  10. After s4.intersect(s4), s4 = {  }
  11. After s3.union(s4), s3 = {  3  5  8  }
  12. After s3.intersect(s4), s3 = {  }
复制代码
  1. An empty list should be [  ]: [  ]
  2. l.isEmpty() should be true: true
  3. l.length() should be 0: 0
  4. Finding front node p of l.
  5. p.isValidNode() should be false: false
  6. p.item() should throw an exception, and did.
  7. p.setItem() should throw an exception, and did.
  8. p.next() should throw an exception, and did.
  9. p.prev() should throw an exception, and did.
  10. p.insertBefore() should throw an exception, and did.
  11. p.insertAfter() should throw an exception, and did.
  12. p.remove() should throw an exception, and did.
  13. Finding back node p of l.
  14. p.isValidNode() should be false: false
  15. p.item() should throw an exception, and did.
  16. p.setItem() should throw an exception, and did.
  17. p.next() should throw an exception, and did.
  18. p.prev() should throw an exception, and did.
  19. p.insertBefore() should throw an exception, and did.
  20. p.insertAfter() should throw an exception, and did.
  21. p.remove() should throw an exception, and did.
  22. l after insertFront(10) should be [  10  ]: [  10  ]
  23. l is a list of 3 elements: [  1  2  3  ]
  24. n.item() should be 1: 1
  25. n.item() should be 2: 2
  26. n.item() should be 2: 2
  27. n.item() should be 4: 4
  28. n.item() should be 3: 3
  29. n.item() should be 6: 6
  30. After doubling all elements of l: [  2  4  6  ]
  31. p.isValidNode() should be false: false
  32. p.item() should throw an exception, and did.
  33. p.setItem() should throw an exception, and did.
  34. p.next() should throw an exception, and did.
  35. p.prev() should throw an exception, and did.
  36. p.insertBefore() should throw an exception, and did.
  37. p.insertAfter() should throw an exception, and did.
  38. p.remove() should throw an exception, and did.
  39. n.item() should be 6: 6
  40. n.item() should be 12: 12
  41. n.item() should be 4: 4
  42. n.item() should be 8: 8
  43. n.item() should be 2: 2
  44. n.item() should be 4: 4
  45. After doubling all elements of l again: [  4  8  12  ]
  46. p.isValidNode() should be false: false
  47. p.item() should throw an exception, and did.
  48. p.setItem() should throw an exception, and did.
  49. p.next() should throw an exception, and did.
  50. p.prev() should throw an exception, and did.
  51. p.insertBefore() should throw an exception, and did.
  52. p.insertAfter() should throw an exception, and did.
  53. p.remove() should throw an exception, and did.
  54. Removing middle element (8) of l: 8
  55. l is now: [  4  12  ]
  56. p.isValidNode() should be false: false
  57. p.item() should throw an exception, and did.
  58. p.setItem() should throw an exception, and did.
  59. p.next() should throw an exception, and did.
  60. p.prev() should throw an exception, and did.
  61. p.insertBefore() should throw an exception, and did.
  62. p.insertAfter() should throw an exception, and did.
  63. p.remove() should throw an exception, and did.
  64. Removing end element (12) of l: 12
  65. l is now: [  4  ]
  66. p.isValidNode() should be false: false
  67. p.item() should throw an exception, and did.
  68. p.setItem() should throw an exception, and did.
  69. p.next() should throw an exception, and did.
  70. p.prev() should throw an exception, and did.
  71. p.insertBefore() should throw an exception, and did.
  72. p.insertAfter() should throw an exception, and did.
  73. p.remove() should throw an exception, and did.
  74. Removing first element (4) of l: 4
  75. l is now: [  ]
  76. p.isValidNode() should be false: false
  77. p.item() should throw an exception, and did.
  78. p.setItem() should throw an exception, and did.
  79. p.next() should throw an exception, and did.
  80. p.prev() should throw an exception, and did.
  81. p.insertBefore() should throw an exception, and did.
  82. p.insertAfter() should throw an exception, and did.
  83. p.remove() should throw an exception, and did.
复制代码

评分

参与人数 1学分 +1 收起 理由
yingy4 + 1 坚持的不错,再接再厉!

查看全部评分

回复

使用道具 举报

🔗
Sophia_Z 2016-5-23 13:30:52 | 只看该作者
全局:
本帖最后由 Sophia_Z 于 2016-5-23 20:00 编辑

作业不难,主要学习了inheritance, exception以及如何运用List。
我觉得因为Comparable interface是在Java.lang里面提供的,而且java.lang是自动包含的,因此可以直接使用。

后来分别使用了althinking 和88垅的测试代码(非常感谢!)测试没有问题,但是总结思路的时候检查了一下代码发现自己写错了一个地方,又增加一些case,发现了intersect() method问题。
前面的测试代码没有包含 当遍历到s list的最后一个节点时对this list剩下所有的节点remove 的测试(如下面的例子,remove掉9和11)。写法不同可能不一定犯这个错误,但是为了防止小伙伴和我犯同样的错误建议增加intersect() method的测试代码:


Set s = new Set();
    try{
    s.insert(new Integer(3));  
    s.insert(new Integer(5));   
    s.insert(new Integer(3));
    s.insert(new Integer(4));
    s.insert(new Integer(1));
    s.insert(new Integer(9));
    s.insert(new Integer(11));
    }catch(InvalidNodeException invalidn){
            System.err.println("s"+invalidn);
    }
    System.out.println("Set s = " + s);

Set s3 = new Set();
    try{
    s3.insert(new Integer(5));
    s3.insert(new Integer(3));
    s3.insert(new Integer(8));
    s3.insert(new Integer(1));
    }catch(InvalidNodeException invalidn){
            System.err.println("s3"+invalidn);
    }
    System.out.println("Set s3 = " + s3);

try{   
             s.intersect(s3);
     }catch(InvalidNodeException invalidn){
             System.err.println("s.intersect(s3)"+invalidn);
     }
     System.out.println("After s.intersect(s3), s = " + s);
     System.out.println("Set s3 = " + s3);



结果为
Set s = { 1 3 4 5 9 11 }
Set s3 = { 1 3 5 8 }
After s.intersect(s3), s = { 1 3 5 }
Set s3 = { 1 3 5 8 }


更多图片 小图 大图
组图打开中,请稍候......

评分

参与人数 1学分 +1 收起 理由
yingy4 + 1 坚持的不错,再接再厉!

查看全部评分

回复

使用道具 举报

🔗
F_F 2016-5-24 20:24:21 | 只看该作者
全局:
之前长时间落下,现在补上
PartI太长,贴上PartII结果
最后换SList()试了下得到相同的结果,明白了List class的巧妙,收获颇多
LIST的函数返回都是ListNode, 而在子类具体实现中强制cast。

partII.png (11.55 KB, 下载次数: 0)

partII.png

评分

参与人数 1学分 +1 收起 理由
yingy4 + 1 坚持的不错,再接再厉!

查看全部评分

回复

使用道具 举报

🔗
beautifei 2016-5-25 02:52:48 | 只看该作者
全局:
看了别人的答案还做了好几天,最后自己成功编出来了。学到了好多,很有收获。
进度要加快!!!!!
更多图片 小图 大图
组图打开中,请稍候......

评分

参与人数 1学分 +1 收起 理由
yingy4 + 1 坚持的不错,再接再厉!

查看全部评分

回复

使用道具 举报

🔗
farewell 2016-5-28 10:50:30 | 只看该作者
全局:
太愚蠢了 卡了好
union和intersect两个要记得考虑空集!
坚持坚持!!!求加分


评分

参与人数 1学分 +1 收起 理由
yingy4 + 1 坚持的不错,再接再厉!

查看全部评分

回复

使用道具 举报

🔗
世界末日 2016-6-2 04:07:01 | 只看该作者
全局:
本帖最后由 ericjiang 于 2016-6-1 15:09 编辑

这次作业真的是看起来简单做起来难,要总结的也跟前面的同学说的差不多。protected的field和method在package外是无法使用的。如何实现良好的封装。另外对于exception,是不是要么throw出去,要么就一定要catch住?
Comparable这个东西还是不太懂。
除了掌握那些知识点,细心真的很重要!各种边界情况要好好考虑。debug真的很累啊。part2自己加了几个test cases,又发现了一些错误并修正了代码。



评分

参与人数 1学分 +1 收起 理由
yingy4 + 1 坚持的不错,再接再厉!

查看全部评分

回复

使用道具 举报

🔗
李土它 2016-6-11 20:43:56 | 只看该作者
全局:
艾玛,还是不太懂comparable,就强制转换直接用了,有小叹号,不知道对不对,运行结果是对的。
非常感谢前面同学提供的测试代码!好人一生平安-。-
Part 1


Part 2

评分

参与人数 1学分 +1 收起 理由
yingy4 + 1

查看全部评分

回复

使用道具 举报

🔗
topnessman 2016-6-11 22:58:39 | 只看该作者
全局:
交作业了。很有意思的作业,自己实现了一个hashset.
更多图片 小图 大图
组图打开中,请稍候......

评分

参与人数 1学分 +1 收起 理由
yingy4 + 1

查看全部评分

回复

使用道具 举报

🔗
topnessman 2016-6-15 00:21:41 | 只看该作者
全局:
做完作业不加学分了吗?呼叫版主

评分

参与人数 1大米 +1 收起 理由
yingy4 + 1 版主在旅游中不要着急哈~

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表