


全文預(yù)覽已結(jié)束
下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 高效證明教育記錄可追溯性與合規(guī)性研究
- 多模態(tài)生物技術(shù)與行業(yè)深度融合展望
- 廣告制作過(guò)程中的團(tuán)隊(duì)協(xié)作與項(xiàng)目管理技巧的分享
- 2025至2030中國(guó)脂肪酸乙氧基化物行業(yè)市場(chǎng)占有率及投資前景評(píng)估規(guī)劃報(bào)告
- 新時(shí)代藥物制劑技術(shù)的發(fā)展與應(yīng)用研究
- 2025至2030中國(guó)育亨賓行業(yè)產(chǎn)業(yè)運(yùn)行態(tài)勢(shì)及投資規(guī)劃深度研究報(bào)告
- 2025至2030中國(guó)美藤果油行業(yè)產(chǎn)業(yè)運(yùn)行態(tài)勢(shì)及投資規(guī)劃深度研究報(bào)告
- 2025至2030中國(guó)細(xì)胞培養(yǎng)基行業(yè)產(chǎn)業(yè)運(yùn)行態(tài)勢(shì)及投資規(guī)劃深度研究報(bào)告
- 2025至2030中國(guó)線路板行業(yè)市場(chǎng)深度調(diào)研及發(fā)展前景與投資報(bào)告
- 人工智能在能源管理中的應(yīng)用
- 2023中國(guó)專利獎(jiǎng)申報(bào)實(shí)務(wù)
- 常見(jiàn)骨關(guān)節(jié)疾病的評(píng)定技術(shù)-肩關(guān)節(jié)周圍炎的評(píng)定技術(shù)(康復(fù)評(píng)定技術(shù)課件)
- 益海嘉里(盤錦)糧油工業(yè)有限公司稻殼鍋爐可研報(bào)告
- JGJ106-2014 建筑基樁檢測(cè)技術(shù)規(guī)范
- 2023年中國(guó)石化河北石家莊石油分公司社會(huì)招聘20人筆試模擬試題及答案解析
- 太陽(yáng)能熱水系統(tǒng)設(shè)計(jì)
- 醫(yī)務(wù)科崗前培訓(xùn)
- 共青團(tuán)團(tuán)課主題班會(huì)課件PPT模板PPT
- GB/T 8685-2008紡織品維護(hù)標(biāo)簽規(guī)范符號(hào)法
- 合成氨行業(yè)發(fā)展現(xiàn)狀及趨勢(shì)分析
- 2022年徐聞縣(中小學(xué)、幼兒園)教師招聘筆試試題及答案解析
評(píng)論
0/150
提交評(píng)論