《斯坦福大學(xué)開放課程:編程方法》講義#12.doc_第1頁
《斯坦福大學(xué)開放課程:編程方法》講義#12.doc_第2頁
《斯坦福大學(xué)開放課程:編程方法》講義#12.doc_第3頁
全文預(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論