【笔记】Stanford OpenCourse—CS106A:Programming Methodology—006

Assignment 2: Simple Java Programs

Problem 1: Brick Pyramids

    这题要求画个图1所示的类似金字塔的东东,主要是使用getWidth(),和getHeitht().

image

                                                              图 1

1 /*File pyramid.java
2 * this program draws a pyramid
3 * Programming exercise 1 cs106a stanford:
4 * Assignment #2: Simple Java Programs
5 * Write a GraphicsProgram subclass that draws a pyramid consisting of bricks
6 arranged in horizontal rows, so that the number of bricks in each row decreases by
7 one as you move up the pyramid
8 The pyramid should be centered at the bottom of the window and should use
9 constants for the following parameters:
10 BRICK_WIDTH The width of each brick (30 pixels)
11 BRICK_HEIGHT The height of each brick (12 pixels)
12 BRICKS_IN_BASE The number of bricks in the base (14)
13 The numbers in parentheses show the values for this diagram, but you must be able
14 to change those values in your program.
15 */
16
17  import acm.program.*;
18  import acm.graphics.*;
19
20
21  public class Pyramid extends GraphicsProgram {
22
23 private static final int BRICK_WIDTH = 30; //The width of each brick (30 pixels)
24   private static final int BRICK_HEIGHT = 12; //The height of each brick (12 pixels)
25   private static final int BRICKS_IN_BASE = 14; //The number of bricks in the base (14)
26  
27
28 public void run() {
29 drawPyramid();
30 }
31
32 private void drawBrick(double x, double y) {
33 GRect brick = new GRect( x, y, BRICK_WIDTH,BRICK_HEIGHT);;
34 add (brick);
35
36 }
37
38 private void drawRowOfBricks(double x, double y, int n){
39 for (int i=0; i<n; i++){
40
41 drawBrick(x + i*BRICK_WIDTH,y);
42 }
43 }
44
45 private void drawCenteredRowOfBricks(double y, int n) {
46 double x = getWidth()/2 - n*BRICK_WIDTH/2;
47 drawRowOfBricks(x, y, n);
48 }
49
50 private void drawPyramid(){
51 double y = getHeight();
52 for (int i=BRICKS_IN_BASE; i>0; i--) {
53 y -= BRICK_HEIGHT;
54 drawCenteredRowOfBricks(y, i);
55 }
56 }
57 }

Proble 2:Rainbow

    这题要画个图2所示的彩虹。

image

                                                          图 2

1 /** File: Rainbow.java
2 * ------------------
3 * This program is a stub for the Rainbow problem, which displays
4 * a rainbow by adding consecutively smaller circles to the canvas.
5 */
6
7  import acm.program.*;
8  import acm.graphics.*;
9  import java.awt.*;
10
11  public class Rainbow extends GraphicsProgram {
12
13 public void run() {
14 drawSky();
15 drawRainbow();
16 }
17
18 private void drawSky() {
19 int skyWidth = getWidth();
20 int skyHeight = getHeight();
21 GRect sky = new GRect(0, 0, skyWidth, skyHeight);
22 sky.setColor(Color.CYAN);
23 sky.setFillColor(Color.CYAN);
24 sky.setFilled(true);
25 add(sky);
26 }
27
28 private void drawRainbow() {
29 Color bandColor = Color.RED;
30 for (int i = 0; i < 7; i++) {
31 switch (i) {
32 case 1: bandColor = Color.ORANGE; break;
33 case 2: bandColor = Color.YELLOW; break;
34 case 3: bandColor = Color.GREEN; break;
35 case 4: bandColor = Color.BLUE; break;
36 case 5: bandColor = Color.MAGENTA; break;
37 case 6: bandColor = Color.CYAN; break;
38 }
39
40 double offset = 20 * i;
41 double bandDiameter = 1.5*getWidth() - (offset * 2);
42 double bandX = (getWidth() - bandDiameter)/2;
43 double bandY = getHeight()/10 + offset;
44
45 GOval band = new GOval(bandX, bandY, bandDiameter, bandDiameter);
46 band.setColor(bandColor);
47 band.setFillColor(bandColor);
48 band.setFilled(true);
49 add(band);
50 }
51 }
52 }

Problem 3:Graphics Hierarchy

    如图3,画个结构图。

image

                                                             图 3

