📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 22547| 回复: 89
跳转到指定楼层
上一主题 下一主题
收起左侧

Stanford CS106A 1#Assignment1讨论

全局:
本帖最后由 yiyiyaya 于 2012-7-25 10:28 编辑

时间:2012.07.24-2012.7.26

课程:
1. Welcome to CS106A
2. Programming with Karel
3. Problem-solving in Karel

教材:Karel the robot learns Java

作业:
Assignment1
due 2012.7.26

软件:
需要安装eclipse,安装前要有Java环境
下载地址http://see.stanford.edu/see/materials/icspmcs106a/software.aspx

发帖规则:
1. 发帖主要是Q&A,也可帖出学习心得
2. 回答的楼层,请使用“引用回复”
3. 回答的楼层,请将字体颜色设为白色
4. 出色的跟帖,将会给予积分奖励



单选投票, 共有 54 人参与投票
您所在的用户组没有投票权限

评分

参与人数 1大米 +3 收起 理由
LLLLLin + 3 给你点个赞!

查看全部评分


上一篇:有想法组织Stanford CS106A课程
下一篇:[Udacity CS215 Algorithm] 问个HW4计算中位数的题
🔗
nprotect 2012-7-25 00:55:30 | 只看该作者
全局:
assigment1之前主要是karel,熟悉一下基本的java语法结构,基本循环,decomposition的基本概念即可。assignment的4道大题的代码我在上个帖子贴出来了,就不复制了,具体请看: http://www.1point3acres.com/bbs/thread-36754-1-1.html    需要说明的是有些想法可能比较繁琐,尤其第四题,但是网上没有找到相对简单一些又没错误的答案。

评分

参与人数 1学分 +1 收起 理由
yiyiyaya + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-25 10:00:40 | 只看该作者
全局:
本帖最后由 yiyiyaya 于 2012-7-25 19:59 编辑

第四题确实很繁琐。下面我贴一下我的代码,其中有部分是借鉴别人的
MidpiontFindingKarel.java:
  1. import stanford.karel.*;

  2. public class MidpointFindingKarel extends SuperKarel {

  3.         public void run() {
  4.                 spreadBeepers(); //put beepers in one row
  5.                 pickBeepers(); //pick up those beepers which are not in the center
  6.         }
  7. /*
  8.   * Method:spreadBeepers() is to put beepers in one row
  9.   * postconditions: Karel is at the end of the row, facing the wall.
  10.   */
  11.         private void spreadBeepers() {
  12.                 while(frontIsClear()) {
  13.                         putBeeper();
  14.                         move();
  15.                 }
  16.                 putBeeper();
  17.         }        
  18. /*
  19.   * Method:pickBeepers() is to pick up those beepers which are not in the center
  20.   * preconditions: Karel is at the end of the row, facing the wall(east)
  21.   * postconditions: Karel is at the center of the row with the beeper
  22.   * Karel moves back and forth to pick up the first and the last beepers in turn.
  23.   * After picking up all the beepers in turn, Karel finally puts the beeper at the midpoint.
  24.   */
  25.         private void pickBeepers() {
  26.                 turnAround();//facing west
  27.                 pickBeeper();//pick the last beeper in the row
  28.                 while(frontIsClear()) move();//move until the first beeper in the row
  29.                 turnAround();//facing east
  30.                 pickBeeper();//pick up the first beeper in the row
  31.                 while(noBeepersPresent()) {
  32.                         move();
  33.                         while(beepersPresent()) move();
  34.                         turnAround();
  35.                         move();
  36.                         if(beepersPresent()) pickBeeper();//pick up the last and first beeper in the row in turn
  37.                         else putBeeper();//when there are no beepers in the row, put the midpoint beeper down
  38.                 }
  39.         }
  40. }
复制代码

评分

参与人数 1大米 +1 收起 理由
Brianlin + 1 回答的很好!

查看全部评分

回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-25 10:13:08 | 只看该作者
全局:
本帖最后由 yiyiyaya 于 2012-7-26 23:04 编辑

