cs106a编程方法学作业解答(2)

 

第二次作业

problem #1

第一题要求做一个由相同大小矩形砖块搭建而成的金字塔。砖块尺寸和底层砖块数由用户指定。要求是金字塔必须坐落在屏幕底部中央,而且每一层比下一层恰少一块砖。做出来应该是这个效果:

我的想法是利用两个for循环嵌套在一起实现。代码如下:

/*
* File: Pyramid.java
* ------------------
* This program is a stub for the Pyramid problem, which draws
* a brick pyramid.
*/

import acm.program.*;
import acm.graphics.*;

public class Pyramid extends GraphicsProgram {

            public void run() {
                     int brickWidth;
                     int brickHeight;
                     int bricksInBase;
                     int windowWidth=getWidth();//获取窗口宽度

 

                     int windowHeight=getHeight();//获取窗口高度
                     println("This program will draw a pyramid of bricks in a window");
                     brickWidth=readInt("Enter brickWidth:");
                     brickHeight=readInt("Enter brickHeight:");
                     bricksInBase=readInt("Enter bricksInBase:");
                     for(int i=0;i<bricksInBase;i++){        //从底部开始绘制第i+1行
                                  int r=bricksInBase-i;           //r表示第i+1行的砖块数
                                  int y=windowHeight-(i+1)*brickHeight; //确定第i+1行砖块的纵坐标位置

                                  for(int j=0;j<r;j++){          //绘制第i+1行第j+1块砖块
                                            int x=(windowWidth-(bricksInBase-i)*brickWidth)/2+j*brickWidth;  //确定这一块的横坐标位置
                                            GRect Brick= new GRect(x,y,brickWidth,brickHeight);
                                            add(Brick);
                                  }

                     }

          }

}

problem #2

第二题要我们画出如下图所示的彩虹:

要求是,彩虹不能碰到窗口底部。彩虹的顶点必须在窗口中可见。

题目实现起来非常直白,因此有一点儿乏味:

/*
* File: Rainbow.java
* ------------------
* This program is a stub for the Rainbow problem, which displays
* a rainbow by adding consecutively smaller circles to the canvas.
*/

import java.awt.Color;

import acm.program.*;
import acm.graphics.*;


public class Rainbow extends GraphicsProgram {

               public void run() {
                        int windowWidth=getWidth();
                        int windowHeight=getHeight();
                        GRect sky = new GRect(0,0,windowWidth,windowHeight);
                        sky.setColor(Color.CYAN);
                        sky.setFilled(true);
                        sky.setFillColor(Color.CYAN);
                        add(sky);
                        GOval circle1= new GOval(-143,27,1040,1040);
                        circle1.setFilled(true);
                        circle1.setFillColor(Color.RED);
                        add(circle1);
                        GOval circle2= new GOval(-123,47,1000,1000);
                        circle2.setFilled(true);
                        circle2.setFillColor(Color.ORANGE);
                        add(circle2);
                        GOval circle3= new GOval(-103,67,960,960);
                        circle3.setFilled(true);
                        circle3.setFillColor(Color.YELLOW);
                        add(circle3);
                        GOval circle4= new GOval(-83,87,920,920);
                        circle4.setFilled(true);
                        circle4.setFillColor(Color.GREEN);
                        add(circle4);
                        GOval circle5= new GOval(-63,107,880,880);
                        circle5.setFilled(true);
                        circle5.setFillColor(Color.BLUE);
                        add(circle5);
                        GOval circle6= new GOval(-43,127,840,840);
                        circle6.setFilled(true);
                        circle6.setFillColor(Color.MAGENTA);
                        add(circle6);
                        GOval circle7= new GOval(-23,147,800,800);
                        circle7.setFilled(true);
                        circle7.setFillColor(Color.CYAN);
                        add(circle7);


            }

}

problem #3

如图,要求做一个这样的带文本的图。要求是整个图在窗口中居中。文本在各自的方框中居中。也是一道没啥好说的题,比较无趣。

/*
* File: GraphicsHierarchy.java
* ----------------------------
* This program is a stub for the GraphicsHierarchy problem, which
* draws a partial diagram of the acm.graphics hierarchy.
*/

import acm.program.*;
import acm.graphics.*;

public class GraphicsHierarchy extends GraphicsProgram {

