


全文預覽已結束
下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- ICU膿毒血癥的護理措施
- 幼兒園網(wǎng)絡安全管理培訓
- 幼犬術后護理常規(guī)
- 護理學基礎知識培訓
- 起重作業(yè)安全培訓
- 安全教育網(wǎng)課
- 2025年互聯(lián)網(wǎng)廣告精準投放算法效果評估與廣告投放效果效果評估創(chuàng)新報告
- 腫瘤登記隨訪方法
- 2025年工業(yè)互聯(lián)網(wǎng)平臺自然語言處理技術賦能工業(yè)生產(chǎn)報告
- 儲能系統(tǒng)在智能電網(wǎng)中的應用與系統(tǒng)穩(wěn)定性研究報告
- 改革開放簡史
- 哮喘的治療與護理講課件
- 部編版語文五年級下冊全冊復習知識匯-總
- 2025年河北地礦中地建設有限公司招聘筆試參考題庫含答案解析
- 常見護理工作應急預案及流程
- 2025年聊城市茌平區(qū)高鐵建設發(fā)展有限公司招聘筆試參考題庫含答案解析
- 湖南省長沙市寧鄉(xiāng)市2024-2025學年三年級下學期6月期末科學試卷(含答案)
- 2025五年級道德與法治下冊期末綜合測試卷(含答案)
- 2025至2030中國房產(chǎn)證抵押貸款行業(yè)市場深度分析及投資與前景預測報告
- 定向士官心理測試題及答案
- 2025至2030中國LNG運輸行業(yè)市場發(fā)展分析及前景預測與戰(zhàn)略規(guī)劃報告
評論
0/150
提交評論