下面是CheckerboardKarel.java的程序:

  1. import stanford.karel.*;

  2. public class CheckerboardKarel extends SuperKarel {

  3.         public void run() {
  4.                 putBeeperInOneRow();
  5.                 while(leftIsClear()) {//leftIsClear() means the left of Karel is clear
  6.                         repositionToWest();
  7.                         putBeeperInOneRow();
  8.                
  9.                     if(rightIsClear()) {
  10.                             repositionToEast();
  11.                             putBeeperInOneRow();
  12.                     }
  13.                     else {
  14.                             turnAround();//When all rows are done, Karel turns around. This is a terminate order to tell Karel to stop.
  15.                     }
  16.                 }
  17.         }
  18.         /*
  19.          * Method: putBeeperInOneRow() is to put beepers in one row intervally
  20.          */
  21.         private void putBeeperInOneRow(){
  22.                 putBeeper();
  23.                 while(frontIsClear()){
  24.                     move();
  25.                     if(frontIsClear()){
  26.                             move();
  27.                         putBeeper();
  28.                     }
  29.             }
  30.         }
  31.         /*
  32.          * Method: repositionToWest() is to make Karel turn left and move up one row and then turn left again
  33.          * so that he can continue put beepers in a new row
  34.          * preconditions: Karel is at the end of the row facing east(Karel is at the right corner of the row)
  35.          * postconditions:Karel has moved up one row and face west
  36.          */
  37.         private void repositionToWest(){
  38.                 turnLeft();
  39.                 move();
  40.                 turnLeft();
  41.         }
  42.         /*
  43.          * Method: repositionToEast() is to make Karel turn right and move up one row and then turn right again
  44.          * so that he can continue put beepers in a new row
  45.          * preconditions: Karel is at the end of the row facing west(Karel is at the left corner of the row)
  46.          * postconditions:Karel has moved up one row and face east
  47.          */
  48.         private void repositionToEast(){
  49.                 turnRight();
  50.                 move();
  51.                 turnRight();
  52.         }
  53. }
复制代码

点评

这个代码奇数列时不对。 reposition里要先判断,再决定下一行是否错格。  发表于 2014-1-14 18:28
回复

使用道具 举报

🔗
nprotect 2012-7-25 11:49:24 | 只看该作者
全局:
yiyiyaya 发表于 2012-7-25 10:00
第四题确实很繁琐。下面我贴一下我的代码,其中有部分是借鉴别人的
MidpiontFindingKarel.java:


你这个代码就是有问题的,这题明显不太可能30几行就完成,题目下有明确说明需要符合各种条件,当宽为奇数的时候,放中间,当宽为偶数的时候,放中间两格,还有哈,注释很重要,没注释看的很累人的。
回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-25 11:55:54 | 只看该作者
全局:
nprotect 发表于 2012-7-25 11:49
你这个代码就是有问题的,这题明显不太可能30几行就完成,题目下有明确说明需要符合各种条件,当宽为奇 ...

我的代码没有问题啊,题目要求是这样的:If the width is even, Karel may drop the beeper on either of the two center squares.他写的是either,也就是当宽为偶数的时候,我放在中间两格里面的任意一格均可。我运行了好几次,一点问题没有。恩,注释我会注意的,马上加上
回复

使用道具 举报

🔗
nprotect 2012-7-25 11:59:26 | 只看该作者
全局:
yiyiyaya 发表于 2012-7-25 11:55
我的代码没有问题啊,题目要求是这样的:If the width is even, Karel may drop the beeper on either of ...

恩,我理解错了,我以为是当偶数的时候,放中间两个格子,这样那应该不会那么烦 :)
回复

使用道具 举报

🔗
ericalingyuan 2012-7-26 15:29:02 | 只看该作者
全局:
视频是看完了,但是资料没完全看完。在eclipse中要导入的运行karel这个程序在哪里下载呢,官网好像没找到。另外教材你们都买了吗
回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-26 16:12:38 | 只看该作者
全局:
ericalingyuan 发表于 2012-7-26 15:29
视频是看完了,但是资料没完全看完。在eclipse中要导入的运行karel这个程序在哪里下载呢,官网好像没找到。 ...

导入assignment1文件夹就可以了。assignment1.rar在官网上有。教材我买了,觉得不错,和老师讲的很配套,建议买一本
回复

使用道具 举报

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

本版积分规则

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