


全文預(yù)覽已結(jié)束
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 航拍項(xiàng)目合作協(xié)議中關(guān)于版權(quán)歸屬與使用范圍的補(bǔ)充條款
- 網(wǎng)紅奶茶店區(qū)域代理銷售及推廣合同
- 老齡公寓專業(yè)管理及社區(qū)服務(wù)委托合同
- 歐洲移民臨時(shí)倉儲(chǔ)租賃及倉儲(chǔ)貨物清點(diǎn)協(xié)議
- 短視頻平臺(tái)粉絲權(quán)益轉(zhuǎn)移與合規(guī)管理合同
- 互聯(lián)網(wǎng)智能零售平臺(tái)供應(yīng)鏈金融解決方案合同
- 醫(yī)療邊緣計(jì)算節(jié)點(diǎn)部署與醫(yī)療影像處理服務(wù)合同
- 銷售簽單培訓(xùn)
- 培訓(xùn)班課程介紹策略
- 遺產(chǎn)繼承稅務(wù)代理合同(2篇)
- 大型心血管造影系統(tǒng)及其DSA質(zhì)量控制
- 2024-2025學(xué)年人教版七年級(jí)(下)期中數(shù)學(xué)試卷(考試范圍:第7~9章) (含解析)
- 人工智能安全監(jiān)控系統(tǒng)開發(fā)協(xié)議
- 油田夏季十防培訓(xùn)課件
- 工傷賠償私了協(xié)議書范本
- 學(xué)生心理健康一生一策檔案表
- 2025年佛山市三水海江建設(shè)投資有限公司招聘筆試參考題庫含答案解析
- 急性心梗診療(2025 )
- 國家義務(wù)教育質(zhì)量監(jiān)測八年級(jí)美術(shù)樣卷
- 2024年江蘇宿遷中考滿分作文《夢想照進(jìn)現(xiàn)實(shí):我的未來職業(yè)暢想》
- 上海市2022年中考英語卷試題真題及答案詳解
評(píng)論
0/150
提交評(píng)論