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

Stanford CS106A 2#Assignment2讨论贴

 关闭
🔗
zhuolinliu8 2012-7-30 13:38:37 | 只看该作者
全局:

复制代码
FindRange
  1. import acm.program.*;

  2. public class FindRange extends ConsoleProgram {

  3. public void run() {
  4. println("This program finds the largest and smallest numbers.");
  5. int min = readInt("?");
  6. int max = min;
  7. if (min != 0){
  8. int a = readInt("?");
  9. while(a != 0){
  10. if (a > max) max = a;
  11. else if (a < min) min = a;
  12. a = readInt("?");

  13. }

  14. }
  15. println("smallest: " + min);
  16. println("largest: " + max);
  17. }

  18. }
复制代码
GraphicsHierarchy.java
  1. import acm.program.*;
  2. import acm.graphics.*;

  3. public class GraphicsHierarchy extends GraphicsProgram {

  4. public void run() {
  5. int label_l = 120;
  6. int label_w = 40;
  7. int midoffset = 50;
  8. int recinterval = 30;
  9. int x = getWidth()/2;
  10. int y = getHeight()/2;

  11. GLabel lpro = new GLabel("Program", x,y-midoffset);
  12. lpro.move(-lpro.getWidth()/2,0);
  13. add(lpro);
  14. double xp = x-label_l/2;
  15. double yp = y-midoffset-lpro.getAscent()/2-label_w/2;
  16. add(new GRect(xp,yp,label_l,label_w));

  17. double yc = y + ( y - yp - label_w);
  18. add(new GRect(xp,yc,label_l,label_w));
  19. add(addLabel("ConsoleProgram",xp,yc,label_l,label_w));
  20. add(new GLine(x,yp+label_w,x,yc));

  21. double xg = xp - recinterval - label_l;
  22. add(new GRect(xg,yc,label_l,label_w));
  23. add(addLabel("GraphicProgram",xg,yc,label_l,label_w));
  24. add(new GLine(x,yp+label_w,xg+label_l/2,yc));

  25. double xd = xp+ label_l + recinterval;
  26. add(new GRect(xd,yc,label_l,label_w));
  27. add(addLabel("DialogProgram",xd,yc,label_l,label_w));
  28. add(new GLine(x,yp+label_w,xd+label_l/2,yc));
  29. }

  30. private GLabel addLabel(String st, double x,double y, double l, double w){
  31. GLabel label = new GLabel(st,x+l/2,y+w/2);
  32. label.move(-label.getWidth()/2,label.getAscent()/2);
  33. return label;
  34. }

  35. }
复制代码
Hailstone.java

  1. import acm.program.*;

  2. public class Hailstone extends ConsoleProgram {

  3. public void run() {
  4. int a = readInt("Enter a number: ");
  5. int step = 0;
  6. int temp;
  7. while(a != 1){
  8. step++;
  9. if(a%2 == 1){
  10. temp = 3*a + 1;
  11. println(a +" is odd, so I make 3n + 1:" + temp );
  12. a = temp;
  13. }else{
  14. temp = a / 2;
  15. println(a +" is even so I take half:" + temp );
  16. a = temp;
  17. }
  18. }
  19. println("The process took " + step + " to reach 1");
  20. }

  21. }
复制代码
Pyramid.java
  1. import acm.program.*;
  2. import acm.graphics.*;

  3. public class Pyramid extends GraphicsProgram {

  4. private static final int BRICK_WIDTH = 30;
  5. private static final int BRICK_HEIGHT = 12;
  6. private static final int BRICKS_IN_BASE = 12;

  7. public void run() {
  8. int x = getWidth()/2;
  9. int y = getHeight()- BRICK_HEIGHT;
  10. double xstart = x - (double)BRICKS_IN_BASE/2*BRICK_WIDTH;
  11. int step = BRICKS_IN_BASE;

  12. while( step >= 1){
  13. double k = xstart;
  14. for(int i = 1; i<= step; i++,k=k+BRICK_WIDTH)
  15. add(new GRect(k,y,BRICK_WIDTH,BRICK_HEIGHT));
  16. y -= BRICK_HEIGHT;
  17. xstart += BRICK_WIDTH / 2;
  18. step--;
  19. }
  20. }
  21. }
复制代码
Quadratic.java
  1. import acm.program.*;

  2. public class Quadratic extends ConsoleProgram {

  3. public void run() {
  4. println("Enter coefficients for the quadratic equation");
  5. int a = readInt("a:");
  6. int b = readInt("b:");
  7. int c = readInt("c:");
  8. double y = Math.sqrt(b*b-4*a*c);
  9. println("The first solution is: "+ 0.5*(y-b)/a);
  10. println("The second solution is: "+ 0.5*(-y-b)/a);
  11. }

  12. }