1 /*
2 * File: GraphicsHierarchy.java
3 * ----------------------------
4 * This program is a stub for the GraphicsHierarchy problem, which
5 * draws a partial diagram of the acm.graphics hierarchy.
6 */
7
8  import acm.program.*;
9 import acm.graphics.*;
10
11 public class GraphicsHierarchy extends GraphicsProgram {
12 private static final double BOX_HEIGHT = 50;
13 private static final double BOX_WIDTH = 100;
14
15 public void run() {
16 drawRowOne();
17 drawRowTwo();
18 }
19
20 private void drawRowOne() {
21 double centerX = getWidth()/2;
22 double centerY = getHeight()/4;
23
24 drawBox(centerX, centerY, "GObject");
25 }
26
27 private void drawRowTwo() {
28 String boxType = "GLabel";
29 for (int i = 1; i < 5; i++) {
30 double centerY = getHeight() * 2/3;
31 double centerX = getWidth()/5 * i;
32
33 switch (i) {
34 case 1: break;
35 case 2: boxType = "GLine"; break;
36 case 3: boxType = "GOval"; break;
37 case 4: boxType = "GRect"; break;
38 }
39
40 drawBox(centerX, centerY, boxType);
41 drawLineToRowOne(centerX, centerY);
42 }
43 }
44
45 private void drawBox(double centerX, double centerY, String boxType) {
46 double boxX = centerX - BOX_WIDTH/2;
47 double boxY = centerY - BOX_HEIGHT/2;
48 GRect box = new GRect(boxX, boxY, BOX_WIDTH, BOX_HEIGHT);
49 add(box);
50
51 GLabel label = new GLabel(boxType);
52 label.setFont("Palatino-Bold-12");
53 double labelX = centerX - label.getWidth()/2;
54 double labelY = centerY + label.getAscent()/2;
55 add(label, labelX, labelY);
56
57 }
58
59 private void drawLineToRowOne(double centerX, double centerY) {
60 // the middle point of base line boxes
61 double x0 = centerX;
62 double y0 = centerY - BOX_HEIGHT/2;
63 // the middle point of top line
64 double x1 = getWidth()/2;
65 double y1 = getHeight()/4 + BOX_HEIGHT/2;
66 GLine line = new GLine(x0, y0, x1, y1);
67 add(line);
68 }
69
70 }

Problem :4:Quadratic Formula

    一元二次方程求根公式。

1 /*
2 * File: Quadratic.java
3 * --------------------
4 * This program is a stub for the Quadratic problem, which finds the
5 * roots of the quadratic equation.
6 */
7
8 import acm.program.*;
9
10 public class Quadratic extends ConsoleProgram {
11
12 public void run() {
13 println("Enter coefficients for the quadratic equation:");
14 double a = readDouble("a: ");
15 double b = readDouble("b: ");
16 double c = readDouble("c: ");
17
18 double delta = Math.sqrt(b*b - 4*a*c);
19 if (delta >= 0){
20 println("The first solution is " + (-b + delta)/(2*a));
21 println("The first solution is " + (-b - delta)/(2*a));
22 }else {
23 println("The equation has no real solutions.");
24 }
25 }
26
27 }

Problem 5:Determining the Range

    排排坐,输入一系列数,选出最大的和最小的。这里用了2中方法实现。

1 /*
2 * File: FindRange.java
3 * --------------------
4 * This program is a stub for the FindRange problem, which finds the
5 * smallest and largest values in a list of integers.
6 */
7
8 import acm.program.*;
9
10 /*
11 public class FindRange extends ConsoleProgram {
12
13 public static final int SENTINEL = 0;
14
15 public void run() {
16 println ("This program finds the largest and smallest numbers.");
17
18 int largestNumber = Integer.MIN_VALUE;
19 int smallestNumber = Integer.MAX_VALUE;
20
21 while (true) {
22 int x = readInt("? ");
23 if (x == SENTINEL) break;
24
25 if (x > largestNumber) largestNumber = x;
26 if (x < smallestNumber) smallestNumber = x;
27 }
28
29 if (largestNumber >= smallestNumber) {
30 println("smallest: " + smallestNumber);
31 println("largest: " + largestNumber);
32 } else {
33 println("No values given.");
34 }
35 }
36
37 }
38 */
39
40 public class FindRange extends ConsoleProgram{
41
42 /* Specifies the value of the sentinel */
43 private static final int SENTINEL = 0;
44
45 public void run(){
46 intro();
47 enterValue();
48 }
49
50
51 private void intro() {
52 println("This program determines the maximum and minimum values.");
53 println("Enter values, one per line, using " + SENTINEL);
54 println("to signal the end of the list.");
55 }
56
57 private void enterValue() {
58 int val = readInt(" ? ");
59 if(val == SENTINEL) println("No values were entrerd");
60 if (val != SENTINEL) enterNextValue (val);
61 }
62
63 private void enterNextValue(int val) {
64 int min=val;
65 int max=val;
66 while(true){
67 val = readInt(" ? ");
68 if(val == SENTINEL)break;
69 max=max(val,max);
70 min=min (val,min);
71 }
72 printResults(max, min);
73 }
74
75 private void printResults(int max, int min) {
76 println("The Maximum is " + max + ".");
77 println("The Minimum is "+ min +".");
78
79 }
80
81 private int max(int val, int max) {
82 if(val>max)max=val;
83 return max;
84 }
85
86 private int min(int val, int min) {
87 if (val<min)min=val;
88 return min;
89 }
90 }

Problem 6:Hailstones

    这题名字挺怪:冰雹。基本没见过,说是牛人牛书里的不论给定一个数是奇或偶,最后总能折腾到1.(未证明)

1 /*
2 * File: Hailstone.java
3 * --------------------
4 * This program is a stub for the Hailstone problem, which computes
5 * Hailstone sequence described in Assignment #2.
6 */
7
8 import acm.program.*;
9
10 public class Hailstone extends ConsoleProgram {
11
12 public void run() {
13 int n = readInt("Enter a number: ");
14 int i = 0;
15 while ( n != 1) {
16 if (n%2 == 0){
17 println(n + " is even, so I take half = " + n/2);
18 n = n/2;
19 }
20 else {
21 println(n + " is odd, so I take 3n+1 = " + (3*n+1));
22 n = 3*n +1;
23 }
24 i++;
25 }
26 println("The porcess took " + i + " steps to reach 1.");
27 }
28
29 }

原文地址:https://www.cnblogs.com/halflife/p/2083663.html