


全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Mehran SahamiCS 106AHandout #12October 5, 2007Control StatementsBased on a handout by Eric RobertsThis handout offers some additional notes on Javas control statements (described morefully in Chapter 4 of the textbook) that emphasize the important concepts. It alsodescribes a programming problem making use of various control structures.To write programs, you need to understand control statements from two perspectives: youmust have a holistic sense of when to use them and why, but you must also learn tounderstand the reductionistic details. For this big-picture perspective, you can rely to alarge extent on your experience from Karel: If you want to test a condition that requires an if statement in Karel, you need the ifstatement in Java. If you would use the while or for statement in Karel, you will presumably use thesame statement form in Java.The other holistic point that is essential about control statements is that the control lineis conceptually independent from the body. Thus, if you see a construct likefor (int i = 0; i 10; i+) statementsControl lineBodythe statements in the body will be repeated for each of the values of i from 0 to 9. Itdoesnt matter at all what those statements are.Boolean dataAnother important topic is that of the data type boolean, which is the means by whichJava programs ask questions. In Karel, the counterparts to boolean are the conditionssuch as frontIsClear() or beepersPresent(). In Java, the range of availableconditions is much richer and involves the relational operators and the logical operators(both covered on page 78 of textbook). The most important lessons to take from thesesections are: Watch out for confusing = (assignment) with = (equality). This feature of severalprogramming languages (including C, C+, and Java) has probably caused more bugsthan any other. Be careful to understand both the interpretation and the evaluation order of the logicaloperators & (and), | (or), and ! (not).The time you put into making sure you understand boolean data now will pay for itselfmany times over when the programs get more complicated later in the quarter.2Checkerboard problemCreate a GraphicsProgram subclass that draws a checkerboard in the graphics window.The number of rows and columns are given by the named constants NROWS and NCOLUMNS,and the squares should be sized so that they fill the vertical space. For example, if NROWSand NCOLUMNS are both 8, running this program should produce the following output:Graphics library documentationThe javadoc documentation for the ACM libraries is available under the “Links” section/javadoc/student/index.html Figure 1 will help with the assignment.Figure 1. Some useful methods in acm.graphicsConstructorsnew GLabel(String text)ornew GLabel(String text, double x, double y)Creates a new GLabel object; the second form sets its location as well.new GRect(double x, double y, double width, double height)Creates a new GRect object; the x and y parameters can be omitted and default to 0.new GOval(double x, double y, double width, double height)Creates a new GOval object; the x and y parameters can be omitted and default to 0.new GLine(double x1, double y1, double x2, double y2)Creates a new GLine object connecting (x1, y1) and (x2, y2).Methods common to all graphical objectvoid setLocation(double x, double y)Sets the location of this object to the specified coordinates.void move(double dx, double dy)Moves the object using the displacements dx and dy.double getWidth()Returns the width of the object.double getHeight()Returns the height of the object.void setColor(Color c)Sets the color of the object.Methods available for GRect and GOval onlyvoid setFilled(boolean fill)Sets whether this object is filled (true means filled, false means outlined).boolean isFilled()Returns true if the object is filled.void setFillColor(Color c)Sets the color used to fill this object. If the color is null, filling uses the color of the object.Methods available for GLabel onlyvoid setFont(String fontName)Sets the font, as described in Chapter 5.double getAscent()Returns the height above the baseline.CS 106A Also,3Solution to the Checkerboard problem/* File: Checkerboard.java* -* This program draws a checkerboard.*/import acm.graphics.*;import gram.*;/* This class draws a checkerboard on the graphics window.* The size of the checkerboard is specified by the* constants NROWS and NCOLUMNS, and the checkboard fills* the vertical space available.*/public class Checkerboard extends GraphicsProgram /* Number of rows */private static final int NROWS = 8;/* Number of columns */private static final int NCOLUMNS = 8;/* Runs t
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 青岛港湾职业技术学院《算法与数据结构》2023-2024学年第一学期期末试卷
- 东华大学《数学拓展课程》2023-2024学年第一学期期末试卷
- 人工智能时代拔尖创新人才发现机制与培养模式创新研究
- 武汉电力职业技术学院《美国文学史及作品读》2023-2024学年第一学期期末试卷
- 重庆健康职业学院《房地产营销管理》2023-2024学年第一学期期末试卷
- 广东科技学院《数学分析(1)》2023-2024学年第一学期期末试卷
- 遂宁能源职业学院《欧美设计规范释义二》2023-2024学年第一学期期末试卷
- 南京信息职业技术学院《建筑结构试验与测试技术》2023-2024学年第一学期期末试卷
- 安徽工业职业技术学院《外国性格舞》2023-2024学年第一学期期末试卷
- 团建聚会野餐活动方案
- 店铺转让合同协议书模板
- 2025辽宁中考:历史必考知识点
- 农村电商赋能乡村振兴培训课程大纲
- 2025届重庆康德三诊英语+答案
- 耳石症教学课件
- 玻璃吊装施工专项施工方案
- 探寻河北省竞技体育可持续发展的社会资源密码:现状、挑战与破局之策
- 焊接安全知识考核试题及答案
- 2025燃气电厂智能巡检系统技术方案
- 高中学校托管协议书范本
- ICU谵妄管理课件
评论
0/150
提交评论