复制代码
Target
  1. import java.awt.Color;

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

  4. public class Rainbow extends GraphicsProgram {

  5. public void run() {
  6. int x = getWidth()/2;
  7. int y = getHeight()/2;

  8. add(createCircle(x,y,72,Color.RED));
  9. add(createCircle(x,y,72*0.65,Color.WHITE));
  10. add(createCircle(x,y,72*0.3,Color.RED));
  11. }

  12. private GOval createCircle(double x, double y, double r, Color c){
  13. GOval bigred = new GOval(x-r,y-r,2*r,2*r);
  14. bigred.setFilled(true);
  15. bigred.setFillColor(c);
  16. return bigred;
  17. }

  18. }
复制代码

评分

参与人数 2大米 +1 学分 +1 收起 理由
Brianlin + 1 学习学习
yiyiyaya + 1 代码写的很好!学习了

查看全部评分

回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-30 20:51:36 | 只看该作者
全局:
No.5 FindRange.java
看了ls的代码,觉得自愧不如啊,但是还是贴上来吧,哈哈,给大家举个反面例子

  1. import acm.program.*;

  2. public class FindRange extends ConsoleProgram {
  3.         private static final int SENTINEL=0;
  4.         public void run() {
  5.                 println("This program finds the largest and smallest numbers.");
  6.                 int val1=readInt("?");
  7.                 int smallest=val1;
  8.                 int largest=val1;
  9.                 if(val1==SENTINEL){
  10.                         println("Error:the first input number can not be zero.");
  11.                 }
  12.                 else {
  13.                         while(true){
  14.                                 int val2=readInt("?");
  15.                             if(val2!=SENTINEL) {
  16.                                     smallest=smallNum(val2,smallest);
  17.                                     largest=largeNum(val2,largest);
  18.                             }
  19.                             else {
  20.                                     break;
  21.                             }
  22.                         }
  23.                         println("smallest:"+smallest);
  24.                 println("largest:"+largest);
  25.                 }
  26.         }

  27.         private int smallNum(int n1,int n2){
  28.                 if(n1<n2) return n1;
  29.                 else      return n2;
  30.                
  31.         }
  32.         private int largeNum(int n1,int n2){
  33.                 if(n1>n2) return n1;
  34.                 else      return n2;
  35.         }
  36. }
复制代码

No.6 Hailstone.java
  1. import acm.program.*;

  2. public class Hailstone extends ConsoleProgram {
  3.         public void run() {
  4.                 int val=readInt("Enter a number:");
  5.                 int val1=val;
  6.                 while (true){       
  7.                     val1=iffOdd(val1);
  8.                     val1=iffEven(val1);
  9.                     if(val1==1){
  10.                         println("The process took "+val+" to reach 1");
  11.                         break;
  12.                     }
  13.                 }
  14.         }
  15.         private int iffOdd(int val){
  16.                 boolean p;
  17.                 p=(val%2==1);
  18.                 if(p){
  19.                         println(val+" is odd, so I make 3n + 1: "+(3*val+1));
  20.                         val=3*val+1;
  21.                         return val;
  22.                 }
  23.                 else return val;
  24.         }
  25.         private int iffEven(int val){
  26.                 boolean p;
  27.                 p=(val%2==1);
  28.                 if(!p){
  29.                         println(val+" is even, so I make half: "+(val/2));
  30.                         val=val/2;
  31.                         return val;
  32.                 }
  33.                 else return val;
  34.         }
  35. }
复制代码

回复

使用道具 举报

🔗
nprotect 2012-7-30 22:20:24 | 只看该作者
全局:
本帖最后由 nprotect 于 2012-7-30 22:45 编辑

No.1 Pyramid:

import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class Pyramid extends GraphicsProgram {
public void run() {   
     for (int i=14; i>0; i--){
      for (int j=i; j>0; j--){
       int x = (getWidth()-30*i)/2+30*(i-j);       //每行的x轴坐标
       int y = getHeight()-(15-i)*12;                //每行的y轴坐标
       GRect rect = new GRect (x, y, 30, 12);
       add(rect);
      }
     }
   }
private static final int BRICK_WIDTH = 30;       //方块宽度
private static final int BRICK_HEIGHT = 12;      //方块高度
private static final int BRICKS_IN_BASE = 14;  //基座方块数量
}

回复

使用道具 举报

🔗
nprotect 2012-7-30 22:21:38 | 只看该作者
全局:
本帖最后由 nprotect 于 2012-7-30 22:45 编辑