              public void run() {
                     int windowWidth=getWidth();
                     int windowHeight=getHeight();
                     int boxWidth=readInt("Enter class box width:");
                     int boxHeight=readInt("Enter class box height:");
                     int x1,x2,x3,x4,x5,y1,y2;
                     x1=windowWidth/2;
                     y1=windowHeight/3;
                     y2=windowHeight*2/3;
                     x2=windowWidth/8;
                     x3=windowWidth*3/8;
                     x4=windowWidth*5/8;
                     x5=windowWidth*7/8;
                     GLine line1=new GLine(x1,y1,x2,y2);
                     add(line1);
                     GLine line2=new GLine(x1,y1,x3,y2);
                     add(line2);
                     GLine line3=new GLine(x1,y1,x4,y2);
                     add(line3);
                     GLine line4=new GLine(x1,y1,x5,y2);
                     add(line4);
                     GRect box1=new GRect(x1-boxWidth/2,y1-boxHeight,boxWidth,boxHeight);
                     add(box1);
                     GRect box2=new GRect(x2-boxWidth/2,y2,boxWidth,boxHeight);
                     add(box2);
                     GRect box3=new GRect(x3-boxWidth/2,y2,boxWidth,boxHeight);
                     add(box3);
                     GRect box4=new GRect(x4-boxWidth/2,y2,boxWidth,boxHeight);
                     add(box4);
                     GRect box5=new GRect(x5-boxWidth/2,y2,boxWidth,boxHeight);
                     add(box5);
                     GLabel label1=new GLabel("GObject",x1,y1);
                     int a1=(int)label1.getWidth();
                     int b1=(int)label1.getAscent();
                     label1.move(-a1/2,-boxHeight/2+b1/2);
                     add(label1);
                     GLabel label2=new GLabel("GLabel",x2,y2);
                     int a2=(int)label2.getWidth();
                     int b2=(int)label2.getAscent();
                     label2.move(-a2/2,boxHeight/2+b2/2);
                     add(label2);
                     GLabel label3=new GLabel("GLine",x3,y2);
                     int a3=(int)label3.getWidth();
                     int b3=(int)label3.getAscent();
                     label3.move(-a3/2,boxHeight/2+b3/2);
                     add(label3);
                     GLabel label4=new GLabel("GOval",x4,y2);
                     int a4=(int)label4.getWidth();
                     int b4=(int)label4.getAscent();
                     label4.move(-a4/2,boxHeight/2+b4/2);
                     add(label4);
                     GLabel label5=new GLabel("GRect",x5,y2);
                     int a5=(int)label5.getWidth();
                     int b5=(int)label5.getAscent();
                     label5.move(-a5/2,boxHeight/2+b5/2);
                     add(label5);

        }

}

problem #4

此题要求用户输入一元二次方程的系数a b c 然后给出解。若无实根,则提示用户。

/*
* File: Quadratic.java
* --------------------
* This program is a stub for the Quadratic problem, which finds the
* roots of the quadratic equation.
*/

import acm.program.*;

public class Quadratic extends ConsoleProgram {

                public void run() {
                         println("Enter coefficients for the quadratic equation:");
                         double a=readInt("a:");
                         double b=readInt("b:");
                         double c=readInt("c:");
                         double x=b*b-4*a*c;
                         if(x<0){
                                   println("The equation has no real solutions!");
                        }
                        else{
                                   double y=Math.sqrt(x);
                                   double p=(-b+y)*0.5/a;
                                   double q=(-b-y)*0.5/a;
                                   println("The first solution is "+p);
                                   println("The second solution is "+q);

                        }
               }

}

problem #5

提示用户输入一串正整数,从中找出最大的和最小的,并使用用户指定的数作为输入完毕的标示。

/*
* File: FindRange.java
* --------------------
* This program is a stub for the FindRange problem, which finds the
* smallest and largest values in a list of integers.
*/

import acm.program.*;

public class FindRange extends ConsoleProgram {

          public void run() {
                  int sentinel=readInt("Enter a integer as sentinel:");
                  println("This program finds the smallest and largest integers in a list. Enter values,one per line,using a "+sentinel+"to signal the end of the list");
                  int a=readInt("?");
                  int largest=0;
                  int smallest=1000000;
                  if(a==sentinel){
                           println("No values have been entered!");
                  }
                  else{
                           while (a!=sentinel){
                                   if(a>largest){
                                          largest=a;
                                   }
                                   if(a<smallest){
                                          smallest=a;
                                   }
                                   a=readInt("?");
                           }
                           println("The smallest value is "+smallest);
                           println("The largest value is "+largest);
                 }
         }
}

problem #6

最后一题是角谷猜想的一个简易验算的程序。提示用户输入一个正整数,要求显示出验算步骤和步骤数。

/*
* File: Hailstone.java
* --------------------
* This program is a stub for the Hailstone problem, which computes
* Hailstone sequence described in Assignment #2.
*/

import acm.program.*;

public class Hailstone extends ConsoleProgram {

             public void run() {
                     println("This program computes Hailstone sequences.");
                     int a=readInt("Enter a number:");
                     int count=0;
                     while (a!=1){
                                if(a%2==0){
                                      a=a/2;
                                      println(2*a+" is even,so I take half = "+a);
                                      count=count+1;
                                }
                                else{
                                      a=3*a+1;
                                      println((a-1)/3+" is odd,so I take 3n+1 ="+a);
                                      count=count+1;
                                }
                      }
                      println("The process took "+count+" steps to reach 1");
             }

}

 

posted on 2014-09-29 15:41  livingisgood  阅读(625)  评论(0编辑  收藏  举报