• 【笔记】Stanford OpenCourse—CS106A:Programming Methodology—005


    ch04 Programming Exercise

    1.

    1 /**
    2 * File: Ex4_1.java
    3 * ----------------
    4 * This program is to generate the lyrics .
    5 *
    6 * @author Administrator
    7 *
    8 */
    9  import acm.program.*;
    10
    11  public class Ex4_1 extends ConsoleProgram {
    12
    13 public void run() {
    14
    15 for (int i=n; i>0; i--){
    16 println(i + " bottles of beer on the wall.");
    17 println(i + " bottles of beer.");
    18 println("You take one down, pass it around.");
    19 println(i-1 + " bottles of beer on the wall.");
    20 println();
    21 }
    22
    23 }
    24
    25 private static final int n = 99;
    26
    27
    28 }

    2.

    1 /**
    2 * File: Ex4_2.java
    3 * ----------------
    4 * This program also write a silly song.
    5 *
    6 * @author Administrator
    7 *
    8 */
    9
    10  import acm.program.*;
    11
    12  public class Ex4_2 extends ConsoleProgram {
    13
    14 public void run() {
    15
    16 for (int i=1; i<11; i++) {
    17 println("This old man, he played " + i + ".");
    18 println("He played knick-knack on my ");
    19 switch(i){
    20 case 1: println("thumb"); break;
    21 case 2: println("shoe"); break;
    22 case 3: println("knee"); break;
    23 case 4: println("door"); break;
    24 case 5: println("hive"); break;
    25 case 6: println("sticks"); break;
    26 case 7: println("up to heaven"); break;
    27 case 8: println("pate"); break;
    28 case 9: println("spine"); break;
    29 case 10: println("shin"); break;
    30 default: break;
    31 }
    32
    33 println("With a knick-knack, paddy-whack.");
    34 println("Give you dog a bone.");
    35 println("This old man came roling home.");
    36 println();
    37
    38 }
    39 }
    40
    41
    42 }

    3.

    1 /**
    2 * File: Ex4_3.java
    3 * ----------------
    4 * This program reads in a positive integer N and then calculates and
    5 * displays the sum of the first N odd integers.
    6 *
    7 * @author Administrator
    8 *
    9 */
    10
    11  import acm.program.*;
    12
    13  public class Ex4_3 extends ConsoleProgram {
    14
    15 public void run() {
    16
    17 int sum = 0;
    18 int n = 1;
    19
    20 int N = readInt("Enter N: ");
    21 for (int i=0; i<N; i++) {
    22 sum += n;
    23 n += 2;
    24 }
    25 println("sum = " + sum);
    26
    27 }
    28
    29 }

    4.略。

    5.

    1 /**
    2 * File: Ex4_5.java
    3 * ----------------
    4 * This program display the integers between 1 and 100 that are
    5 * divisible by either 6 or 7 but not both.
    6 *
    7 * @author Administrator
    8 *
    9 */
    10
    11  import acm.program.*;
    12
    13  public class Ex4_4 extends ConsoleProgram {
    14
    15 public void run() {
    16 for (int i=1; i<=100; i++){
    17
    18 if((i%6==0 || i%7==0)&&(i%42!=0)){
    19 println(i);
    20 }
    21 }
    22 }
    23
    24 }

    6.略。

    7.

    1 /**
    2 * File: Ex4_7.java
    3 * ----------------
    4 * This program reverse the order of the integer.
    5 *
    6 * @author Administrator
    7 *
    8 */
    9
    10  import acm.program.*;
    11
    12  public class Ex4_7 extends ConsoleProgram {
    13
    14 public void run() {
    15 println("This program reverse the digits in an intger.");
    16 int n = readInt("Enter a positive integer: ");
    17 int rn = 0;
    18 while (n>0){
    19 rn = rn*10 + n%10;
    20 n /=10;
    21 }
    22 println("The reversed number is " + rn);
    23 }
    24
    25 }

    8.

    1 /**
    2 * File: Ex4_8.java
    3 * ----------------
    4 * This program counts backwards from the value START
    5 * to zero, as in the countdown preceding a rocket
    6 * launch.
    7 *
    8 * @author Administrator
    9 *
    10 */
    11
    12  import acm.program.*;
    13
    14  public class Ex4_8 extends ConsoleProgram {
    15
    16 private static final int START = 10;
    17
    18 public void run() {
    19 int t = START;
    20 while ( t>=0){
    21 println(t);
    22 t--;
    23 }
    24 println("Liftoff!");
    25 }
    26
    27 }

    9.

    1 /**
    2 * File: Ex4_9.java
    3 * ----------------
    4 * Fibonacci sequence.
    5 *
    6 * @author Administrator
    7 *
    8 */
    9
    10  import acm.program.*;
    11
    12  public class Ex4_9 extends ConsoleProgram {
    13
    14 public void run(){
    15
    16 int n1 = 0;
    17 int n2 = 1;
    18 int n = 0;
    19 int i = 2;
    20
    21 println("F0 = " + n1);
    22 println("F1 = " + n2);
    23
    24 while(n<10000){
    25 n = n1 + n2;
    26 println("F" + i + " = " + n);
    27 n1 = n2;  //Fn = Fn-1 + Fn-2
    28 n2 = n;
    29 i++;
    30 }
    31
    32 }
    33
    34 }

    10.略

    11. 这里要画个8x8的棋盘,方格放置简单,放圆有点困难,看图总结,啥时候放圆。

    1 /**
    2 * File: Ex4_11.java
    3 * -----------------
    4 * checkboard
    5 *
    6 * @author Administrator
    7 *
    8 */
    9 import acm.graphics.*;
    10 import acm.program.*;
    11
    12 public class Ex4_11 extends GraphicsProgram {
    13 public void run(){
    14 for (int i=0; i<8; i++){
    15 for(int j=0; j<8; j++){
    16 add(new GRect(i*20, j*20, 20, 20));
    17 if(((j%2==0)&&(i%2!=0))||((j%2!=0)&&(i%2==0))){
    18 add(new GOval(i*20, j*20, 20, 20));
    19
    20 }
    21 }
    22 }
    23
    24 }
    25
    26 }

    12. 简单的日历,画表简单,就是放格子,加数字有点困难。

    1 /**
    2 * File: Ex4_12.java
    3 * -----------------
    4 * calendar.
    5 *
    6 * @author Administrator
    7 *
    8 */
    9 import acm.graphics.*;
    10 import acm.program.*;
    11
    12 public class Ex4_12 extends GraphicsProgram {
    13 public void run() {
    14 int rows = (DAYS_IN_MONTH + DAY_MONTH_STARTS) / 7;
    15 if ((DAY_MONTH_STARTS + DAYS_IN_MONTH) % 7 != 0){
    16 rows++;
    17 }
    18
    19 for (int i = 0; i < rows; i++) {
    20 int y = i * DAY_HEIGHT;
    21
    22 for (int j = 0; j < 7; j++) {
    23 int x = j * DAY_WIDTH;
    24
    25 add(new GRect(x, y, DAY_WIDTH, DAY_HEIGHT));
    26
    27 int date = (((7 * i) + j) - DAY_MONTH_STARTS) + 1;
    28
    29 if (date > 0 && date <= DAYS_IN_MONTH) {
    30 add(new GLabel("" + date + ".", x + 5, y + 15));
    31 }
    32
    33 }
    34 }
    35 }
    36
    37 // The number of days in the month
    38 private static final int DAYS_IN_MONTH = 31;
    39
    40 // The day of the week on which the month starts
    41 // Sunday = 0, Monday = 1, ...
    42 private static final int DAY_MONTH_STARTS = 5;
    43
    44 // The width in pixels of a day on the calendar
    45 private static final int DAY_WIDTH = 40;
    46
    47 // The height in pixels of a day on the calendar
    48 private static final int DAY_HEIGHT = 30;
    49
    50
    51 }

  • 相关阅读:
    赶集个人 车源推送到 58
    fsockopen get,post 封装 (转)
    php write excel
    《HTTP协议详解》读书笔记---请求篇之消息报头
    《HTTP协议详解》读书笔记---请求篇之响应状态码
    《HTTP协议详解》读书笔记---请求篇之情求方法
    高效使用 GNOME Files(Nautilus) 管理文件
    手机无法连接 GSConnect:防火墙未开放端口
    Ubuntu 上安装 ArtiPub(一款开源的一文多发平台)
    GNOME 桌面必备扩展(GNOME Shell Extensions)
  • 原文地址:https://www.cnblogs.com/halflife/p/2075705.html
Copyright © 2020-2023  润新知