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

Stanford CS106A 2#Assignment2讨论贴

 关闭
🔗
 楼主| yiyiyaya 2012-8-12 13:18:40 | 只看该作者
全局:
jasonilove 发表于 2012-8-12 12:23
楼主好,刚刚贴了代码,请问要怎么获得学分啊?求加入~

不好意思啊,忘记给你加分了,已经加上了
回复

使用道具 举报

无效楼层,该帖已经被删除
🔗
melissami 2012-10-5 22:19:04 | 只看该作者
全局:
Part One: The Pythagorean Theorem
  1. /*
  2. * File: PythagoreanTheorem.java
  3. * Name:
  4. * Section Leader:
  5. * -----------------------------
  6. * This file is the starter file for the PythagoreanTheorem problem.
  7. */

  8. import acm.program.*;

  9. public class PythagoreanTheorem extends ConsoleProgram {
  10.         public void run() {
  11.                 printIn ("Enter values to compute the Pythagrean Thorem.");
  12.                 double a = readDouble("a:");
  13.                 double b = readDouble("b:");
  14.                 double a2 = a*a;
  15.                 double b2 = b*b;
  16.                 double c = Math.sqrt(a2+b2);
  17.                 printIn("c="+ c);
  18.         }
  19. }


复制代码
Part Two: Find Range,这个实在想不出,不知道如何能在要求用户输入后比较大小,然后循环此过程,所以参考了别人的
  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 SENITEL=0;
  11.         public void run() {
  12.                 println("This program finds the largest and smallest numbers.");
  13.                 int a=readInt("?");
  14.                 if(a==0){
  15.                         println("No value.");
  16.                         
  17.                 }
  18.                 int max=a;
  19.                 int min=a;

  20.                 while(true){
  21.                         a=readInt("?");
  22.                         if(a==SENITEL)break;
  23.                         
  24.                         if(a>max){
  25.                                 max=a;
  26.                         }
  27.                         if(a<min){
  28.                                 min=a;
  29.                         }
  30.                
  31.                 }
  32.                 println("smallest: "+min);
  33.                 println("largest: "+max);
  34. }
  35. }

复制代码

Part Four: Hailstone Sequence
  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 n= readInt("Enter a number:");
  12.                 int times = 0;
  13.                 while (n!=1){
  14.                 if (n%2==0){
  15.                         println(n+" is even so I take half: "+n/2);
  16.                         n=n/2;
  17.                         times++;}
  18.                         else {
  19.                                 println(n+" is odd, so I make 3n+1: "+(n*3+1));
  20.                                 n=n*3+1;
  21.                                 times++;
  22.                         }
  23.                 }
  24.                 println("The process took "+ times+"to reach 1");
  25.         }
  26. }
复制代码
Part Five: Artistry!
  1. /*
  2. * File: Artistry.java
  3. * Name:
  4. * Section Leader:
  5. * ==========================================================
  6. * Replace these comments with a description of your program.
  7. * Since this program is more freeform than the rest, tell us
  8. * a bit about it in these comments!
  9. */
  10. import java.awt.Color;

  11. import acm.program.*;
  12. import acm.graphics.*;

  13. public class Artistry extends GraphicsProgram {
  14.         public void run() {
  15.                 /* You fill this in.  Remember that you must have
  16.                  *
  17.                  * 1. At most twenty GObjects,
  18.                  * 2. At least one filled object,
  19.                  * 3. At least two different colors of objects, and
  20.                  * 4. At least three different types of objects (not
  21.                  *    counting the GLabel you use to sign your name).
  22.                  *
  23.                  * Also, be sure to sign your name in the bottom-right
  24.                  * corner of the canvas.
  25.                  */
  26.                 GRect rect1 = new GRect(50,50,150,30);
  27.                 rect1.setFillColor(Color.black);
  28.                 rect1.setFilled(true);
  29.                 add(rect1);
  30.                
  31.                 GRect rect2 = new GRect(250,50,150,30);
  32.                 rect2.setFillColor(Color.black);
  33.                 rect2.setFilled(true);
  34.                 add(rect2);
  35.                
  36.                 GRect rect3 = new GRect(200,200,50,30);
  37.                 rect3.setFillColor(Color.black);
  38.                 rect3.setFilled(true);
  39.                
  40.                
  41.                 GOval oval1 = new GOval(200,220,40,30);
  42.                 oval1.setFillColor(Color.cyan);
  43.                 oval1.setFilled(true);
  44.                 add(oval1);
  45.                
  46.                 GRect rect4 = new GRect(450,50,20,250);
  47.                 rect4.setFillColor(Color.black);
  48.                 rect4.setFilled(true);
  49.                 add(rect4);
  50.                
  51.                 GRect rect5 = new GRect(500,50,20,250);
  52.                 rect5.setFillColor(Color.black);
  53.                 rect5.setFilled(true);
  54.                 add(rect5);
  55.                
  56.                 GLabel label1 = new GLabel("AWKWARD", 550,300);
  57.             label1.setFont("SansSerif-36");
  58.             label1.setColor(Color.blue);
  59.             add(label1);
  60.                
  61.             GLabel label2 = new GLabel("Artistry by Julia", 630,450);
  62.             add(label2);
  63.             
  64.             
  65.             
  66.         }
  67. }
