免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Mehran SahamiCS 106AHandout #12October 5, 2007Assignment #2: Simple Java ProgramsDue: 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 consisting of bricksarranged in horizontal rows, so that the number of bricks in each row decreases byone 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 useconstants for the following parameters:BRICK_WIDTHBRICK_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 ableto change those values in your program. 2 2. Suppose that youve been hired to produce a program that draws an image of anarchery targetor, if you prefer commercial applications, a logo for a nationaldepartment store chainthat looks like this:This figure is simply three GOval objects, two red and one white, drawn in the correctorder. The outer circle should have a radius of one inch (72 pixels), the white circlehas a radius of 0.65 inches, and the inner red circle has a radius of 0.3 inches. Thefigure should be centered in the window of a GraphicsProgram subclass.3. Write a GraphicsProgram subclass that draws a partial diagram of the gramclass hierarchy, as follows:The only classes you need to create this picture are GRect, GLabel, and GLine. Themajor part of the problem is specifying the coordinates so that the different elements 3 of the picture are aligned properly. The aspects of the alignment for which you areresponsible are: The width and height of the class boxes should be specified as named constantsso that they are easy to change. The labels should be centered in their boxes. You can find the width of a label bycalling label.getWidth() and the height it extends above the baseline by callinglabel.getAscent(). If you want to center a label, you need to shift its origin byhalf of these distances in each direction. The connecting lines should start and end at the center of the appropriate edge ofthe box. The entire figure should be centered in the window.4.In high-school geometry, you learned the Pythagorean theorem for the relationship ofthe 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 onepiece thats missing is taking square roots, which you can do by calling the standardfunction Math.sqrt. For example, 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 calculatesthe solution of c as a double. Your program should be able to duplicate thefollowing sample run:a + b 4 5. Write a ConsoleProgram that reads in a list of integers, one per line, until a sentinelvalue of 0 (which you should be able to change easily to some other value). When thesentinel is read, your program should display the smallest and largest values in thelist, as illustrated in this sample run:Your program should handle the following special cases: If the user enters only one value before the sentinel, the program should reportthat value as both the largest and smallest. If the user enters the sentinel on the very first input line, then no values have beenentered, and your program should display a message to that effect.6. Douglas Hofstadters Pulitzer-prize-winning book Gdel, Escher, Bach containsmany interesting mathematical puzzles, many of which can be expressed in the formof computer programs. In Chapter XII, Hofstadter mentions a wonderful problem thatis well within the scope of the control statements from Chapter 4. The problem canbe 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 thefollowing example, starting with the number 15:154623703510653is 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:4623703510653160 5 16080402010516842is even, so 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:804020105168421As you can see from this example, the numbers go up and down, but eventuallyatleast for all numbers that have ever been triedcomes down to end in 1. In somerespects, this process is reminiscent of the formation of hailstones, which get carriedupward by the winds over and over again before they finally descend to the ground.Because of this analogy, this sequence of numbers is usually called the Hailstonesequence, although it goes by many other names as well.Write a ConsoleProgram that reads in a number from the user and then displays theHailstone sequence for that number, just as in Hofstadters book, followed by a lineshowing the number of steps taken to reach 1. For example, your program should be
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年下半年“才聚齐鲁成就未来”山东黄金集团2025夏季校园招聘易考易错模拟试题(共500题)试卷后附参考答案
- 2025山东高速集团限公司社会招聘(1593人)易考易错模拟试题(共500题)试卷后附参考答案
- 2025山东济南光明电力服务限责任公司招聘53人易考易错模拟试题(共500题)试卷后附参考答案
- 2025届云南省建设投资控股集团限公司校园招聘易考易错模拟试题(共500题)试卷后附参考答案
- 2025安徽芜湖市运达轨道交通建设运营限公司招聘129人易考易错模拟试题(共500题)试卷后附参考答案
- 2025安徽电信春季校园招聘易考易错模拟试题(共500题)试卷后附参考答案
- 2025安徽庐江县部分县属国企业招聘60人易考易错模拟试题(共500题)试卷后附参考答案
- 2025安徽合肥肥西自来水限公司招聘10人易考易错模拟试题(共500题)试卷后附参考答案
- 2025安徽亳州市人民政府办公室招聘见习生7人易考易错模拟试题(共500题)试卷后附参考答案
- 2025国网国际融资租赁限公司招聘(32人)易考易错模拟试题(共500题)试卷后附参考答案
- 矿业权评估师考试(油气矿产资源勘查与实物量估算)全真题库及答案(2025年东营)
- 信创基础知识培训课件
- (完整版)路面拆除施工方案
- 中远海运集团2025年社会招聘第十八次集中笔试
- 全国大学生职业规划大赛《机电一体化技术》专业生涯发展展示【高职(专科)】
- 绿色能源5万吨年生物质颗粒燃料生产线可行性研究报告
- 水尺施工方案
- 占道施工交通安全培训课件
- 医院放射源安全培训课件
- 工程项目结算审核指标与绩效考核标准
- 地下综合管廊安全培训课件
评论
0/150
提交评论