查看: 11581| 回复: 59
跳转到指定楼层
上一主题 下一主题
收起左侧

Stanford CS106A 2#Assignment2讨论贴

 关闭
全局:
本帖最后由 yiyiyaya 于 2012-7-27 16:47 编辑

时间:2012.07.27-2012.7.30

课程:
4. Classes and objects, The program class hierarchy
5. Variables, values and types, Arithmetic expressions
6. Control statements, Boolean expressions
7. Methods using parameters
8. (review parameters in class 7) The  Randomgenerator class

作业:
Assignment 2
due: 2012.7.30

建议:
作业一共有6个小题。其中有关画图的三个:1.Pyramid, 2.Target, 3.ProgramHierarchy
有关数字的有三题:4. PythagoreanTheorem 5. FindRange 6. Hailstone
个人感觉题目比较多,而且难度比assignment1大一些。可以先把题目看了,大致了解了之后再去听课。

欢迎大家积极讨论!



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

上一篇:MIT, Harvard, Berkeley终于也来了!新公开课平台Edx发布了6门课程!
下一篇:[Udacity CS 262] Programming Languages (Week #1)
🔗
ericalingyuan 2012-7-26 22:11:53 | 只看该作者
全局:
嗯,assignmen1还有些没完成,我得加油了
回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-26 22:26:41 | 只看该作者
全局:
ericalingyuan 发表于 2012-7-26 22:11
嗯,assignmen1还有些没完成,我得加油了

不着急,我只是先贴出来了而已
回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-26 23:11:20 | 只看该作者
全局:
No1.Pyramid.java
  1. import acm.graphics.*;
  2. import acm.program.*;

  3. public class Pyramid extends GraphicsProgram {
  4. /** Width of each brick in pixels */
  5.         private static final int BRICK_WIDTH = 30;
  6. /** Width of each brick in pixels */
  7.         private static final int BRICK_HEIGHT = 12;
  8. /** Number of bricks in the base of the pyramid */
  9.         private static final int BRICKS_IN_BASE = 14;
  10.        
  11.         public void run() {
  12.                 int x=getWidth();
  13.                 int y=getHeight();
  14.                 for (int j=1;j<=BRICKS_IN_BASE;j++){ //j表示金字塔的层数,从1到14
  15.                         for(int i=0;i<BRICKS_IN_BASE-j+1;i++){ //i表示每层金字塔的brick数目,例如,最底层i为从0-13,表示共有14块
  16.                                
  17.                             int x0=(x-(BRICKS_IN_BASE-j+1)*BRICK_WIDTH)/2;//每层第一块砖头开始的横坐标
  18.                             int y0=y-BRICK_HEIGHT*j;//每层第一块砖头开始的纵坐标
  19.                             
  20.                             int x1=i*BRICK_WIDTH+x0;//每层之后的每块砖头开始的横坐标
  21.                             
  22.                             GRect rect=new GRect(x1,y0,BRICK_WIDTH,BRICK_HEIGHT);
  23.                             add(rect);   
  24.                         }
  25.                 }
  26.         }       
  27. }
复制代码

评分

参与人数 2大米 +2 收起 理由
desmond49 + 1 给你点个赞!
stella_minxin + 1 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-26 23:17:21 | 只看该作者
全局:
No.2 Target.java

  1. import acm.graphics.*;
  2. import acm.program.*;
  3. import java.awt.*;

  4. public class Target extends GraphicsProgram {       
  5.         public void run() {
  6.                 int x0=getWidth()/2; // x0 = half of the width of the window
  7.                 int y0=getHeight()/2; // y0 = half of the height the window, so (x0,y0) is the center of the circle
  8.                 int rOuter=72; // the big circle's radius
  9.                 double rMiddle=72*0.65; // the middle circle's radius
  10.                 double rInner=72*0.3; // the inner circle's radius
  11.                
  12.                 add(filledCircle(x0,y0,rOuter,Color.RED));
  13.                 add(filledCircle(x0,y0,rMiddle, Color.WHITE));
  14.                 add(filledCircle(x0,y0,rInner, Color.RED));
  15.         }
  16.         private GOval filledCircle(double x,double y,double r,Color color){
  17.                        
  18.                 GOval filledCircle=new GOval(x-r,y-r,2*r,2*r);
  19.                 filledCircle.setFilled(true);
  20.                 filledCircle.setColor(color);
  21.                 return filledCircle;
  22.         }
  23. }
复制代码

评分

参与人数 2大米 +2 收起 理由
stella_minxin + 1 感谢分享!
Brianlin + 1 回答的很好!

查看全部评分

回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-27 15:56:07 | 只看该作者
全局:
shz117 发表于 2012-7-27 15:38
mark! 感谢楼主,本来都不想看了,这个活动又激起我的动力啊

谢谢,你们的支持也是我的动力,加油
回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-27 16:05:26 | 只看该作者
全局:
本帖最后由 yiyiyaya 于 2012-7-27 16:37 编辑

关于第三题,ProgramHierarchy中涉及到将label中的word居中的问题。要用到后面视频中讲到的知识:
label.getWidth()可以返回label的宽度;
但是关于label的高度就比较麻烦,因为一般来说,label是有个baseline的,比如说Program这个单词,baseline实际上是以roram为标准的,由于P字母上面突出一部分,而g字母下面又突出一部分,因此,单纯的label.getHeight()不能达到想将label居中的效果,因为居中的时候我们考虑的是它的上线,需要用到的函数为:
label.getAscent()  returns the distance the characters in the current font extend above the baseline
如果想看视频的话,可以参见第10课40分钟左右的位置

评分

参与人数 1大米 +1 收起 理由
stella_minxin + 1 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-27 16:37:52 | 只看该作者
全局:
本帖最后由 yiyiyaya 于 2012-7-27 16:39 编辑

No3 ProgramHierarchy

  1. import acm.graphics.*;
  2. import acm.program.*;


  3. public class ProgramHierarchy extends GraphicsProgram {        
  4.         private static final int rect_Width=150;
  5.         private static final int rect_Height=60;
  6.         private static final int spaceOffset=80;
  7.         //spaceoffset refers to the space between the first program rect and the other three rects
  8.         
  9.         public void run() {
  10.                 int x=getWidth();
  11.                 int y=getHeight();
  12.                 /*
  13.                  * draw rect1
  14.                  */
  15.                 int x0=(x-rect_Width)/2;
  16.                 int y0=(y-rect_Height*2-spaceOffset)/2;
  17.                 GRect rect1=new GRect(x0,y0,rect_Width,rect_Height);
  18.                 add(rect1);
  19.                 add(addLabel("Program",x0,y0));
  20.                 /*
  21.                  * draw the other three rects, and the space between these rects is set to be rect_Width as well
  22.                  */
  23.                 int x2= (x-rect_Width*4)/2;
  24.                 int y2= y0+rect_Height+spaceOffset;
  25.                 for(int i=0;i<3;i++)
  26.                 {
  27.                 double x3=x2+rect_Width*i*3/2;
  28.                 GRect rect2=new GRect(x3,y2,rect_Width,rect_Height);
  29.                 add(rect2);
  30.                 }
  31.                 add(addLabel("Graphics Program",x2,y2));
  32.                 add(addLabel("Console Program",x2+rect_Width*3/2,y2));        
  33.                 add(addLabel("Dialog Program",x2+rect_Width*3,y2));
  34.                 /*
  35.                  * draw the three lines
  36.                  */
  37.                 double xStart=x0+rect_Width/2;
  38.                 double yStart=y0+rect_Height;
  39.                 double yEnd=y2;
  40.                 for(int j=0;j<3;j++)
  41.                 {
  42.                         double xEnd=x2+rect_Width/2+rect_Width*3/2*j;
  43.                         GLine line=new GLine(xStart,yStart,xEnd,yEnd);
  44.                         add(line);
  45.                 }
  46.         }
  47.         /*
  48.          * set the label font and then move it to the center of the rect
  49.          */
  50.         private GLabel addLabel(String z,double x,double y){
  51.                 GLabel addLabel=new GLabel(z,x,y);
  52.                 addLabel.setFont("Sanserif-16");
  53.                 double addLabel_Width=addLabel.getWidth();
  54.                 double addLabel_Height=addLabel.getAscent();
  55.                 addLabel.move((rect_Width-addLabel_Width)/2,(rect_Height+addLabel_Height)/2);
  56.                 return addLabel;
  57.         }        
  58. }
复制代码

评分

参与人数 1大米 +1 收起 理由
stella_minxin + 1 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-27 16:40:20 | 只看该作者
全局:
No.4:
  1. import acm.program.*;
  2. import java.lang.Math;

  3. public class PythagoreanTheorem extends ConsoleProgram {
  4.         public void run() {
  5.                 println("Enter values to compute Pythagorean theorem");
  6.                 int a = readInt("a:");
  7.                 int b = readInt("b:");
  8.                 double a2= Math.pow(a, 2);
  9.                 double b2= Math.pow(b, 2);
  10.                 double c2=a2+b2;
  11.                 double c = Math.sqrt(c2);
  12.                 println("c="+c);
  13.         }
  14. }
复制代码


回复

使用道具 举报

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

本版积分规则

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