复制代码

Part Six: Target
  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.                 GOval ovalOut = new GOval((getWidth()-72)/2,(getHeight()-72)/2,72,72);
  14.                 ovalOut.setFillColor(Color.red);
  15.                 ovalOut.setFilled(true);
  16.                 add(ovalOut);
  17.                
  18.                 GOval ovalMid = new GOval((getWidth()-46.8)/2,(getHeight()-46.8)/2,46.8,46.8);
  19.                 ovalMid.setFillColor(Color.white);
  20.                 ovalMid.setFilled(true);
  21.                 add(ovalMid);
  22.                
  23.                 GOval ovalInter = new GOval((getWidth()-21.6)/2,(getHeight()-21.6)/2,21.6,21.6);
  24.                 ovalInter.setFillColor(Color.red);
  25.                 ovalInter.setFilled(true);
  26.                 add(ovalInter);
  27.         }
  28. }
复制代码

Part Seven: Pyramid双重循环这个想了好久,最终还是有借鉴他人的思路
  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.         /** Height 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.         for (int i=1; i<=BRICKS_IN_BASE; i++){
  25.             for (int n=1; n<=i; n++){                              
  26.             GRect rect = new GRect((getWidth()-BRICK_WIDTH*i)/2+BRICK_WIDTH*(n-1),getHeight()-BRICK_HEIGHT*(BRICKS_IN_BASE-i+1),BRICK_WIDTH,BRICK_HEIGHT);
  27.             add(rect);
  28.             }
  29.     }

  30.         }
  31.        

  32.        
  33. }


复制代码


回复

使用道具 举报

🔗
melissami 2012-10-5 22:26:27 | 只看该作者
全局:
Part Three: Fixing Broken Java
  1. /*
  2. * File: FixingBrokenJava.java
  3. * Name:
  4. * Section Leader:
  5. *
  6. * This program does not work as intended.  It contains both
  7. * compile-time errors (errors that prevent the compiler from even
  8. * running the program) and run-time errors (errors where the
  9. * program does not function as intended).  Your job is to fix
  10. * this program so that it works correctly.  Note that it is *not*
  11. * sufficient to simply fix the compiler errors; you will need to
  12. * update the logic as well.
  13. *
  14. * This program attempts to read a positive integer from the user,
  15. * then check whether that integer is prime (whether its only
  16. * divisors are 1 and itself).  If so, it prints a message saying
  17. * that the number is prime; otherwise it says that the number is
  18. * composite.
  19. */
  20. import acm.program.*;

  21. public class FixingBrokenJava extends ConsoleProgram {
  22.         /* Reads a number from the user and reports whether or not it
  23.          * is prime.
  24.          */
  25.         public void run() {
  26.                 /* Get the value from the user. */
  27.                 int value = readPositiveInt();
  28.                
  29.                 /* Check whether or not it is prime. */
  30.                 if (isPrime(value)) {
  31.                         println(value + " is prime.");
  32.                 } else {
  33.                         println(value + " is composite.");
  34.                 }
  35.         }
  36.        
  37.         /**
  38.          * Given a positive integer, returns whether that integer is
  39.          * prime.
  40.          *
  41.          * @param value The value to test.
  42.          * @return Whether or not it is prime.
  43.          */
  44.         private boolean isPrime(int value) {
  45.                 /* Try all possible divisors of the number.  If any of them
  46.                  * cleanly divide the number, we return that the number is
  47.                  * composite.
  48.                  */
  49.                 for (int divisor = 0; divisor <= value/2; divisor++) {
  50.                         if (value % divisor == 0) {
  51.                                 return false;
  52.                         }
  53.                         return true;
  54.                 }
  55.                
  56.         }
  57.        
  58.         /**
  59.          * Reads a positive integer from the user and returns it.
  60.          *
  61.          * @return A positive integer entered by the user.
  62.          */
  63.         private int readPositiveInt() {
  64.                 /* Get an initial value. */
  65.                 int value = readInt("Enter a positive integer: ");
  66.                 /* If the value was nonpositive, reprompt the user. */
  67.                 while (true) {
  68.                 if (value <= 0) {
  69.                         println("Please enter a positive integer.");
  70.                         int value1 = readInt("Enter a positive integer: ");
  71.                         return value1;
  72.                 }
  73.                 else {
  74.                         return value;
  75.                 }
  76.                 }
  77.                
  78.         }
  79. }