No.2  Target:

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

public class Target extends GraphicsProgram {        
        public void run() {
                int x = getWidth()/2;
                int y = getHeight()/2;
                add(createdFilledCirlce(x, y, 72, Color.RED));
                add(createdFilledCirlce(x, y, 47, Color.WHITE));
                add(createdFilledCirlce(x, y, 22, Color.RED));
        }        
        private GOval createdFilledCirlce (int x, int y, int r, Color color){
                GOval circle = new GOval (x-r, y-r, 2*r, 2*r);
                circle.setColor(color);
                circle.setFilled(true);
                return circle;
        }
}

回复

使用道具 举报

🔗
nprotect 2012-7-30 22:25:25 | 只看该作者
全局:
本帖最后由 nprotect 于 2012-7-30 22:44 编辑

No.3  ProgramHierarchy:

import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class ProgramHierarchy extends GraphicsProgram {
public void run() {
  add(createNewRect(getWidth()/2-FIGURE_WIDTH/2, (getHeight()-(FIGURE_HEIGHT)*2-l)/2, FIGURE_WIDTH, FIGURE_HEIGHT));//创立顶上的方框
  add(createNewRect(getWidth()/2-FIGURE_WIDTH/2, (getHeight()-(FIGURE_HEIGHT)*2-l)/2+l+FIGURE_HEIGHT, FIGURE_WIDTH, FIGURE_HEIGHT));//创立中间的方框
        add(createNewRect (getWidth()/2-FIGURE_WIDTH/2-k-FIGURE_WIDTH, (getHeight()-(FIGURE_HEIGHT)*2-l)/2+l+FIGURE_HEIGHT, FIGURE_WIDTH, FIGURE_HEIGHT));//创立左边方框
        add(createNewRect (getWidth()/2-FIGURE_WIDTH/2+k+FIGURE_WIDTH, (getHeight()-(FIGURE_HEIGHT)*2-l)/2+l+FIGURE_HEIGHT, FIGURE_WIDTH, FIGURE_HEIGHT));//创立右边方框   
        //至此,4个方框都在上面了
        add(createNewLine (getWidth()/2, (getHeight()-(FIGURE_HEIGHT)*2-l)/2+FIGURE_HEIGHT, getWidth()/2-FIGURE_WIDTH/2-k-FIGURE_WIDTH+FIGURE_WIDTH/2, (getHeight()-(FIGURE_HEIGHT)*2-l)/2+l+FIGURE_HEIGHT));//创立左边线条
        add(createNewLine (getWidth()/2, (getHeight()-(FIGURE_HEIGHT)*2-l)/2+FIGURE_HEIGHT, getWidth()/2, (getHeight()-(FIGURE_HEIGHT)*2-l)/2+l+FIGURE_HEIGHT));//创立中间的线条
        add(createNewLine (getWidth()/2, (getHeight()-(FIGURE_HEIGHT)*2-l)/2+FIGURE_HEIGHT, getWidth()/2+FIGURE_WIDTH+k, (getHeight()-(FIGURE_HEIGHT)*2-l)/2+l+FIGURE_HEIGHT));//创立右边线条   
        //至此,3条线都在上面了
        add(createNewLabel ("Program", getWidth()/2-FIGURE_WIDTH/2, (getHeight()-(FIGURE_HEIGHT)*2-l)/2));
        add(createNewLabel ("GraphicsProgram", getWidth()/2-FIGURE_WIDTH/2-k-FIGURE_WIDTH, (getHeight()-(FIGURE_HEIGHT)*2-l)/2+l+FIGURE_HEIGHT));
        add(createNewLabel ("ConsoleProgram", getWidth()/2-FIGURE_WIDTH/2, (getHeight()-(FIGURE_HEIGHT)*2-l)/2+l+FIGURE_HEIGHT));
        add(createNewLabel ("DialogProgram", getWidth()/2-FIGURE_WIDTH/2+k+FIGURE_WIDTH, (getHeight()-(FIGURE_HEIGHT)*2-l)/2+l+FIGURE_HEIGHT));
}   //至此,所有东西都上去了
private GRect createNewRect (int x, int y, int FIGURE_WIDTH, int FIGURE_HEIGHT){   //方法-创建方框
  GRect rectangular = new GRect (x, y, FIGURE_WIDTH, FIGURE_HEIGHT);
  return rectangular;
}
private GLabel createNewLabel (String B, int x, int y){                            //方法-创建字体
  GLabel label = new GLabel (B, x, y);
  label.setFont("Sanserif-16");
  double LABEL_WIDTH = label.getWidth();
  double LABEL_HEIGHT = label.getAscent();
  label.move((FIGURE_WIDTH-LABEL_WIDTH)/2, (FIGURE_HEIGHT-LABEL_HEIGHT)/2+LABEL_HEIGHT);
  return label;    //注意此类中.move的用法,                     
}
private GLine createNewLine (int x, int y, int x1, int y1){                        //方法-创建线条
  GLine line = new GLine (x, y, x1, y1);
  return line;
}
private static final int FIGURE_WIDTH = 160;  //方框长
private static final int FIGURE_HEIGHT = 40;  //方框宽
private static final int k = 20; //左边和中间方框 & 右边和中间方框 之间的距离
private static final int l = 40; //topRect和middleRect之间线的距离
}
回复

