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

Stanford CS106A 2#Assignment2讨论贴

 关闭
🔗
Brianlin 2013-5-3 22:29:19 | 只看该作者
全局:
zhuolinliu8 发表于 2012-7-30 13:38
FindRangeGraphicsHierarchy.java
Hailstone.java

FindRange中如果第一个输入的是0,最后的结果会是最小最到数都为0,但题目中
"If the user enters the sentinel on the very first input line, then no values have been
entered, and your program should display a message to that effect."
意思应该是这种情况需要显示出不同于
"If the user enters only one value before the sentinel, the program should report
that value as both the largest and smallest."的信息.
小弟认为在最后加上下面代码即可
                if (min == 0)println("No value input.");
                else{
                        println("smallest: " + min);
                        println("largest: " + max);
                }
回复

使用道具 举报

🔗
Brianlin 2013-5-3 22:41:03 | 只看该作者
全局:
nprotect 发表于 2012-7-30 22:20
No.1 Pyramid:

import acm.graphics.*;

不要看写的不整齐,其实您这个方法是最好的!啊,没有大米了~~
回复

使用道具 举报

🔗
Brianlin 2013-5-3 22:50:37 | 只看该作者
全局:
没有亮点...
  1. /*
  2. * File: FindRange.java
  3. * Name:
  4. * Section Leader:
  5. * --------------------
  6. * This file is the starter file for the FindRange problem.
  7. */

  8. import acm.program.*;

  9. public class FindRange extends ConsoleProgram {
  10.         private static final int SENTINEL=0;
  11.         public void run() {
  12.                 println("This program finds the largest and smallest numbers.");
  13.                 int input=readInt("?");
  14.                 int mini=input;
  15.                 int max=input;
  16.                 if(input==SENTINEL){
  17.                         println("There is not number input before you input SENTINEL.");
  18.                 }else{
  19.                         while (input!=SENTINEL){
  20.                                 if(input>max){
  21.                                         max=input;
  22.                                 }else if(input<mini){
  23.                                         mini=input;
  24.                                 }
  25.                                 input=readInt("?");
  26.                         }
  27.                 println("smallest: "+mini);
  28.                 println("largest: "+max);
  29.                 }
  30.         }

  31. }
复制代码
回复

使用道具 举报