复制代码

这是一个修改原有java程序的题目,最终要实现的是:用户输入一个正整数,程序判断是否是质数,然后告知用户。里面涉及到很多定义新的method,譬如有返回值的method,判断正误,给出boolean值的method,虽然听过课,看过handout,但没找到老师讲授相关的内容。修改到最后,编译依然会出现以下错误:
This method must return a result of type boolean   

大家能不能帮忙看下哪里出了问题?O(∩_∩)O谢谢

还有就是我发现自己做的题目似乎和大家的不完全一样,我的是在http://www.stanford.edu/class/archive/cs/cs106a/cs106a.1124/handouts.html这个网站下载的,你们课程资源都是在哪里找的呢?

回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-10-6 03:14:22 | 只看该作者
全局:
melissami 发表于 2012-10-5 22:26
Part Three: Fixing Broken Java

这是一个修改原有java程序的题目,最终要实现的是:用户输入一个正整数 ...

http://see.stanford.edu/see/cour ... 8-a5aa-866adcae1111

点评

O(∩_∩)O谢谢,这个是07发布的,我找的那个网站是11年发布的,稍微有一点不同……  发表于 2012-10-6 16:36
回复

使用道具 举报

🔗
豆渣 2013-2-8 04:22:02 | 只看该作者
全局:
        public void run() {               
                int x = getWidth()/2;
                int y = getHeight()/2;               
                //it is helpful for calculation about each floor' x
                int n = 1;       
            /*
             * According to the brick's number in the base to set cycle number
             * They are same that the height floors of pyramid and the brick in base.
            */
            for (int i = 0 ; i < BRICKS_IN_BASE; i++){                   
                    //let floor is not overlapping.
                    y = y + BRICK_HEIGHT;            
                    //Each circle  
                    x = x - n * (BRICK_WIDTH / 2);                              
                    /*
                     * Adding a line of bricks from top to bottom.
                     * the top only had one brick. add a line same time add a brick, set the x.
                     * they are same that the number of bricks and the number of layers.                     
                     */
                    for ( int j=0 ; j < (i+1); j = j+1){
                            //let each brick be not overlapping
                            x = x + 30;
                        GRect BRICK = new GRect( x, y, BRICK_WIDTH, BRICK_HEIGHT );               
                        add(BRICK);                                   
                        }
                    n = n + 2;
                }       
        }
回复

使用道具 举报

全局:
在哪学的?
回复

使用道具 举报

🔗
豆渣 2013-2-9 22:34:28 | 只看该作者
全局:
月清天涯外 发表于 2013-2-8 21:38
在哪学的?

pps有视频。官网有笔记http://www.stanford.edu/class/cs106a/lecture.html
回复

使用道具 举报

🔗
豆渣 2013-2-10 18:19:49 | 只看该作者
全局:
  1. Part Two: Find Range
复制代码

回复

使用道具 举报

🔗
豆渣 2013-2-10 18:20:51 | 只看该作者
全局:
不可以编辑回复的帖子吗???
回复

使用道具 举报

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

本版积分规则

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