《斯坦福大学开放课程:编程方法》讲义#13

上传人:ji****n 文档编号:45598610 上传时间:2018-06-17 格式:DOC 页数:5 大小:312.03KB
返回 下载 相关 举报
《斯坦福大学开放课程:编程方法》讲义#13_第1页
第1页 / 共5页
《斯坦福大学开放课程:编程方法》讲义#13_第2页
第2页 / 共5页
《斯坦福大学开放课程:编程方法》讲义#13_第3页
第3页 / 共5页
《斯坦福大学开放课程:编程方法》讲义#13_第4页
第4页 / 共5页
《斯坦福大学开放课程:编程方法》讲义#13_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《《斯坦福大学开放课程:编程方法》讲义#13》由会员分享,可在线阅读,更多相关《《斯坦福大学开放课程:编程方法》讲义#13(5页珍藏版)》请在金锄头文库上搜索。

1、Mehran Sahami CS 106AHandout #12 October 5, 2007 Assignment #2: Simple Java Programs Due: 3:15pm on Monday, October 15thBased on a handout by Eric RobertsYour job in this assignment is to write programs to solve each of these six problems.1. Write a GraphicsProgram subclass that draws a pyramid cons

2、isting of bricks arranged in horizontal rows, so that the number of bricks in each row decreases by one as you move up the pyramid, as shown in the following sample run:The pyramid should be centered at the bottom of the window and should use constants for the following parameters:BRICK_WIDTH BRICK_

3、HEIGHTBRICKS_IN_BASEThe width of each brick (30 pixels) The height of each brick (12 pixels) The number of bricks in the base (14)The numbers in parentheses show the values for this diagram, but you must be able to change those values in your program. 2 2. Suppose that youve been hired to produce a

4、program that draws an image of an archery targetor, if you prefer commercial applications, a logo for a national department store chainthat looks like this:This figure is simply three GOval objects, two red and one white, drawn in the correct order. The outer circle should have a radius of one inch

5、(72 pixels), the white circle has a radius of 0.65 inches, and the inner red circle has a radius of 0.3 inches. The figure should be centered in the window of a GraphicsProgram subclass.3. Write a GraphicsProgram subclass that draws a partial diagram of the acm.program class hierarchy, as follows:Th

6、e only classes you need to create this picture are GRect, GLabel, and GLine. The major part of the problem is specifying the coordinates so that the different elementsa b 3 of the picture are aligned properly. The aspects of the alignment for which you are responsible are: The width and height of th

7、e class boxes should be specified as named constants so that they are easy to change. The labels should be centered in their boxes. You can find the width of a label by calling label.getWidth() and the height it extends above the baseline by callinglabel.getAscent(). If you want to center a label, y

8、ou need to shift its origin by half of these distances in each direction. The connecting lines should start and end at the center of the appropriate edge of the box. The entire figure should be centered in the window.4.In high-school geometry, you learned the Pythagorean theorem for the relationship

9、 of the lengths of the three sides of a right triangle:a2 + b2 = c2which can alternatively be written as:c =22Most of this expression contains simple operators covered in Chapter 3. The one piece thats missing is taking square roots, which you can do by calling the standard function Math.sqrt. For e

10、xample, the statementdouble y = Math.sqrt(x);sets y to the square root of x.Write a ConsoleProgram that accepts values for a and b as ints and then calculates the solution of c as a double. Your program should be able to duplicate the following sample run: 4 5. Write a ConsoleProgram that reads in a

11、 list of integers, one per line, until a sentinel value of 0 (which you should be able to change easily to some other value). When the sentinel is read, your program should display the smallest and largest values in the list, as illustrated in this sample run:Your program should handle the following

12、 special cases: If the user enters only one value before the sentinel, the program should report that value as both the largest and smallest. If the user enters the sentinel on the very first input line, then no values have been entered, and your program should display a message to that effect.6. Do

13、uglas Hofstadters Pulitzer-prize-winning book Gdel, Escher, Bach contains many interesting mathematical puzzles, many of which can be expressed in the form of computer programs. In Chapter XII, Hofstadter mentions a wonderful problem that is well within the scope of the control statements from Chapt

14、er 4. The problem can be expressed as follows:Pick some positive integer and call it n. If n is even, divide it by two. If n is odd, multiply it by three and add one. Continue this process until n is equal to one.On page 401 of the Vintage edition, Hofstadter illustrates this process with the follow

15、ing example, starting with the number 15:1546 23 70 35 106 53is odd, so I make 3n + 1: is even, so I take half: is odd, so I make 3n + 1: is even, so I take half: is odd, so I make 3n + 1: is even, so I take half: is odd, so I make 3n + 1:46 23 70 35 106 53 160 5 160 80 40 20 10 5 16 8 4 2is even, s

16、o I take half: is even, so I take half: is even, so I take half: is even, so I take half: is even, so I take half: is odd, so I make 3n + 1: is even, so I take half: is even, so I take half: is even, so I take half: is even, so I take half:80 40 20 10 5 16 8 4 2 1As you can see from this example, the numbers go up and down, but eventuallyat least for all numbers that have ever been triedcomes down to end in 1. In some respects, this process is reminiscent of the forma

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 初中教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号