🔗
Brianlin 2013-5-3 22:51:34 | 只看该作者
全局:
同样木有亮点的代码,一定要更加努力啊!
  1. /*
  2. * File: ProgramHierarchy.java
  3. * Name:
  4. * Section Leader:
  5. * ---------------------------
  6. * This file is the starter file for the ProgramHierarchy problem.
  7. * 注意,这道题的label高度一直有问题,所以添加了一条直线用于定位,以后有经验了再回来看下
  8. */

  9. import acm.graphics.*;
  10. import acm.program.*;
  11. import java.awt.*;

  12. public class ProgramHierarchy extends GraphicsProgram {       
  13.         private static final int RWIDTH=150;
  14.         private static final int RHEIGHT=50;       
  15.         private static final int VERTICAL_DISTANCE=35;
  16.         private static final int HORIZONTAL_DISTANCE=20;
  17.         public void run() {
  18.                 int Px=getWidth()/2-RWIDTH/2;
  19.                 int Py=getHeight()/2-RHEIGHT-VERTICAL_DISTANCE;
  20.                
  21.                 int Cx=getWidth()/2-RWIDTH/2;
  22.                 int Cy=getHeight()/2;
  23.                
  24.                 int Gx=getWidth()/2-RWIDTH/2-RWIDTH-HORIZONTAL_DISTANCE;
  25.                 int Gy=getHeight()/2;
  26.                
  27.                 int Dx=getWidth()/2+RWIDTH/2+HORIZONTAL_DISTANCE;
  28.                 int Dy=getHeight()/2;
  29.                
  30.                 int Point1x=getWidth()/2;
  31.                 int Point1y=Py+RHEIGHT;
  32.                 int Point2x=getWidth()/2-RWIDTH-HORIZONTAL_DISTANCE;
  33.                 int Point2y=Cy;
  34.                 int Point3x=getWidth()/2;
  35.                 int Point3y=Cy;
  36.                 int Point4x=getWidth()/2+RWIDTH+HORIZONTAL_DISTANCE;
  37.                 int Point4y=Cy;
  38.                
  39.                 add(new GLine(Point1x,Point1y,Point2x,Point2y));
  40.                 add(new GLine(Point1x,Point1y,Point3x,Point3y));
  41.                 add(new GLine(Point1x,Point1y,Point4x,Point4y));
  42.                
  43.                 add(new GRect(Px,Py,RWIDTH,RHEIGHT));
  44.                 add(new GRect(Cx,Cy,RWIDTH,RHEIGHT));
  45.                 add(new GRect(Gx,Gy,RWIDTH,RHEIGHT));
  46.                 add(new GRect(Dx,Dy,RWIDTH,RHEIGHT));

  47.                
  48.                 GLabel Label1=new GLabel("Program");
  49.                 int Label1x=Point1x-(int)(Label1.getWidth()/2);
  50.                 int Label1y=Point1y-RHEIGHT/2+(int)(Label1.getAscent()/2);
  51.                 add(Label1,Label1x,Label1y);
  52.                
  53.                 GLabel Label3=new GLabel("ConsoleProgram");
  54.                 int Label3x=Point1x-(int)(Label3.getWidth()/2);
  55.                 int Label3y=getHeight()/2+RHEIGHT/2+(int)(Label3.getAscent()/2);
  56.                 add(Label3,Label3x,Label3y);
  57.                 add(new GLine(0,getHeight()/2-RHEIGHT/2-VERTICAL_DISTANCE,1000,getHeight()/2-RHEIGHT/2-VERTICAL_DISTANCE));
  58.                 GLabel Label2=new GLabel("GraphicsProgram");
  59.                 int Label2x=Point2x-(int)(Label2.getWidth()/2);
  60.                 int Label2y=getHeight()/2+RHEIGHT/2+(int)(Label3.getAscent()/2);
  61.                 add(Label2,Label2x,Label2y);
  62.                
  63.                 GLabel Label4=new GLabel("DialogProgram");
  64.                 int Label4x=Point4x-(int)(Label4.getWidth()/2);
  65.                 int Label4y=getHeight()/2+RHEIGHT/2+(int)(Label3.getAscent()/2);
  66.                 add(Label4,Label4x,Label4y);
  67.         }
  68. }
复制代码
回复

使用道具 举报

🔗
Brianlin 2013-5-3 22:52:30 | 只看该作者
全局:
  1. /*
  2. * File: Target.java
  3. * Name:
  4. * Section Leader:
  5. * -----------------
  6. * This file is the starter file for the Target problem.
  7. */

  8. import acm.graphics.*;
  9. import acm.program.*;
  10. import java.awt.*;

  11.         public class Target extends GraphicsProgram{
  12.                 public void run(){
  13.                         double rCir1=72;
  14.                         double rCir2=rCir1*0.65;
  15.                         double rCir3=rCir1*0.3;
  16.                         double xCir1=getWidth()/2-rCir1;
  17.                         double yCir1=getHeight()/2-rCir1;
  18.                         double xCir2=getWidth()/2-rCir2;
  19.                         double yCir2=getHeight()/2-rCir2;
  20.                         double xCir3=getWidth()/2-rCir3;
  21.                         double yCir3=getHeight()/2-rCir3;
  22.                        
  23.                         GOval circle1=new GOval(xCir1,yCir1,2*rCir1,2*rCir1);
  24.                         circle1.setFilled(true);
  25.                         circle1.setColor(Color.red);
  26.                         add(circle1);
  27.                         GOval circle2=new GOval(xCir2,yCir2,2*rCir2,2*rCir2);
  28.                         circle2.setFilled(true);
  29.                         circle2.setColor(Color.white);
  30.                         add(circle2);
  31.                         GOval circle3=new GOval(xCir3,yCir3,2*rCir3,2*rCir3);
  32.                         circle3.setFilled(true);
  33.                         circle3.setColor(Color.red);
  34.                         add(circle3);
  35.                 }

  36.         }
复制代码
回复

使用道具 举报