使用道具 举报

🔗
nprotect 2012-7-30 22:32:24 | 只看该作者
全局:
本帖最后由 nprotect 于 2012-7-30 23:01 编辑

No.4 PythagoreanTheorem:

import acm.program.*;
public class PythagoreanTheorem extends ConsoleProgram {
public void run() {
println("Enter values to compute Pythagorean theorem");
int a = readInt("a: ");
int b = readInt("b: ");
double c = Math.sqrt(a*a+b*b);
println("c = "+c);
}
}

回复

使用道具 举报

🔗
nprotect 2012-7-30 22:57:40 | 只看该作者
全局:
本帖最后由 nprotect 于 2012-7-30 23:02 编辑

还有两题明天贴来,第三题写的稍微烦了一点,我估计参数设置的有点不合理。问一下。。。你们带行数的代码是怎么贴上来的?

回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-31 16:54:24 | 只看该作者
全局:
有一个<>形状的东东,点开里面就可以贴代码
回复

使用道具 举报

🔗
nprotect 2012-7-31 19:52:37 | 只看该作者
全局:
本帖最后由 nprotect 于 2012-7-31 19:59 编辑

No. 5 FindRange:

import acm.program.*;
import acm.graphics.*;
import java.lang.Math;

public class FindRange extends ConsoleProgram {
public void run() {
  println ("This program finds the largest and smallest numbers.");
   int inputOne = readInt("? ");
   if (inputOne == SENTINEL){
    println("no values have been entered");
   }
   else {             //现在inputOne中储存了刚读入的一个不为0的数字
    int inputTwo = readInt ("? ");   //读入的第二个数字储存在inputTwo中
    if (inputTwo == SENTINEL){
     println (inputOne + " as both the largest and smallest value");
    }
    else{          //现在inputOne与inputTwo都储存了不为0的两个数
     while (true){         //循环主体开始
      inputOne = Math.max(inputOne, inputTwo); //inputOne中存大的
      inputTwo = Math.min(inputOne, inputTwo); //inputTwo中存小的
      int inputThree = readInt("? ");          //读入第三个数字
      if (inputThree == SENTINEL) break;
      boolean n = ((inputOne>inputThree)||(inputOne>inputTwo));
      if (n == false){
       int temp = inputOne; //最小值
       inputOne = Math.max(inputTwo, inputThree); //最大值
       inputTwo = temp;
      }
      else {
       int temp = Math.min(inputThree, inputTwo);  //最小值           
             int temp2 = Math.max(inputThree, inputTwo);
             inputOne = Math.max(temp2, inputOne);      //最大值
              inputTwo = temp;
      }                                               //结束  
     }
     println ("smallest: " + inputTwo);
     println ("largest: " + inputOne);
    }
   }
}
private static final int SENTINEL = 0;
}

回复

使用道具 举报

🔗
nprotect 2012-7-31 19:55:13 | 只看该作者
全局:
本帖最后由 nprotect 于 2012-7-31 20:00 编辑

No.6 HailStone:

import acm.program.*;
public class Hailstone extends ConsoleProgram {
public void run() {
  int val = readInt("Enter a number: ");
     int counter = 0;
   while (val != 1){        //val不等于1
                counter++;           //计数器 ,每结束一个while循环,加1  
    if (val % 2 == 1){   //奇数时
     int x = val*3+1;
     println (val + " is odd, so I make 3n+1: " + x);
        val = x;         //x的值重新赋给val,跳出if循环
    }
    else {
     int x = val/2;   //偶数时
     println(val + " is even, so i take half: " + x);
     val = x;         //x的值重新赋给val,跳出if循环
    }
   }
   println("The process took " + counter + " steps to reach 1");
}
}
//while嵌套if+else,用于双向选择的循环中,注意在If/else循环中的x变量,本题重点
//计数循环步骤,while循环主体下面加一个counter即可

回复

使用道具 举报

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

本版积分规则

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