全局:
  1. /*
  2. * File: Pyramid.java
  3. * Name:
  4. * Section Leader:
  5. * ------------------
  6. * This file is the starter file for the Pyramid problem.
  7. * It includes definitions of the constants that match the
  8. * sample run in the assignment, but you should make sure
  9. * that changing these values causes the generated display
  10. * to change accordingly.
  11. */

  12. import acm.graphics.*;
  13. import acm.program.*;
  14. import java.awt.*;

  15. public class Pyramid extends GraphicsProgram {

  16. /** Width of each brick in pixels */
  17.         private static final int BRICK_WIDTH = 30;

  18. /** Width of each brick in pixels */
  19.         private static final int BRICK_HEIGHT = 12;

  20. /** Number of bricks in the base of the pyramid */
  21.         private static final int BRICKS_IN_BASE = 14;
  22.        
  23.         public void run() {
  24.                 /* You fill this in. */
  25.                 for(int j=0; j<=BRICKS_IN_BASE-1; j++){
  26.                         for(int i=BRICKS_IN_BASE-1-j; i>=0; i--){
  27.                        
  28.                                 addBrick(getWidth()/2-(BRICK_WIDTH)*(BRICKS_IN_BASE-j)/2+(i)*BRICK_WIDTH,getHeight()-j*BRICK_HEIGHT);
  29.                         }
  30.                        
  31.                         }
  32.                
  33.                 }
  34.                
  35.        
  36.         private void addBrick(double cx, double cy){
  37.                 double x=cx-BRICK_WIDTH/2;
  38.                 double y=cy-BRICK_HEIGHT;
  39.                 GRect Brick =new GRect(x,y,BRICK_WIDTH,BRICK_HEIGHT);
  40.                 add(Brick);
  41.         }
  42.                
  43.                
  44.                
  45.         }
复制代码
第一个,至少花了半个小时在想怎么写i和j的表达式能使得每一行往右移还能保持是个金字塔形,最后写出来的东西估计明天自己都看不懂……智商捉急数体教。膜拜一下三楼
回复

使用道具 举报

全局:
第二题比较简单不放


第三题写了比预计时间长很久的时间,基础还是不牢固:
  1. /*
  2. * File: ProgramHierarchy.java
  3. * Name:
  4. * Section Leader:
  5. * ---------------------------
  6. * This file is the starter file for the ProgramHierarchy problem.
  7. */

  8. import acm.graphics.*;
  9. import acm.program.*;
  10. import java.awt.*;

  11. public class ProgramHierarchy extends GraphicsProgram {       
  12.         private static final double boxWidth=100;
  13.         private static final double boxHeight=30;
  14.        
  15.        
  16.         public void run() {
  17.                 /* You fill this in. */
  18.                 addFigure(getWidth()/2,getHeight()/2);
  19.                
  20.         }
  21.        
  22.         private void addFigure(double cx,double cy){
  23.                 GLine Line2 = new GLine(cx,cy+boxHeight/2,cx,cy-boxHeight/2);
  24.                 add(Line2);
  25.                

  26.                
  27.                 addBox(cx,cy-boxHeight);
  28.                 addLabel("Program",cx,cy-boxHeight);
  29.                
  30.                 addBox(cx,cy+boxHeight);
  31.                 addLabel("GraphicsProgram",cx,cy+boxHeight);
  32.                
  33.                
  34.                 double x = 4*boxWidth/3;
  35.                
  36.                 addBox(cx-x,cy+boxHeight);
  37.                 addLabel("ConsoleProgram",cx-x,cy+boxHeight);
  38.                
  39.                
  40.                 addBox(cx+x,cy+boxHeight);
  41.                 addLabel("DialogProgram",cx+x,cy+boxHeight);
  42.                
  43.                
  44.                 GLine Line1= new GLine(cx,cy-boxHeight/2,cx-x+boxWidth/2,cy+boxHeight/2);
  45.                 GLine Line3= new GLine(cx,cy-boxHeight/2,cx+x-boxWidth/2,cy+boxHeight/2);
  46.                 add(Line1);
  47.                 add(Line3);
  48.                
  49.         }
  50.        
  51.         private void addBox(double bx,double by){
  52.                 GRect box=new GRect(bx-boxWidth/2,by-boxHeight/2,boxWidth, boxHeight);
  53.                 add(box);
  54.         }
  55.         private void addLabel(String str, double lx,double ly){
  56.                 GLabel label=new GLabel(str);
  57.                 double x=label.getWidth()/2;
  58.                 double y=label.getAscent()/2;
  59.                 add(label,lx-x,ly+y);
  60.         }
  61.                
  62.                
  63.                
  64.                
  65.                
  66.                
  67.         }
  68.        

复制代码
回复

使用道具 举报

全局:
第五题想了好久,在自己写的乱七八糟的东西的基础上改了半天依然无法达到效果,最后一怒之下全部删掉画了个草稿没一会就写出来了……看来以后还是不能妄想傻盯着页面就能平白直接写出程序
  1. /*
  2. * File: FindRange.java
  3. * Name:
  4. * Section Leader:
  5. * --------------------
  6. * This file is the starter file for the FindRange problem.
  7. */

  8. import acm.program.*;

  9. public class FindRange extends ConsoleProgram {
  10.         public void run() {
  11.                 /* You fill this in */
  12.                 println("This program finds the largest and smallest numbers.");
  13.                 int a=readInt("?");  // get the first number
  14.                
  15.                 if (a!=0){
  16.                 int A1=a;   //assign maximum number
  17.                 int A2=a;   //assign minimum number
  18.                
  19.        
  20.                 while(true){
  21.                         int b=readInt("?");
  22.                         if (b!=0 && b>A1){   //find maximum, if the number given is greater than the first number, rewrite the maximum
  23.                                 A1=b;      
  24.                         }
  25.                         else if(b!=0 && b<A2){   // find minimum, if the number given is smaller than the first number, rewrite the minimum
  26.                                 A2=b;
  27.                         }
  28.                         else if(b ==0){     //stop if client assigns 0
  29.                                 break;
  30.                         }
  31.                        
  32.                 }
  33.                
  34.                 println("min:"+A1);
  35.                 println("max:"+A2);
  36.                 }
  37.        
  38.                 if (a==0){
  39.                         println("redo");  
  40.                 }
  41.         }
  42. }

复制代码
其实没太懂如果第一个数是0到底要干嘛……
回复

使用道具 举报

全局:
最后一题,还好不难
  1. /*
  2. * File: Hailstone.java
  3. * Name:
  4. * Section Leader:
  5. * --------------------
  6. * This file is the starter file for the Hailstone problem.
  7. */

  8. import acm.program.*;

  9. public class Hailstone extends ConsoleProgram {
  10.         public void run() {
  11.                 int a=readInt("Enter a number:");
  12.                 int i=0;
  13.                 while(a!=1){
  14.                        
  15.                         while(a%2==1){
  16.                                 int b=3*a+1;
  17.                                 println(a+"is odd, so I make 3n+1:"+b);
  18.                                 a=b;
  19.                                 i++;
  20.                         }
  21.                         while(a%2==0){
  22.                                 int b=a/2;
  23.                                 println(a+"is even, so I take half:"+b);
  24.                                 a=b;
  25.                                 i++;
  26.                         }
  27.                        
  28.                        
  29.                        
  30.                 }
  31.                 println("The process took " +i+ " to reach 1");
  32.                

  33.         }
  34. }

复制代码
虽然为什么用while(true)和break就会死循环实在不能理解,但是好歹算是写完了太不容易了…………
回复

使用道具 举报

🔗
sewen67 2013-8-1 23:14:34 | 只看该作者
全局:
/*
* File: PythagoreanTheorem.java
* Name:
* Section Leader:
* -----------------------------
* This file is the starter file for the PythagoreanTheorem problem.
*/

import acm.program.*;

public class PythagoreanTheorem extends ConsoleProgram {
        public void run() {
                println("Welcome to compute Pythagorean Theorem");
                int x = readInt ("a:");
                int y = readInt ("b:");
                double z = Math.sqrt(x*x+y*y);
                println ("c = "+z);
        }
       
}

/*
* File: Hailstone.java
* Name:
* Section Leader:
* --------------------
* This file is the starter file for the Hailstone problem.
*/

import acm.program.*;

public class Hailstone extends ConsoleProgram {
        public void run() {
                int x = readInt ("number:");
                int y=0;
                while (true){
                        if (x==1){
                                break;
                        }
                        if (x%2==0){
                                y=x;
                                x=x/2;
                                println (y+" is oven, so I take half: "+x);
                        }else{
                                y=x;
                                x=x*3+1;
                                println (y+" is odd, so make 3n+1: "+x);
                        }
                }
        }       
}

/*
* File: FindRange.java
* Name:
* Section Leader:
* --------------------
* This file is the starter file for the FindRange problem.
*/

import acm.program.*;


public class FindRange extends ConsoleProgram {
       
        public void run() {
                int x =readInt("?");
                if (x == 0) {
                        println ("The number you entered is 0");
                }
                int l=x;
                int s=x;
                while (x !=y){
                        x =readInt("?");
                        if (x==y) {
                                break;
                        } else if (x>l) {
                            l=x;
                    } else if (x<s){
                            s=x;
                    } else {
                           
                    }
                            
                }
                if (l !=s){
                        println ("The largest number is: "+l+".");
                        println ("The smallest number is: "+s+".");
                }else if (l==s){
                        println (s+" is both the largest and the smallest number.");
                }
        }
       
        private static int y=0;
}

/*
* File: Target.java
* Name:
* Section Leader:
* -----------------
* This file is the starter file for the Target problem.
*/

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class Target extends GraphicsProgram {       
        public void run() {
                double y= getHeight();
                double x= getWidth();
                GOval a = new GOval((x-144)/2,(y-144)/2,144,144);
                a.setColor(Color.red);
                a.setFilled(true);
                a.setFillColor (Color.red);
                add(a);
                GOval b = new GOval((x-100)/2,(y-100)/2,100,100);
                b.setColor(Color.white);
                b.setFilled(true);
                b.setFillColor (Color.white);
                add(b);
                GOval c = new GOval((x-44)/2,(y-44)/2,44,44);
                c.setColor(Color.red);
                c.setFilled(true);
                c.setFillColor (Color.red);
                add(c);
        }
}

/*
* File: ProgramHierarchy.java
* Name:
* Section Leader:
* ---------------------------
* This file is the starter file for the ProgramHierarchy problem.
*/

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class ProgramHierarchy extends GraphicsProgram {       
        private static final int height=50;
        private static final int length=100;
       
        public void run() {
                double x=getWidth();
                double y=getHeight();
                GRect a = new GRect ((x-length)/2,(y-3*height)/2,length,height);
                add(a);
                GRect b = new GRect ((x-length)/2,(y-3*height)/2+length,length,height);
                add(b);
                GRect c = new GRect ((x-length)/2-3*height,(y-3*height)/2+length,length,height);
                add(c);
                GRect d = new GRect ((x-length)/2+3*height,(y-3*height)/2+length,length,height);
                add(d);
                GLine ab = new GLine(x/2,(y-3*height)/2+height,x/2,y/2+height/2);
                add(ab);
                GLine ac = new GLine(x/2,(y-3*height)/2+height,x/2-length-height,y/2+height/2);
                add(ac);
                GLine ad = new GLine(x/2,(y-3*height)/2+height,x/2+length+height,y/2+height/2);
                add(ad);
                GLabel aa = new GLabel("Program");
                GLabel bb = new GLabel("GraphicsProgram");
                GLabel cc = new GLabel("ConsoleProgram");
                GLabel dd = new GLabel("DialogProgram");
                add(aa,(x-aa.getWidth())/2,y/2-50+aa.getHeight()/2);
                add(bb,(x-bb.getWidth())/2-3*bb.getWidth()/2,y/2-50+bb.getHeight()/2+2*height);
                add(cc,(x-cc.getWidth())/2,y/2-50+cc.getHeight()/2+2*height);
                add(dd,(x-dd.getWidth())/2+3*bb.getWidth()/2,y/2-50+dd.getHeight()/2+2*height);
               
        }
}

/*
* File: Pyramid.java
* Name:
* Section Leader:
* ------------------
* This file is the starter file for the Pyramid problem.
* It includes definitions of the constants that match the
* sample run in the assignment, but you should make sure
* that changing these values causes the generated display
* to change accordingly.
*/

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class Pyramid extends GraphicsProgram {

        /** Width of each brick in pixels */
        private static final int BRICK_WIDTH = 30;

        /** Width of each brick in pixels */
        private static final int BRICK_HEIGHT = 12;

        /** Number of bricks in the base of the pyramid */
        private static final int BRICKS_IN_BASE = 14;
       
        public void run() {
                int x = getWidth();
                int y = getHeight();
                for (int i=14;i>0;i--){
                        for (int j=0;j<i;j++){
                                GRect a = new GRect (x/2-7*30+15*(14-i)+j*30,y-12*(14-i),30,12);
                                add(a);
                        }
                }
        }
}
回复

使用道具 举报

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

本版积分规则

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